【发布时间】:2018-04-24 11:50:55
【问题描述】:
我目前对在我的起始项目中包含头文件感到非常沮丧。到目前为止,我有一个链接到我的基文件的头文件,并且我不断收到编译无法读取我的基类的相同错误。
我认为读取头文件有问题。我该怎么办?
//现在更新它显示构建时出现编译错误
主 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吗? -
COLOR在 cpp 文件中定义,在标头中不可见,您在标头文件中使用cout和std命名空间中的其他内容,而没有正确添加::std::。
标签: c++ visual-studio visual-studio-2010 visual-c++