【问题标题】:Unknown override specifier, missing type specifier未知的覆盖说明符,缺少类型说明符
【发布时间】:2016-02-01 23:51:30
【问题描述】:

首先,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() {};

我已将这两个文件归结为最简单的内容,以获取似乎正在弹出的错误。在strings 的两个私有声明中,我得到两个错误:

'constValue': unknown override specifier
missing type specifier - int assumed. Note: C++ does not support default-int

我已经看到了几个与这些错误有关的问题,但每个问题都涉及循环引用或缺少引用。由于我已将其简化为绝对需要的内容,因此我看不到任何循环引用或缺少的引用。

有什么想法吗?

【问题讨论】:

  • 名字应该是std::string
  • 在我编写的所有(工作)类中,我从未放入std::,而是依赖于.cpp 文件中的using namespace std;。为什么这个会有所不同?
  • 使用声明必须在使用名称之前。
  • @David Parady:不,.cpp 文件中的using namespace std; 位于上面的示例中,对头文件没有影响。显然,在您编写的其他“课程”中,您的做法有所不同。
  • TL;DR override specifier 其余的是红鲱鱼。更丰富的错误消息应该是:'constValue' unknown.

标签: c++


【解决方案1】:

正如@Pete Becker 在 cmets 中指出的那样,您需要将名称 string 限定为 std::string

private:
    std::string constValue;
    std::string varName;

编译器只是不知道你在说什么,这相当于只是写:

SomeGreatType myMagicalUniversalType

编译器只是不知道这是什么类型,除非你已经声明,因此错误

缺少类型说明符 - 假定为 int

你应该阅读why you should avoid using namespace std;

关于您在 cmets 中的问题:

在我编写的所有(工作)类中,我从未放入 std::,而是依赖于 using 命名空间 std;在 .cpp 文件中。为什么这个会有所不同?

我只能在包含“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

【讨论】:

  • 我认为你是对的,我可能有一些课程,其中包括在我的其他工作项目中已经拥有using namespace std 的其他课程。我会努力解决这个问题。
  • 帮自己一个忙,avoid #using namespace std
  • 即使您避免使用#using namespace std,您也可能希望始终使用std 字符串,在这种情况下您可以使用#using std::string;然后你只需要输入字符串,它就会知道你想要哪个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
相关资源
最近更新 更多