我目前收到很多挫折包括在我的起始項目中的頭文件。到目前爲止,我有一個頭文件鏈接到我的基本文件,我不斷收到編譯無法讀取我的基類的錯誤。C++頭文件錯誤:基類未定義
我認爲讀取頭文件時出現問題。我該怎麼辦?
//更新,現在它顯示與構建
主CPP文件
#include <string>
#include "animal.h"
using namespace std;
enum COLOR { Green, Blue, White, Black, Brown };
int main() {
cout << "Starting" << endl;
int value = 0; Mammal *zoo[3];
int i = 0;
cout << "Program exiting …. " << endl;
return 0;
}
頭文件的編譯錯誤
#include <iostream>
#include <string>
#ifndef HEADER_H
#define HEADER_H
using namespace std;
enum COLOR { Green, Blue, White, Black, Brown };
class Animal {
public:
Animal() {
cout << "constructing Animal object " << _name << endl;
}
~Animal() {
cout << "destructing Animal object " << _name << endl;
}
Animal(std::string n, COLOR c) {
_name = n; _color = c;
cout << "constructing " << _name << " Color " <<
endl;
}
virtual void speak() const { cout << "Animal speaks " << endl; }
//void speak() const { cout << "Animal speaks " << endl; }
virtual void move() = 0;
void setName(std::string n) { _name = n; }
void setCOLOR(COLOR c) { _color = c; }
private:
std::string _name; COLOR _color;
};
class Mammal : public Animal {
public:
Mammal() {}
Mammal(std::string n, COLOR c) {
setName(n);
setCOLOR(c);
cout << "constructing Mammal object " << endl;
}
~Mammal() { cout << "destructing Mammal object " << endl; }
};
#endif
在使用之前,不應該在'animal.h'中聲明'COLOR'? – user0042
'COLOR'在cpp文件中定義,並且在頭文件中不可見,您正在使用'cout'和其他來自頭文件中'std'名稱空間的東西,而沒有正確地預先加入':: std ::'。 – VTT