2012-04-13 81 views
1

我在我的代码中面对“模糊调用错误”,我无法检测到它我认为错误是在addressBookType类中,但我将能够检测到它!C++“重载函数调用不明确”编程错误

由于这些错误,我必须重新发布此问题吗? “

Compiler: MinGW GCC 4.6.2 32-bit 
Executing g++.exe... 
g++.exe "D:\New Folder\Cpp\Assignment 4\q4\main.cpp" -o "D:\New Folder\Cpp\Assignment 4\q4\main.exe" -I"E:\Program Files\Dev-Cpp\MinGW32\include" -I"E:\Program Files\Dev-Cpp\MinGW32\include" -L"E:\Program Files\Dev-Cpp\MinGW32\lib" -L"E:\Program Files\Dev-Cpp\MinGW32\lib" -static-libstdc++ -static-libgcc 
D:\New Folder\Cpp\Assignment 4\q4\main.cpp: In constructor 'addressBookType::addressBookType()': 
D:\New Folder\Cpp\Assignment 4\q4\main.cpp:318:34: error: call of overloaded 'addressType()' is ambiguous 

D:\New Folder\Cpp\Assignment 4\q4\main.cpp:318:34: note: candidates are: 
D:\New Folder\Cpp\Assignment 4\q4\main.cpp:104:1: note: addressType::addressType(std::string, std::string, std::string, std::string) 

D:\New Folder\Cpp\Assignment 4\q4\main.cpp:88:3: note: addressType::addressType() 
D:\New Folder\Cpp\Assignment 4\q4\main.cpp:318:34: error: call of overloaded 'extPersonType()' is ambiguous 

D:\New Folder\Cpp\Assignment 4\q4\main.cpp:318:34: note: candidates are: 
D:\New Folder\Cpp\Assignment 4\q4\main.cpp:168:1: note: extPersonType::extPersonType(std::string, std::string) 
D:\New Folder\Cpp\Assignment 4\q4\main.cpp:154:3: note: extPersonType::extPersonType() 

Execution terminated 

这里是我的代码:

#include<iostream> 
#include<cstdlib> 
#include<fstream> 
#include<string> 

using namespace std; 

//////////////////////////////"personType" is from D.S Malik Course Website 
////////////***Class person Start 


class personType 
{ 
public: 
    void print() const; 
     //Function to output the first name and last name 
     //in the form firstName lastName. 


    void setName(string first, string last); 
     //Function to set firstName and lastName according 
     //to the parameters. 
     //Postcondition: firstName = first; lastName = last 

    string getFirstName() const; 
     //Function to return the first name. 
     //Postcondition: The value of firstName is returned. 

    string getLastName() const; 
     //Function to return the last name. 
     //Postcondition: The value of lastName is returned. 

    personType(string first, string last); 
     //Constructor 
     //Sets firstName and lastName according to the parameters. 
     //The default values of the parameters are null strings. 
     //Postcondition: firstName = first; lastName = last 

private: 
    string firstName; //variable to store the first name 
    string lastName; //variable to store the last name 
}; 
/////////Class person End***/////////// 
// IMPLEMENTATION OF "PersonType" CLASS // 

///////////////// IMP START //////////////////// 
personType::personType(string first="",string last=""){ 

} 

void personType::setName(string first,string last){ 
    firstName=first; 
    lastName=last; 

} 

string personType::getFirstName() const{ 

return firstName; 
} 

string personType::getLastName() const{ 
return lastName; 
} 

void personType::print() const{ 
    cout<<firstName<<" "<<lastName<<endl; 
} 
////////////////// IMP END //////////////////// 



///////////////////////////////////////////////////////////////////////////////////////////// 


////////***class addressType Start 

class addressType{ 


    private: 
    string stAddress; 
    string city; 
    string state; 
    string zipcode; 

    public: 
     addressType(); 
     addressType(string,string,string,string); 
     void setAddress(string); 
     string getAddress(); 
     void setCity(string); 
     string getCity(); 
     void setState(string); 
     string getState(); 
     void setZipcode(string); 
     string getZipcode(); 

}; 

// IMPLEMENTATION OF "addressType" CLASS // 

///////////////// IMP START //////////////////// 
addressType::addressType(string=" ",string=" ",string=" ",string=" "){ 

} 

void addressType::setAddress(string addr){ 
    stAddress=addr; 
} 

string addressType::getAddress(){ 
    return stAddress; 
} 

void addressType::setCity(string cit){ 
    city=cit; 
} 

string addressType::getCity(){ 
    return city; 
} 

void addressType::setState(string sta){ 
    state=sta; 
} 

string addressType::getState(){ 
    return state; 
} 

void addressType::setZipcode(string zip){ 
    zipcode=zip; 
} 

string addressType::getZipcode(){ 
    return zipcode; 
} 
///////////////// IMP END //////////////////// 

//////////class addressType End*** 
///////////////////////////////// 


////////////////////////////////// 
//////***class extPersonType Start 
class extPersonType { 

    private: 
     string relation; 
     string phNo; 

    public: 
     extPersonType(); 
     extPersonType(string,string); 
     void setRelation(string); 
     string getRelation(); 
     void setphNo(string); 
     string getphNo(); 


}; 


// IMPLEMENTATION OF "extPersonType" CLASS // 
///////////////// IMP START //////////////////// 

extPersonType::extPersonType(string =" " ,string = " "){ 

} 

void extPersonType::setRelation(string rel){ 
    relation=rel; 
} 

string extPersonType::getRelation(){ 
    return relation; 
} 

void extPersonType::setphNo(string ph){ 
    phNo=ph; 
} 

string extPersonType::getphNo(){ 
    return phNo; 
} 

///////////////// IMP END //////////////////// 
//////////class extPersonType End*** 

/////////////////////////////////////////////////////////////////////////////////////////// 


//////////////////////////////"dateType" is from D.S Malik Course Website 
////////***class DateType Start 

class dateType 
{ 
public: 
    void setDate(int month, int day, int year); 
     //Function to set the date. 
     //The member variables dMonth, dDay, and dYear are set 
     //according to the parameters. 
     //Postcondition: dMonth = month; dDay = day; 
     //    dYear = year 

    int getDay() const; 
     //Function to return the day. 
     //Postcondition: The value of dDay is returned. 

    int getMonth() const; 
     //Function to return the month. 
     //Postcondition: The value of dMonth is returned. 

    int getYear() const; 
     //Function to return the year.  
     //Postcondition: The value of dYear is returned. 

    void printDate() const; 
     //Function to output the date in the form mm-dd-yyyy. 

    dateType(int month = 1, int day = 1, int year = 1900); 
     //Constructor to set the date 
     //The member variables dMonth, dDay, and dYear are set 
     //according to the parameters. 
     //Postcondition: dMonth = month; dDay = day; dYear = year; 
     //    If no values are specified, the default 
     //    values are used to initialize the member 
     //    variables. 

private: 
    int dMonth; //variable to store the month 
    int dDay; //variable to store the day 
    int dYear; //variable to store the year 
}; 
//////////class dateType End*** 
///////////////////////////////// 


// IMPLEMENTATION OF "DateType" CLASS // 
///////////////// IMP START //////////////////// 

void dateType::setDate(int month, int day, int year) 
{ 
    dMonth = month; 
    dDay = day; 
    dYear = year; 
} 

int dateType::getDay() const 
{ 
    return dDay; 
} 

int dateType::getMonth() const 
{ 
    return dMonth; 
} 

int dateType::getYear() const 
{ 
    return dYear; 
} 

void dateType::printDate() const 
{ 
    cout << dMonth << "-" << dDay << "-" << dYear; 
} 

    //Constructor with parameters 
dateType::dateType(int month, int day, int year) 
{ 
    dMonth = month; 
    dDay = day; 
    dYear = year; 
} 
//////////////// IMP END ///////////////////// 

//////////////////////////////////////////////////////////////////////////////// 


//////***class addressBookType Start 

class addressBookType { 

private: 
    string FirstName; //variable to store the first name 
    string LastName; //variable to store the last name 
    string StAddress; 

    string City; 
    string State; 
    string Zipcode; 
    string Relation; 
    string PhNo; 
    int DMonth; //variable to store the month 
    int DDay; //variable to store the day 
    int DYear; //variable to store the year 

protected: 

    addressType obj1; 
    dateType obj2; 
    extPersonType obj3; 

public: 
    addressBookType(); 
    static int count; 
    void loadData(addressBookType *&ptr); 

}; 

//////////class addressType End*** 
///////////////////////////////// 

// IMPLEMENTATION OF "addressBookType" CLASS // 
///////////////// IMP START //////////////////// 
addressBookType::addressBookType(){ 

} 
void addressBookType::loadData(addressBookType *&ptr){ 

    ifstream fin; 
    ifstream fout; 

    string tempName; 
    cout<<"Enter file name:"<<endl; 


      if(!fin){ 

       cout<<"Cannot open the image file : "<<endl; 
       cout<<"Input Failure"<<endl; 
       system("pause"); 


      } 

      else{ 

       for(int i=0;!fin.eof();i++){ 

        fin>>FirstName; 
        fin>>LastName; 
        fin>>DDay; 
        fin>>DMonth; 
        fin>>DYear; 
        getline(fin,StAddress); 
        getline(fin,City); 
        getline(fin,State); 
        fin>>Zipcode; 
        fin>>PhNo; 
        fin>>Relation; 

        cout<<FirstName<<LastName<<DDay<<DMonth<<DYear<<StAddress<<City<<State<<Zipcode<<PhNo<<Relation<<endl; 

       } 

      } 

} 

int main(){ 

    addressBookType *ptr; 
    addressBookType obj; 
    ptr=new addressBookType[500]; 
    obj.loadData(ptr); 

    system("pause"); 
    return(0); 
} 

~Please help 
+0

默认参数应该是在报头中的声明,而不是在执行文件中的定义所述第二函数的第一参数。只要将所有内容全部放在一个文件中(只要定义在使用之前),就不会影响到您,但是您应该记住更复杂的程序。 – bames53 2012-04-13 19:03:25

+1

噢,另一个非_sscce.org_代码示例的完美例子。 – Griwes 2012-04-13 19:20:36

回答

12

这就是问题所在:

addressType(); 
addressType(string,string,string,string); 
... 

addressType::addressType(string=" ",string=" ",string=" ",string=" "){ 

你声明了两个构造函数,但第二个具有所有参数的默认值。所以如果你调用addressType(),它可以是第一个无参数构造函数,或者第二个,所有参数都设置为默认值。

无论如何,你似乎永远不会实现第一个构造函数,简单的修复就是删除声明。

1

的问题是,你有你的addressType类2个冲突的构造函数:

addressType(); 
addressType(string,string,string,string); 

你声明的缺省值的第二个中的定义:

addressType::addressType(string=" ",string=" ",string=" ",string=" "){ 

} 

,你应该将其删除:

addressType::addressType(string,string,string,string){ 

} 
4

你已经声明了一个不带para的构造函数米和一个构造函数,其中所有参数默认为null。当你调用构造函数并且没有传入任何参数时,编译器不知道你是否需要这个参数,或者你是否想要多参数的参数,但是对于所有的参数都要传递null

0

这两个函数原型是无法区分的:

addressType::addressType(); 
addressType::addressType(string=" ",string=" ",string=" ",string=" "); 
当您创建通过默认构造函数 addressType对象( extPersonType相同)

=" "符号意味着如果一个参数没有明确规定,使用=之后的值(也称为默认参数值)。因此,在这种情况下,对addressType()的调用可能是没有参数的函数,或者是其所有参数都设置为" "的其他函数 - 编译器无法确定您的意思并引发错误。

要修正,删除默认值从(至少)