获取头文件以使用 .cpp 文件
Getting header files to work with .cpp files
我正在尝试学习如何将 .h 文件与我的 .cpp 文件一起使用,并且任何时候我 运行 我的代码都会遇到我拥有的几乎每个变量的错误。
这是我的 .h 文件:
class Person
{
public:
Person(
string firstNames,
string lastNames,
string socialSecurityNumber,
string gender)
: firstNames(firstNames),
lastNames(lastNames),
socialSecurityNumber(socialSecurityNumber)
{}
string getFirstNames() const;
void setFirstNames(string aFirstNames);
string getLastNames() const;
void setLastNames(string aLastNames);
...
private:
string firstNames;
string lastNames;
string socialSecurityNumber;
string gender;
};
我的 .cpp 文件:
#include "stdafx.h"
#include <ctime>
#include <string>
#include "Person.h"
using namespace std;
string Person::getFirstNames() const
{
return firstNames;
}
void Person::setFirstNames(string aFirstNames)
{
firstNames = aFirstNames;
}
string Person::getLastNames() const
{
return lastNames;
}
您可以在我的构造函数中看到的其他变量的函数继续。每当我尝试构建它时,它都会给我错误,例如:
'getFirstNames' is not a member of 'Person'
'getFirstNames': modifiers not allowed on nonmember functions
'firstNames' undeclared identifier
我刚开始学习 C++ 和使用头文件,但我有 Java 背景,不知道为什么会出现这些错误。从我在网上完成的研究来看,这应该有效,但显然没有。任何帮助都会很棒,谢谢。
经 OP 确认(见问题评论):
.h 文件缺少以下行:
using namespace std;
header 使用 string
,它是 std
命名空间的一部分。
或者,将所有 string
实例替换为 std::string
(因此,不需要 using namespace std
行)。
Person
的定义在 using namespace std
之前,因此 header 中的 string
的使用无效。
header 文件没有必须首先包含的依赖项是一种很好的做法,为了确保发生这种情况,我总是在库或系统 [=31] 之前包含我的本地 headers =]s:
#include "Person.h"
#include <ctime>
#include <string>
在您的 header 中,确保它包含所需的内容:
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>
class Person
{
public:
Person(
std::string firstNames,
std::string lastNames,
std::string socialSecurityNumber,
std::string gender)
: firstNames(firstNames),
lastNames(lastNames),
socialSecurityNumber(socialSecurityNumber)
{}
...
};
#endif
不要 在 header 文件中 using namespace
- 这会将符号带入全局命名空间并且被认为对用户无礼header 文件。
(顺便说一句,在 i18n 问题上 - 小心假设每个人都有 "first name" 和 "last name",并且其中哪一个(如果有的话)是 "family name" ...)
我正在尝试学习如何将 .h 文件与我的 .cpp 文件一起使用,并且任何时候我 运行 我的代码都会遇到我拥有的几乎每个变量的错误。
这是我的 .h 文件:
class Person
{
public:
Person(
string firstNames,
string lastNames,
string socialSecurityNumber,
string gender)
: firstNames(firstNames),
lastNames(lastNames),
socialSecurityNumber(socialSecurityNumber)
{}
string getFirstNames() const;
void setFirstNames(string aFirstNames);
string getLastNames() const;
void setLastNames(string aLastNames);
...
private:
string firstNames;
string lastNames;
string socialSecurityNumber;
string gender;
};
我的 .cpp 文件:
#include "stdafx.h"
#include <ctime>
#include <string>
#include "Person.h"
using namespace std;
string Person::getFirstNames() const
{
return firstNames;
}
void Person::setFirstNames(string aFirstNames)
{
firstNames = aFirstNames;
}
string Person::getLastNames() const
{
return lastNames;
}
您可以在我的构造函数中看到的其他变量的函数继续。每当我尝试构建它时,它都会给我错误,例如:
'getFirstNames' is not a member of 'Person'
'getFirstNames': modifiers not allowed on nonmember functions
'firstNames' undeclared identifier
我刚开始学习 C++ 和使用头文件,但我有 Java 背景,不知道为什么会出现这些错误。从我在网上完成的研究来看,这应该有效,但显然没有。任何帮助都会很棒,谢谢。
经 OP 确认(见问题评论):
.h 文件缺少以下行:
using namespace std;
header 使用 string
,它是 std
命名空间的一部分。
或者,将所有 string
实例替换为 std::string
(因此,不需要 using namespace std
行)。
Person
的定义在 using namespace std
之前,因此 header 中的 string
的使用无效。
header 文件没有必须首先包含的依赖项是一种很好的做法,为了确保发生这种情况,我总是在库或系统 [=31] 之前包含我的本地 headers =]s:
#include "Person.h"
#include <ctime>
#include <string>
在您的 header 中,确保它包含所需的内容:
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>
class Person
{
public:
Person(
std::string firstNames,
std::string lastNames,
std::string socialSecurityNumber,
std::string gender)
: firstNames(firstNames),
lastNames(lastNames),
socialSecurityNumber(socialSecurityNumber)
{}
...
};
#endif
不要 在 header 文件中 using namespace
- 这会将符号带入全局命名空间并且被认为对用户无礼header 文件。
(顺便说一句,在 i18n 问题上 - 小心假设每个人都有 "first name" 和 "last name",并且其中哪一个(如果有的话)是 "family name" ...)