我有两个类,一个名为Person
,我检查过它(我可以创建该类的对象,所以问题不应该在这里)。
然后我又有了一个名为Family
的课程,其成分来自Person
:
Family.h
#include "Person.h"
class Family
{
public:
Family();
void printFamily();
private:
Person dad_();
Person mum_();
Person son_();
Person daughter_();
};
Family.cpp
#include "Family.h"
Family::Family()
{
}
void printFamily()
{
dad_.printAll();
mum_.printAll();
son_.printAll();
daughter_.printAll();
//printAll() is a function in the Person class that worked when
//I tested it earlier with only the person class
}
但是当我尝试编译时,我收到一个错误:
left of '.printAll' must have class/struct/union
'son_' : undeclared identifier
此错误适用于 family.cpp 中的所有.printAll()
次调用。
我不明白为什么这不起作用,所以我希望你能。
EDIT1:
好的我改变了 void printFamily() 至 void Family :: printFamily()
删除了一个错误,但我仍然得到了
left of '.printAll' must have class/struct/union
EDIT2
啊,我对人称的不好,我把他们改成了
Person dad_;
和其他人一样。
似乎他们的Person类可能是一个错误所以我也会发布
Person.h
#include <string>
using namespace std;
class Person
{
public:
Person( const string & = "000000-0000", const string & = "N", const string & = "",const string & = "N");
~Person();
void setFirstName(const string &);
void setMiddleName(const string &);
void setLastName(const string &);
void getData(string &,string &,string &,string &);
static int getNumberOfPersons();
void printPartially() const;
void printAll() const;
bool checkForSameName(const Person &);
private:
string firstName_;
string middleName_;
string lastName_;
string socialSecNumber_;
static int numberOfPersons_;
};
Person.cpp
#include "Person.h"
#include <iostream>
int Person::numberOfPersons_ = 0;
Person::Person( const string &sNumber, const string &firstName, const string &middleName,const string &lastName )
:firstName_(firstName),middleName_(middleName),lastName_(lastName),socialSecNumber_(sNumber)
{
numberOfPersons_ ++;
}
Person::~Person()
{
numberOfPersons_--;
}
void Person::setFirstName(const string &firstName)
{ firstName_ = firstName; }
void Person::setMiddleName(const string &middleName)
{ middleName_ = middleName; }
void Person::setLastName(const string &lastName)
{lastName_ = lastName;}
void Person::getData(string &fName,string &mName,string &lName,string &sNumber)
{
fName = firstName_;
mName = middleName_;
lName = lastName_;
sNumber = socialSecNumber_;
}
int Person::getNumberOfPersons()
{
return numberOfPersons_;
}
void Person::printPartially() const
{
cout <<"Navn: "<<firstName_<<" "<<middleName_<<" "<<lastName_<<endl;
cout <<"Født: ";
for (int i = 0;i<6;i++)
{
cout <<socialSecNumber_.at(i);
}
}
void Person::printAll() const
{
cout <<"Navn: "<<firstName_<<" "<<middleName_<<" "<<lastName_<<endl;
cout <<"Personnr: "<<socialSecNumber_<<endl;
}
bool Person::checkForSameName(const Person &p)
{
if (p.firstName_ == firstName_ && p.middleName_ ==middleName_ && p.lastName_ == lastName_)
return true;
else
return false;
}
现在我收到了一些新的错误:
error C2011: 'Person' : 'class' type redefinition
see declaration of 'Person'
'Family::dad_' uses undefined class 'Person'
“爸爸”错误适用于整个家庭
答案 0 :(得分:2)
您有一些语法问题。
首先,您将每个应该是成员变量的内容声明为返回Person
的函数。他们应该看起来像(注意,没有parens):
Person dad_;
Person mum_;
Person son_;
Person daughter_;
您也错过了printFamily
的定义范围:
void Family::printFamily() {
...
}
如果没有前面的Family::
,C ++会认为您正在定义一个自由函数,并且不知道在Family
类中查找dad_
的声明,{{1等等。
此外,至少使用您显示的代码,无法初始化您班级中的人员。 mum_
构造函数应该使用参数来定义人员,或者您应该拥有允许稍后定义它们的setter。现在,你将得到4个相同的人,但是设置默认的人构造函数构建它们。
我通常更喜欢构造函数方法,但我对你的代码有其他的设计保留(例如,一个家庭是否总是包含妈妈,爸爸,兄弟,姐妹?)而这不是真正的问题所在。< / p>
答案 1 :(得分:1)
成员函数的行外定义需要包含类名:
void Family::printFamily()
{
//...
令人惊讶的是,你已经为构造函数做了这个,但后来立刻忘了......
其次,您的私有类成员是函数,而不是数据成员(这很奇怪),但如果这是故意的,您需要调用:
dad_().printAll();
// ^^^
答案 2 :(得分:1)
该行:
Person dad_();
说dad_
是一个返回Person
的函数,而不是一个对象。你的意思是?与其他人相似。
尝试
Person dad_;
<强> Family.h 强>
#include "Person.h"
class Family
{
public:
Family();
void printFamily();
private:
Person dad_;
Person mum_;
Person son_;
Person daughter_;
};
<强> Family.cpp 强>
#include "Family.h"
Family::Family()
{
}
void Family::printFamily()
{
dad_.printAll();
mum_.printAll();
son_.printAll();
daughter_.printAll();
//printAll() is a function in the Person class that worked when
//I tested it earlier with only the person class
}