c++ - 未知的覆盖说明符,缺少类型说明符

标签 c++

首先,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/

相关文章:

c++ - 尝试以相反的顺序使用 extern

c++ - 为什么 unique_ptr 有两个函数 reset 和 operator= 做类似的事情但不重载?

c++ - 为什么多线程没有加速数组的简单复制?

c++ - 试图了解 RapidXml 内存分配

c++ - 使用构造函数将双链表中的指针初始化为 NULL

c++ - 如何在具有多个源文件的 Ubuntu 上用 G++ 编译程序?

c++ - 如何在编译器级别实现 `delete[] obj` 和 `delete obj`

c++ - 使用 DirectShow 将视频捕获为 AVI 文件

c++ - 在分配新版本的资源时,在 C++ 中处理复杂资源指针的做法是什么?

c++ - 调用正确的构造函数