Person.h为什么这个头文件编译时缺少#include?
#ifndef PERSON_H_
#define PERSON_H_
/* Person.h */
class Person {
int age;
std::string name;
public:
Person(int, std::string);
std::string getName();
int getAge();
};
#endif /* PERSON_H_ */
的人(INT的std :: string)函数的声明使用的std :: string的名字,但我还没有在头文件中包含它。因此,我希望编译器抱怨丢失的符号。但它编译并运行良好!为什么?
的代码的其余部分...
Person.cpp
#include <string>
#include "Person.h"
Person::Person(int age, std::string name)
{
this->name = name;
this->age = age;
}
std::string Person::getName()
{
return this->name;
}
int Person::getAge()
{
return this->age;
}
Main.cpp的
#include <string>
#include "Person.h"
int main() {
printFunc();
Person chelsea_manning(5, std::string("Chelsea Manning"));
}
而且,我是新的C++,所以请让我知道如果你看到我的代码/ OOP有什么奇怪的地方。
包含就像复制粘贴。看看将这些标题按顺序复制粘贴到你的cpp中就行了。 – chris
尝试从main.cpp和person.cpp中删除'#include',看看会发生什么;)。 –
mfontanini
尝试颠倒包含的顺序 – tay10r