即使在包含 .h 文件后"[Classname] does not name a type"

"[Classname] does not name a type" even after including .h file

本文关键字:does not name type Classname 包含 文件      更新时间:2023-10-16

我得到这个编译器错误("CLASSNAME不命名类型")在我的。cpp文件中的每个函数原型和函数

. 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>行之前设置它,您可以欺骗它,使其认为它已经被包含在内,因此它不做任何事情。