Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误c2143缺少“;”“>>”之前,错误c2143缺失“;”Visual Studio 2005中C++中CIN和CUT的“此代码不是有效的C++,因为它不属于任何函数。< /P> cout << "Enter two numbers:"; cin >> a >> b;_C++_Googletest - Fatal编程技术网

错误c2143缺少“;”“>>”之前,错误c2143缺失“;”Visual Studio 2005中C++中CIN和CUT的“此代码不是有效的C++,因为它不属于任何函数。< /P> cout << "Enter two numbers:"; cin >> a >> b;

错误c2143缺少“;”“>>”之前,错误c2143缺失“;”Visual Studio 2005中C++中CIN和CUT的“此代码不是有效的C++,因为它不属于任何函数。< /P> cout << "Enter two numbers:"; cin >> a >> b;,c++,googletest,C++,Googletest,最初,我用硬编码的值编写了一个程序,它运行得很好。但现在我希望用户提供输入,因此需要在程序中使用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

最初,我用硬编码的值编写了一个程序,它运行得很好。但现在我希望用户提供输入,因此需要在程序中使用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    
我有三个独立的文件:一个是单元测试文件,另一个是我在gtest中编写的所有测试文件,第三个是我链接了googletest提供的主文件

下面是我的代码:

#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;
}
主要内容如下:

#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 
} 
我基本上没有改变什么。它是由gtest提供的。 请告诉我如何删除这些错误并使我的程序用户具有交互性?

你不能这样做

cout << "Enter two numbers:";
cin >> a >> b;
Arithmatic num;
功能失效


将代码移到主函数中。或者在另一个函数中调用它。

< p>此代码不是有效的C++,因为它不属于任何函数。< /P>
cout << "Enter two numbers:";
cin >> a >> b;
它应该在函数中移动

cout << "Enter two numbers:";
cin >> a >> b;