2017-03-07 363 views
-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 
+1

代碼中有許多錯誤。而不是'char'數組使用'std :: string'。 –

+1

關於你們衆多的bug,其中之一就是'Address'默認構造函數不會初始化成員變量。相反,它定義了它自己的* local *變量。 –

+1

至於你的問題,請將完整的錯誤(包括任何可能的信息註釋)複製粘貼到問題*中作爲文本*。然後添加例如對發生錯誤的行進行評論。最後[閱讀有關逗號運算符](http://en.cppreference.com/w/c/language/operator_other#Comma_o​​perator)。 –

回答

-1

的全部重新檢查,如果你要包括在Person類正確的頭文件,我想首先你必須包括「Date.h」可能不是正確的文件,您應該再次檢查它。 然後在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) 
    { 

    theAddress = ("00","000","00","00"); 
    cout << "\n The default constructor was called" << endl; 
    } 

你不能調用構造函數,再重新申報的屬性。 您應該使用構造函數初始化程序列表來調用Address類所需的構造函數。