2015-10-23 58 views
1

我是一名业余C++编码人员,我开始使用类和对象。我想创建一个小小的“程序”,将人的生日和姓名显示出来。我创建了一个程序,您只需输入生日的日期,年份和月份,并显示它的名称&。我一直在People.h和People.cpp中发现错误: “Member declaration not found” error,and candidate are:std :: People :: People(const std :: People &)People.h原型 '的std ::人::人()' 不匹配任何类'的std ::人民People.cpp未找到C++成员声明

我包括Birthday.h和Birthday.cpp在两个图像在底部如果你需要这些。对不起,我的杂乱格式,这是我的第二篇文章,我试图让事情可读,但我有点失败。 :P

My Main.cpp is: 

#include "Birthday.h" 
#include "People.h" 
#include <iostream> 
using namespace std; 

int main() { 

    Birthday birthObj(4,16,2002); 

    People ethanShapiro("Ethan Shapiro", birthObj); 

    return 0; 
} 

People.h is: 

    #ifndef PEOPLE_H_ 
#define PEOPLE_H_ 
#include <iostream> 
#include "Birthday.h" 
#include <string> 

namespace std { 

class People { 
    public: 
     People(string x, Birthday bo); 
     void printInfo(); 
    private: 
     string name; 
     Birthday dateOfBirth; 
}; 

} 

#endif 

People.cpp is: 

    #include "People.h" 

namespace std { 

People::People(): name(x), dateOfBirth(bo) { 
} 

void People::printInfo(){ 
    cout << name << " is born in"; 
    dateOfBirth.printDate(); 
} 

} 

Birthday.h Birthday.cpp

+1

你可能不应该把自己的班'命名空间std'。这不是问题所在,但它违背了命名空间的目的(使用自己的命名空间而不是非法侵入C++标准的命名空间)。 – skyking

+0

你是什么意思(在C++标准的命名空间中使用我自己的命名空间而不是trspass?是否意味着使用名称空间peo **来创建一个新的命名空间,如**来放人? –

回答

2

People唯一的构造函数声明为:

People(string x, Birthday bo); 

,并要定义构造函数:

People::People(): name(x), dateOfBirth(bo) { 
} 

定义并不符合任何d eclaration。

您需要使用:

People::People(string x, Birthday bo): name(x), dateOfBirth(bo) { 
} 
+0

非常感谢:)。我只有一个问题,为什么我需要将字符串和对象声明为成员(或将冒号放入父项之后)。 –

+0

你是否在构造函数定义中的':'之后使用'name(x),dateOfBirth(op)'? –

+1

@EthanShapiro'People :: People(string x,Birthday bo)'和':name(x),dateOfBirth(bo)'都是为了不同的目的。前者指定将使用参数'string'和'Birthday'调用构造函数,而后者则仅使用构造函数中传递的参数初始化类'name'和'dateOfBirth'类的成员。这样做的等效方法是:'People :: People(string x,Birthday bo){name = x; dateOfBirth = bo; }'。 –