-2
我不断收到一个错误说:组成初始化错误
初始化无法从“为const char *”到“地址”
我试图让我的Person
类使用Address
作为转换构造函数中的一个参数。我将Address
头文件包含在Person
头文件中,所以我不知道我在做什么错误。除了调用默认构造函数Person myPerson
之外,我的.cpp
文件中也没有任何内容。
Address
头文件:
#ifndef ADDRESSMODEL
#define ADDRESSMODEL
#define ADDRESSDEBUG
#include <iostream>
#include <string.h>
using namespace std;
class Address {
public:
Address(void);
Address(char* aNumber,
char* aStreetName,
char* aTownName,
char* aCounty);
~Address();
void setAddress(char* aNumber,
char* aStreetName,
char* aTownName,
char* aCounty);
char* getNumber(void);
char* getStreetName(void);
char* getTownName(void);
char* getCounty(void);
protected:
private:
char theNumber[4];
char theStreetName[20];
char theTownName[20];
char theCounty[20];
};
inline Address::Address(void) {
char theNumber[] = "0";
char theStreetName[] = "0";
char theTownName[] = "0";
char theCounty[] = "0";
cout << "\n Default constructor was called" << endl;
}
inline Address::Address(char* aNumber,
char* aStreetName,
char* aTownName,
char* aCounty) {
strcpy(theNumber, aNumber);
strcpy(theStreetName, aStreetName);
strcpy(theTownName, aTownName);
strcpy(theCounty, aCounty);
cout << "\n Regular constructor was called" << endl;
}
inline Address::~Address() {
cout << "\n Deconstructor was called" << endl;
}
#endif // ifndef ADDRESSMODEL
我Person
头:
#include "Date.h"
#include <iostream>
#include <string.h>
using namespace std;
class Person {
public:
Person(void);
// Person(Address anAddress);
protected:
private:
// Name theName;
// Date theDate;
Address theAddress;
};
inline Person::Person(void) {
Address theAddress = ("00", "000", "00", "00");
cout << "\n The default constructor was called" << endl;
}
// inline Person :: Person(Address anAddress) {
// cout << "\n The regular constructor was called" << endl;
// }
#endif
代码中有许多错误。而不是'char'数组使用'std :: string'。 –
关于你们众多的bug,其中之一就是'Address'默认构造函数不会初始化成员变量。相反,它定义了它自己的* local *变量。 –
至于你的问题,请将完整的错误(包括任何可能的信息注释)复制粘贴到问题*中作为文本*。然后添加例如对发生错误的行进行评论。最后[阅读有关逗号运算符](http://en.cppreference.com/w/c/language/operator_other#Comma_operator)。 –