我正在用C++编写一个简单的类(学校,而不是代码).我有一点点C++经验,但已经有一段时间了,所以我重新学习我忘记的东西并学习了很多新的语法(我在Java方面有更多的经验).这是代码:
#include <iostream>
#include <string>
using namespace std;
class Project112
{
private:
string romanNumeral;
int decimalForm;
public:
Project112()
{
romanNumeral = "";
decimalForm = 0;
}
int getDecimal()
{
return decimalForm;
}
};
Run Code Online (Sandbox Code Playgroud)
这是司机:
include cstdlib
include <iostream>
using namespace std;
int main()
{
Project112 x;
int value2 = x.getDecimal();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是一个更大的计划的一部分,但我已将其简化为此,因为这是问题所在.每次我尝试运行该程序时,都会出现以下错误:
main.cpp:10: error: 'Project112' was not declared in this scope
main.cpp:10: error: expected `;' before 'x'
main.cpp:14: error: 'x' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这个问题吗?提前致谢.
#include "Project112.h"
Run Code Online (Sandbox Code Playgroud)
在主要上方添加.您忘了包含头文件.和:
using namespace std;
Run Code Online (Sandbox Code Playgroud)
不要在头文件中这样做.这会将std命名空间中的所有内容导入包含标头的任何文件的全局命名空间.只是完全限定标题中的类型,即,std::string我会在实现文件中以及在大型项目中避免这种情况(尽管using std::string在实现文件中类似于IMO).