[英]“[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.