使用C+时出现编译错误+;遗产 我是这个网站的新手,我正在尝试C++中的简单继承示例。 我检查了我的代码很多次,我真的没有发现任何错误,但是编译器给了我错误:

使用C+时出现编译错误+;遗产 我是这个网站的新手,我正在尝试C++中的简单继承示例。 我检查了我的代码很多次,我真的没有发现任何错误,但是编译器给了我错误:,c++,inheritance,C++,Inheritance,我的代码: #ifndef READWORDS_H #define READWORDS_H using namespace std; #include "ReadWords.h" /** * ReadPunctWords inherits ReadWords, so MUST define the function filter. * It chooses to override the default constructor. */ class ReadPunctWords: pub

我的代码:

#ifndef READWORDS_H
#define READWORDS_H
using namespace std;
#include "ReadWords.h"

/**
 * ReadPunctWords inherits ReadWords, so MUST define the function filter.
 * It chooses to override the default constructor.
 */

class ReadPunctWords: public ReadWords {
    public:
    bool filter(string word);
};

#endif
以及我从编译器中得到的信息:

ReadPunctWords.h:11: error: expected class-name before '{' token
ReadPunctWords.h:13: error: `string' has not been declared
ReadPunctWords.h:13: error: ISO C++ forbids declaration of `word' with no type

Tool completed with exit code 1
我真的不知道我哪里弄错了,因为我觉得它很好?
感谢您发现的任何错误。

您需要包括字符串:

#include <string>
这也是有争议的,更具可读性。此外,您应该将字符串作为
常量&

bool filter(const std::string& word);
避免不必要地复制字符串。最后,你的头球防守似乎不对劲。应该改变吗?到目前为止,它们似乎与您的另一个标题中使用的相同,这可能会有效地阻止它被包括在内

如果您定义了
READWORDS\u H
,然后包括
READWORDS.H
,并且如果这也包括:

#ifndef READWORDS_H
#define READWORDS_H
那么该文件中的任何内容都不会被处理。如果是这种情况,
ReadWords
作为一个类将不会被定义,并且您不能从中继承。你的卫兵可能是:

READPUNCTWORDS_H

您需要包括
并指定名称空间:

#包括
使用名称空间std;

此外,您的include-guard可能应该命名为
READPUNCHTWORDS\u H
,而不是
READWORDS\u H


编辑:再想一想,GMan没有将
使用名称空间
放在头文件中是正确的-用
std::string
来限定字符串。

ReadWords类def看起来也有问题(第11行的消息)-我们需要查看.h文件

aha-您使用的sentinial def阻止读取单词。h包括被读取

你需要

 #ifndef _READPUNCTWORDS_H
 #define _READPUNCTWORDS_H

这种特殊形式的错误通常是由未定义的类型(至少在代码语法正确时)引起的,在这种情况下,可能是类ReadWords,也可能是std::string

正如其他海报所写,您需要包括获取std::string,但也需要包括您的警卫

#ifndef READWORDS_H
#define READWORDS_H
几乎可以肯定的是,在ReadWords中与警卫发生了冲突。你需要确保你的守卫在不同的头文件中是不同的,否则你会遇到这样的冲突。你应该把警卫换成

#ifndef READPUNCTWORDS_H
#define READPUNCTWORDS_H

// ...

#endif
事实上,最好有更详细的守卫,以确保他们不会发生冲突。我们使用这种形式的警卫

#ifndef MODULE_OR_PATH_FILE_H_INCLUDED
#define MODULE_OR_PATH_FILE_H_INCLUDED

// ...

#endif
这确保了不同的模块或库(具有类似名称的头)不会发生冲突,最后包含的东西是我自己的特殊缺点,这使保护更具可读性

在头文件中放置“using”声明也是一种不好的做法,因为它会在包含头文件的所有位置的全局命名空间中放置(可能不需要或冲突的)符号。就我个人而言,为了清晰起见,我更喜欢保留名称空间,或者在cpp文件中使用别名(例如,如果名称空间很长的话)

namespace fs = boost::filesystem;

我还怀疑这些人包括警卫。如果它们在两个头文件中的命名方式相同,那么将ReadWords.h粘贴到readputchwords.h中时,结果应该如下所示

#ifndef READWORDS_H // Is READWORDS_H defined? No, proceeding
#define READWORDS_H // Now it is defined

// Contents of ReadWords.h is pasted in here
#ifndef READWORDS_H // Is READWORDS_H defined? Yes, ignoring the contents of ReadWords.h (skipping til #endif)
#define READWORDS_H

class ReadWords { ... }; // This is never seen by the compiler as the preprocessor removed it

#endif

class ReadPunctWords: public ReadWords { // Error: ReadWords isn't declared...
public:
    bool filter(string word);
};

#endif

非常感谢。我包含了它,但仍然得到这个错误:ReadPunctWords。h:12:error:在“{”令牌工具完成之前应该有类名,退出代码为1没有问题。请注意我关于
使用命名空间的建议,如果有任何问题的话。:]谢谢,包含了字符串,我仍然得到这个错误:ReadPunctWords。h:12:error:之前应该有类名'{'令牌工具已完成,退出代码为1请参见我的编辑关于include-guards-ReadWords。h可能未被包括在内。他没有前导下划线,这些都是为编译器保留的。
namespace fs = boost::filesystem;
#ifndef READWORDS_H // Is READWORDS_H defined? No, proceeding
#define READWORDS_H // Now it is defined

// Contents of ReadWords.h is pasted in here
#ifndef READWORDS_H // Is READWORDS_H defined? Yes, ignoring the contents of ReadWords.h (skipping til #endif)
#define READWORDS_H

class ReadWords { ... }; // This is never seen by the compiler as the preprocessor removed it

#endif

class ReadPunctWords: public ReadWords { // Error: ReadWords isn't declared...
public:
    bool filter(string word);
};

#endif