C++类文件问题的分离
我正在做另一个学校项目,在这个项目中,我应该为我的班级使用多个文件。。。也就是说,whatever.h和whatever.cpp。我完全了解如何使用它们,但由于某种原因,当我开始使用它们构建代码时,我会遇到大量错误。我将发布Person.h和Person.cpp中的代码,然后发布我得到的错误 第h人: Person.cpp:C++类文件问题的分离,c++,visual-studio-2015,C++,Visual Studio 2015,我正在做另一个学校项目,在这个项目中,我应该为我的班级使用多个文件。。。也就是说,whatever.h和whatever.cpp。我完全了解如何使用它们,但由于某种原因,当我开始使用它们构建代码时,我会遇到大量错误。我将发布Person.h和Person.cpp中的代码,然后发布我得到的错误 第h人: Person.cpp: 我不知道该怎么办,因为我已经试过多次了,我已经查找了那些具体的错误,我无法找出我的程序出了什么问题 如评论中所述,您应该做两件事: 尽可能将所需的头文件直接包含到您自己的头
我不知道该怎么办,因为我已经试过多次了,我已经查找了那些具体的错误,我无法找出我的程序出了什么问题 如评论中所述,您应该做两件事: 尽可能将所需的头文件直接包含到您自己的头文件中,以使其独立工作,而不依赖于包含顺序。除非包含,否则不会定义字符串。 对头文件中的类型使用完全限定名:字符串在全局命名空间中不存在,因为std::string驻留在命名空间std中,所以应该通过std::string引用它。 最后,在用户定义的头之前包含官方库头通常是一个好主意,因此在cpp文件中应该在Person.h之前包含string和vector 这些更改意味着对于您的特定情况,年份标题变为: 人
#pragma once
#include <string>
class Person
{
public:
Person();
~Person();
void setGender(std::string Gen);
std::string getGender();
private:
int Age;
std::string Gender;
int AnnualIncome;
};
您的cpp文件将变为:
Person.cpp
#include <iostream>
#include <string>
#include <vector> //I'll be using this later.
#include <fstream> //I'll be using this later.
#include "Person.h"
using namespace std;
Person::Person()
{
}
Person::~Person()
{
}
void Person::setGender(string Gen)
{
Gender = Gen;
}
string Person::getGender()
{
return Gender;
}
最后,有一点需要注意:使用名称空间std通常不是一个好主意,因为这会导入所有内容,而且通常最好选择您需要的内容,例如使用std::string;,使用std::vector;,等等。将include和其他std include以及使用命名空间std添加到您忘记包含在头文件中的头文件中,并使用std::。始终首先修复第一个错误,因为其他错误通常会从中层叠而来。@Alex:不,不要使用命名空间std将其添加到头中。那是一种可怕的做法@AlexStepanov您几乎不应该使用名称空间std;在头文件中。@Cameron是的,我知道,但是在问题的上下文中我并不重要。我同意你所说的一切,除了关于包含顺序的部分。在任何其他头文件之前包含您自己的头文件是有利的。这样,person.cpp文件确保person.h是幂等的,也就是说,它自己编译时没有任何错误,并且除了包含自身的头依赖项之外,没有其他头依赖项。我遵照您的建议,将代码更改为您发布的代码。不幸的是,这并没有解决所有的错误。我已经减到四个了,但我不知道如何修复它们。错误C2597对非静态成员person的非法引用。cpp 21错误C2061语法错误:标识符“string”person。H10错误C2061语法错误:标识符“string”person。H10错误C2511“void person::setGenderstd::string”:在“person”person.cpp中未找到重载成员函数20@Danny我错过了一个字符串到setGender中标题中的std::string。现在试试看,如果你仍然有错误,可能会先发布。我在编辑上面的评论时尝试设置格式。。。这没用,但他们现在在那里。我确实试过了,它从6个错误中删除了。@Danny我的答案中当前的代码为我编译,没有错误。
Error C2061 syntax error: identifier 'string' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 7 Build
Error C2061 syntax error: identifier 'string' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 7 Build
Error C3646 'getGender': unknown override specifier Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 8 Build
Error C2059 syntax error: '(' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 8 Build
Error C2238 unexpected token(s) preceding ';' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 8 Build
Error C3646 'getGender': unknown override specifier Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 8 Build
Error C2059 syntax error: '(' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 8 Build
Error C2238 unexpected token(s) preceding ';' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 8 Build
Error C3646 'Gender': unknown override specifier Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 11 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 11 Build
Error C3646 'Gender': unknown override specifier Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 11 Build
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h 11 Build
Error C2511 'void Person::setGender(std::string)': overloaded member function not found in 'Person' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.cpp 19 Build
Error C2065 'Gender': undeclared identifier Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.cpp 20 Build
Error C2039 'getGender': is not a member of 'Person' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.cpp 23 Build
Error C2065 'Gender': undeclared identifier Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\person.cpp 26 Build
Error C2660 'Person::setGender': function does not take 1 arguments Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\source.cpp 32 Build
Error C2039 'getGender': is not a member of 'Person' Marketing Software c:\users\danny\documents\visual studio 2015\projects\marketing software\source.cpp 33 Build
#pragma once
#include <string>
class Person
{
public:
Person();
~Person();
void setGender(std::string Gen);
std::string getGender();
private:
int Age;
std::string Gender;
int AnnualIncome;
};
#include <iostream>
#include <string>
#include <vector> //I'll be using this later.
#include <fstream> //I'll be using this later.
#include "Person.h"
using namespace std;
Person::Person()
{
}
Person::~Person()
{
}
void Person::setGender(string Gen)
{
Gender = Gen;
}
string Person::getGender()
{
return Gender;
}