-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)。 –