[英]“[Classname] does not name a type” even after including .h file
我的.cpp文件中的每个函数原型和功能上都出现此编译器错误(“ CLASSNAME不命名类型”)
.h文件:
#ifndef MOVIETREE_HPP
#define MOVIETREE_HPP
struct MovieNode
{
...//members and such
};
class MovieTree
{
public:
MovieTree();
~MovieTree();
void printMovieInventory();
...//more functions
protected:
private:
void printMovieInventory(MovieNode * node);
MovieNode* search(std::string title);
MovieNode *root;
};
#endif // MOVIETREE_HPP
.cpp文件:
#ifndef MOVIETREE_HPP
#define MOVIETREE_HPP
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "MovieTree.hpp"
using namespace std;
MovieTree::MovieTree(); //error code here and all prototypes and functions below
MovieTree::~MovieTree();
void MovieTree::printMovieInventory();
...//more function prototypes
MovieTree::MovieTree(){
}
MovieTree::~MovieTree(){
}
void MovieTree::printMovieInventory(){
...//body
}
...//more function bodies
#endif
我遇到的所有其他论坛和问题都提供了包含头文件的简单解决方案。 我已经将其包含在我的代码中。
我检查了三遍,很高兴我拼写的一切都正确。 我做错了什么?
问题是.cpp
文件中的这些行:
#ifndef MOVIETREE_HPP
#define MOVIETREE_HPP
...
#endif
这些行仅应放在头文件中,而不是在程序文件中-头文件使用它们来检测是否包含了两次,因此它不会尝试重新定义所有内容。 通过自己在#include <movietree.hpp>
行之前进行设置,您会以为它已经被包含,因此它什么也不做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.