1

我正在用 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;
    }
};

这是驱动程序:

include cstdlib
include <iostream>

using namespace std;
int main() 
{
    Project112 x;
    int value2 = x.getDecimal();
    return 0;
}

这是一个更大程序的一部分,但我已将其简化为这一点,因为这就是问题所在。每次我尝试运行该程序时,都会出现以下错误:

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

有人可以解释这个问题吗?提前致谢。

4

1 回答 1

5
#include "Project112.h"

添加上面的主要。您忘记包含头文件。和:

using namespace std;

Don't do that in a header file. That imports the everything from the std namespace into the global namespace of any file which includes your header. Just fully qualify the type in a header, i.e., std::string, and I would avoid that in implementation files as well in a large project (though something like using std::string is ok IMO in an implementation file).

于 2012-06-27T02:54:25.973 回答