visual studio 2010 express gtest c++ - c++

I am trying to create a basic test file using gtest but I am getting the error : testvs\debug\testvs.exe is not recognized as an internal or external command, operable program or batch file.
test.cpp
#include <gtest\gtest.h>
#include <iostream>
using namespace std;
TEST(test_case,simple_case)
{
int i=0;
int b=1;
int result= i*b;
int expected=0;
EXPECT_EQ(result,expected);
}
main.cpp
#include <gtest\gtest.h>
#include <iostream>
using namespace std;
void main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc,argv);
RUN_ALL_TESTS();
}
when I comment out the lines
TEST(test_case,simple_case)
{
int i=0;
int b=1;
int result= i*b;
int expected=0;
EXPECT_EQ(result,expected);
}
and
::testing::InitGoogleTest(&argc,argv);
RUN_ALL_TESTS();
it does not give any error.
Please help

Related

How can I write a function that returns to array inside the header?

I am trying to create a function that returns in main.cpp in the header and .cpp file and run it in the main function.
This process I do works on main.
#include <iostream>
#include <sstream>
#include "Cards.h"
using namespace std;
//this function returns array
int *function1(){
int a=12;
int b=13;
int c=14;
static int list[3]={a,b,c};
return list;
}
int main(int argc, const char * argv[]) {
int *list;
list=function1();
cout<<list[1]<<endl;
return 0;
}
However, I cannot do these in a header and a separate cpp file.
I have a Cards header
#ifndef Cards_H
#define Cards_H
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Cards{
public:
char suit; //A,H,D,C,S. A is empty card
int number; //00-13
int visibilty;//0 - 1. O invisible 1 is visible
int * function2();
};
#endif
This is the class cpp file
#include "Cards.h"
using namespace std;
//function
int Cards:: function2(){
int a=12;
int b=13;
int c=14;
int list[3]={a,b,c};
return list; // error code Cannot initialize return object of type 'int Cards::*' with an lvalue of type 'int [3]'
}
How do I fix this problem and run it in main?
As pointed out in the comments, there is already a SO thread
Return array in a function
which handles your issue.
If your really want to use C arrays then your program shall look like:
Cards_CStyle.h:
#ifndef Cards_CStyle_H
#define Cards_CStyle_H
using namespace std;
class Cards {
public:
int* function2(int arr[]);
};
#endif
Cards_CStyle.cpp:
#include "Cards_CStyle.h"
using namespace std;
//function
int* Cards::function2(int arr[]){
int a=12, b=13, c=14;
arr[0] = a;
arr[1] = b;
arr[2] = c;
return arr;
}
main_CStyle.cpp:
#include <iostream>
#include "Cards_CStyle.h"
using namespace std;
int main(int argc, const char * argv[]) {
int arr[3]; // Take care that all your functions use size <= 3
Cards cards;
int* list=cards.function2(arr);
cout<<list[1]<<endl;
return 0;
}
As recommended in the comments, you should use the containers of the STL, e.g. array for fixed length or vector for variable length. Assuming that fixed length of 3 will be fine for you, then your code would be looking like this:
Cards_STLStyle.h:
#ifndef Cards_STLStyle_H
#define Cards_STLStyle_H
#include<array>
using namespace std;
typedef array<int, 3> my_array;
class Cards {
public:
my_array function2();
};
#endif
Cards_STLStyle.cpp:
#include "Cards_STLStyle.h"
using namespace std;
//function
my_array Cards::function2(){
int a=12, b=13, c=14;
return my_array { a,b,c};
}
main_STLStyle.cpp:
#include <iostream>
#include <array>
#include "Cards_STLStyle.h"
using namespace std;
int main(int argc, const char * argv[]) {
Cards cards;
my_array list=cards.function2();
cout<<list[1]<<endl;
return 0;
}
Please find more information here:
array

reading string in c++

In the Dev C++ compiler I can write and successfully compile this:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string method;
cin >> method;
return 0;
}
but when I wrote the above code in Visual Studio 2013 (console app mode) I got this error:
Error: no operator ">>" matches these operands
operand types are: std::istream >> std::string
EDIT
in Visual Studio:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string method;
cin >> method;
return 0;
}
I understand what the error tells me. But why in only Visual Studio 2013?
Try to place header "stdafx.h" before other headers.
#include "stdafx.h"
simple exlample for read string in visual studio :
#include "stdafx.h"
#include<iostream>
#include<string>
#include<conio.h>
using namespace std;
int main()
{
std::string str ;
getline(cin, str);
cout << str;
_ getch();
return 0;
}

error: C++ requires a type specifier for all declarations?

#include <cstdio>
#include <iostream>
#include <fstream>
#define INPUT_FILE
#ifdef INPUT_FILE
freopen("test.txt", "r", stdin);
#endif
using namespace std;
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d", &n))
printf("%d\n", n);
return 0;
}
I'm trying to pass input to the program via an input file but, the following error pops up,
error: C++ requires a type specifier for all declarations
freopen("test.txt", "r", stdin);
^~~~~~~
1 error generated.
You can't use a function outside of a function or any other executable part of the program.
Your program is equivalent to
#include <cstdio>
#include <iostream>
#include <fstream>
#define INPUT_FILE
freopen("test.txt", "r", stdin); // Makes no sense
using namespace std;
int main(int argc, char const *argv[])
{
int n;
while(scanf("%d", &n))
printf("%d\n", n);
return 0;
}

visual studio 2012 c++ no cout

I'm trying to learn c++ but a simple method like "cout" and "cin" does not exist
this is my code:
#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
cout>>"hello";
return 0;
}
there is an error that says "error C2065: 'cout' : undeclared identifier"
and "IntelliSense: identifier "cout" is undefined"
cout is declared in the std namespace:
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "hello";
return 0;
}
You're also doing it wrong. Note how I have the angle brackets aboove.
Add #include <iostream> to stdafx.h. I was having trouble with that and it worked for me.
Adding using namespace std; may help, and also do cout << "hello" not >>.
cout is in std so you to have use using namespace std. And for cout operator is like <<. >> this is for input i.e cin.
#include "stdafx.h";
#include "iostream";
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"hello";
system("pause");
return 0;
}

error: 'int main(int, char**)' previously defined here in C++

I'm implementing gtest now, and it gives me an error : main previously defined here.
Here's utest.cpp
// Bring in my package's API, which is what I'm testing
#include "../src/test.cpp"
// Bring in gtest
#include <gtest/gtest.h>
// Declare a test
TEST(TestSuite, testCase1)
{
EXPECT_EQ(5,getX(5));
}
// Run all the tests that were declared with TEST()
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
And here's the code that i'm testing
test.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <Project2Sample/R_ID.h>
#include <geometry_msgs/Twist.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/LaserScan.h>
#include <sstream>
#include "math.h"
int getX(int x)
{
return x;
}
int main(int argc, char **argv)
{
return 0;
}
There's nothing in test.cpp main but actual code will have some codes in main.
I dont have header files for utest and test cpp files
I tried
#ifndef UTEST_H
#define UTEST_H
and didn't solve the error.
The error message states what the problem is, you have two main() functions. I believe you want to remove the duplicate main() from test.cpp.