C++ 未知覆盖说明符,缺少类型说明符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35142634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unknown override specifier, missing type specifier
提问by vilequarter
First, Parameter.h
:
首先,Parameter.h
:
#pragma once
#include <string>
class Parameter {
public:
Parameter();
~Parameter();
private:
string constValue;
string varName;
};
And Parameter.cpp
:
并且Parameter.cpp
:
#include "Parameter.h"
using namespace std;
Parameter::Parameter() {};
Parameter::~Parameter() {};
I've brought these two files down to the barest of bones to get the errors that seem to be popping up. At the two private declarations for string
s, I get the two errors:
我已经把这两个文件带到了最简单的地方,以获取似乎突然出现的错误。在string
s的两个私有声明中,我收到两个错误:
'constValue': unknown override specifier
missing type specifier - int assumed. Note: C++ does not support default-int
I've seen several questions with these errors, but each refers to circular or missing references. As I've stripped it down to what's absolutely required, I can see no circular references or references that are missing.
我已经看到了几个关于这些错误的问题,但每个问题都指的是循环引用或缺失引用。由于我已将其精简为绝对需要的内容,因此我看不到任何循环引用或缺少的引用。
Any ideas?
有任何想法吗?
采纳答案by Tas
As @Pete Becker points out in the comments, you need to qualify the name string
as std::string
:
正如@Pete Becker 在评论中指出的那样,您需要将名称限定string
为std::string
:
private:
std::string constValue;
std::string varName;
The compiler just doesn't know what you're talking about, and it's the equivalent of just writing:
编译器只是不知道你在说什么,这相当于只写:
SomeGreatType myMagicalUniversalType
The compiler just doesn't know what type that is unless you've declared, hence the error
除非你已经声明,否则编译器不知道这是什么类型,因此错误
missing type specifier - int assumed
缺少类型说明符 - 假设为 int
You should read up about why you should avoid using namespace std;
.
你应该阅读为什么你应该避免using namespace std;
.
With regards to your question in the comments:
关于您在评论中的问题:
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?
在我编写的所有(工作)类中,我从未放入 std::,而是依赖 using 命名空间 std; 在 .cpp 文件中。为什么这个会有所不同?
I can only infer that at some point before including "Parameter.h" that you had a using namespace std
. E.g.:
我只能推断在包含“Parameter.h”之前的某个时候你有一个using namespace std
. 例如:
// SomeType.h
#using namespace std
...
// Parameter.cpp
#include "SomeType.h"
#include "Parameter.h"
The compiler compiles things top-to-bottom, and including essentially just replaces the #include
with the contents of that file
编译器从上到下编译东西,包括基本上只是用该#include
文件的内容替换