首先,Parameter.h
:
#pragma once
#include <string>
class Parameter {
public:
Parameter();
~Parameter();
private:
string constValue;
string varName;
};
还有Parameter.cpp
:
#include "Parameter.h"
using namespace std;
Parameter::Parameter() {};
Parameter::~Parameter() {};
我已将这两个文件归结为最简单的内容,以获取似乎正在弹出的错误。在 string
的两个私有(private)声明中,我得到两个错误:
'constValue': unknown override specifier
missing type specifier - int assumed. Note: C++ does not support default-int
我已经看到了几个与这些错误有关的问题,但每个问题都涉及循环引用或缺少引用。由于我已将其简化为绝对需要的内容,因此我看不到任何循环引用或缺少的引用。
有什么想法吗?
最佳答案
正如@Pete Becker 在评论中指出的那样,您需要将名称 string
限定为 std::string
:
private:
std::string constValue;
std::string varName;
编译器只是不知道你在说什么,这相当于只是写:
SomeGreatType myMagicalUniversalType
编译器只是不知道这是什么类型,除非您已声明,因此会出现错误
missing type specifier - int assumed
你应该阅读 why you should avoid using namespace std;
.
关于你在评论中的问题:
In all the (working) classes I've written, I've never put in the std::, instead relying on the using namespace std; in the .cpp file. Why would this one be any different?
我只能在包含“Parameter.h”之前的某个时候推断出您有一个 using namespace std
。例如:
// SomeType.h
#using namespace std
...
// Parameter.cpp
#include "SomeType.h"
#include "Parameter.h"
编译器从上到下编译,including essentially just replaces the #include
with the contents of that file
关于c++ - 未知的覆盖说明符,缺少类型说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35142634/