2017-03-07 183 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类所需的构造函数。