我正在使用google test和c ++在visual studio 2005中编写一个程序。该程序只执行4次算术操作......
最初我编写了一个带有硬编码值的程序,它工作正常。但现在我希望用户提供输入,因此需要在程序中使用cin
和cout
。但是,当我在代码中添加cin
和cout
语句时,它会出现以下错误:
error C2143: syntax error : missing ';' before '<<' and error C4430:
missing type specifier - int assumed. Note: C++ does not
support default-int for cout and
同样的cin
error C2143: syntax error : missing ';' before '>>' and error C4430:
missing type specifier - int assumed. Note: C++ does not
support default-int
我有三个单独的文件:一个单元测试文件,另一个文件我已经在gtest
中编写了所有测试,第三个我链接了google test提供的主文件。
以下是我的代码:
#include <iostream>
#include "stdafx.h"
#include "gtest/gtest.h"
#include "unittestcomplex.h"
using namespace std;
float a,b;
cout << "Enter two numbers:";
cin >> a >> b;
Arithmatic num;
TEST(complex, Addition)
{
EXPECT_EQ(a+b,num.addition(a,b));
}
TEST(complex,subtraction)
{
EXPECT_EQ(a-b,num.subtraction(a,b));
}
TEST(complex,multiplication)
{
EXPECT_EQ(a*b,num.multiplication(a,b));
}
TEST(complex,division)
{
EXPECT_EQ(a/b,num.division(a,b));
}
这是我编写所有函数的文件:
#include <iostream>
#include "stdafx.h"
# include <conio.h>
using namespace std;
class Arithmatic
{
public:
float addition(float a, float b);
float subtraction(float a, float b);
float multiplication(float a, float b);
float division(float a, float b);
};
float Arithmatic::addition(float a, float b)
{
float sum;
sum = a+b;
return sum;
}
float Arithmatic::subtraction(float a, float b)
{
float difference;
difference = a-b;
return difference;
}
float Arithmatic::multiplication(float a, float b)
{
float mult;
mult = a*b;
return mult;
}
float Arithmatic::division(float a, float b)
{
float div;
div = a/b;
return div;
}
和main()在这里:
#include <iostream>
#include <conio.h>
#include "stdafx.h"
#include "gtest/gtest.h"
using namespace std;
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::getchar(); // keep console window open until Return keystroke
}
我没有改变main()中的任何内容。它是由gtest
提供的。
请告诉我如何删除这些错误并让我的程序用户互动?
答案 0 :(得分:3)
此代码不是有效的C ++,因为它不属于任何函数。
cout << "Enter two numbers:";
cin >> a >> b;
应将其移入函数内。
答案 1 :(得分:2)
你不能拥有这个
cout << "Enter two numbers:";
cin >> a >> b;
Arithmatic num;
功能不足。
将代码移到main
函数中。或者在另一个函数中,并在main
。