Confusing compiler error

No matching function for call to...

Page 1 of 1

12 Replies - 963 Views - Last Post: 14 February 2009 - 08:10 PM Rate Topic: -----

#1 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Confusing compiler error

Posted 14 February 2009 - 05:11 PM

I'm getting a compiler error of "No matching function for call to 'PhoneNumber::PhoneNumber()'", then it will give me candidates to try.

I've gotten that message before, but if you look at my code it doesn't have a function called PhoneNumber::PhoneNumber() with no arguments. And when I click on the error (I'm using Dev C++), it takes me to a line below my "Record::Record(int, PhoneNumber)" definition.

I have no clue what to do.

#include <iostream.h>
#include <stdlib.h>
#include <vector>

using namespace std;

class PhoneNumber
{
private:
		int areaCode;
		int number;
public:
	   PhoneNumber(int aC, int n);
	   int getAreaCode();
	   int getNumber();
	   void setAreaCode(int area);
	   void setNumber(int num);
};


class Record
{
private:
	   int id;
	   PhoneNumber phoneNumber;
public:
	   Record(int i, PhoneNumber pN);
	   int getId();
	   PhoneNumber getPhoneNumber();
	   void setID(int iD);
	   void setPhoneNumber(PhoneNumber pN);
};


class PhoneBook
{
private:
vector <Record> records; //vector
public:
void add(Record pN);
bool search(PhoneNumber pN);
void list();
PhoneBook();//maybe size as argument
};



int main()
{
	  //Phonebook myFriends
	  //myFriends.PhoneBook();
	  //add (exists already?)
	  //delete (exist or no)
	  //list (print out)
	  //update phone number


	  system("PAUSE");
	  return 0;
}

PhoneBook::PhoneBook()//maybe int size
{
//create vector
}//end phonebook

void PhoneBook::add(Record pN)
{


}//end add
bool PhoneBook::search(PhoneNumber pN)
{
}//end search
void PhoneBook::list()
{
}//end list



PhoneNumber::PhoneNumber(int aC, int n)
{
}//end phonenumber 
int PhoneNumber::getAreaCode()
{
}//end getAC
int PhoneNumber::getNumber()
{
}//end GN
void PhoneNumber::setAreaCode(int area)
{
}//end setAC
void PhoneNumber::setNumber(int num)
{
}//end setN
Record::Record(int i, PhoneNumber pN)
{
}//end record
int Record::getId()
{
}//end getID
PhoneNumber Record::getPhoneNumber()
{
}//end getPN
void Record::setID(int iD)
{
}//end setID
void Record::setPhoneNumber(PhoneNumber pN)
{
}//end setPN



Is This A Good Question/Topic? 0
  • +

Replies To: Confusing compiler error

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Confusing compiler error

Posted 14 February 2009 - 05:26 PM

I think the compiler is looking for the default constuctor for the PhoneNumber class. It expects a function with the same name as the class and with zero arguments. You can add the following line of code to your PhoneNumber class if it helps:

PhoneNumber(); // Class constructor (public)


You already seem to have a definition for the constructor on line 62 of your code.
Was This Post Helpful? 0
  • +
  • -

#3 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Confusing compiler error

Posted 14 February 2009 - 05:53 PM

View Postbsaunders, on 14 Feb, 2009 - 04:26 PM, said:

I think the compiler is looking for the default constuctor for the PhoneNumber class. It expects a function with the same name as the class and with zero arguments. You can add the following line of code to your PhoneNumber class if it helps:

PhoneNumber(); // Class constructor (public)


You already seem to have a definition for the constructor on line 62 of your code.


PhoneNumber(int aC, int num); is supposed to be the constructor for that class. Is there supposed to be a default constructor in ADDITION to the one I already have in there? Also, I have constructors declared for the record and phonebook class with no issue.
Was This Post Helpful? 0
  • +
  • -

#4 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Confusing compiler error

Posted 14 February 2009 - 06:00 PM

In the code you have on your computer right now, are all the statements in main commented out.
Was This Post Helpful? 0
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Confusing compiler error

Posted 14 February 2009 - 06:08 PM

When an instance of a class is created, the class constructor is called to initialize the object. Some class constructors take arguments, e.g., the Record and PhoneNumber classes in your project. When you create an instance of a class whose constructor expects arguments, you must specify the arguments.

In the Record class, you create an instance of the PhoneNumber class:
private:
	   int id;
	   PhoneNumber phoneNumber;


The PhoneNumber class's constructor expects arguments,
PhoneNumber(int aC, int n);


and since they weren't provided in that instantiation, it will call the default constructor (which takes zero arguments) instead. The thing is, your class declaration specifies no default constructor. That leaves the compiler wondering, "Where the hell is the default constructor?"

No matching function for call to 'PhoneNumber::PhoneNumber()

This post has been edited by bsaunders: 14 February 2009 - 06:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Confusing compiler error

Posted 14 February 2009 - 06:10 PM

You have this in your code:

PhoneBook::PhoneBook()//maybe int size
{
//create vector
}//end phonebook





But no corresponding empty constructor in your class definition. So either:

1. Add Phonebook() (as bsaunders pointed out)
2. Remove the above code snippet.


The compiler is looking for the generic constructor; since you declared an alternate one, the default is no longer automatically supplied.

This should work:
#include <iostream.h>
#include <stdlib.h>
#include <vector>

using namespace std;

class PhoneNumber
{
private:
		int areaCode;
		int number;
public:
	   PhoneNumber();
	   PhoneNumber(int aC, int n);
	   int getAreaCode();
	   int getNumber();
	   void setAreaCode(int area);
	   void setNumber(int num);
};


class Record
{
private:
	   int id;
	   PhoneNumber phoneNumber;
public:
	   Record(int i, PhoneNumber pN);
	   int getId();
	   PhoneNumber getPhoneNumber();
	   void setID(int iD);
	   void setPhoneNumber(PhoneNumber pN);
};


class PhoneBook
{
private:
vector <Record> records; //vector
public:
void add(Record pN);
bool search(PhoneNumber pN);
void list();
PhoneBook();//maybe size as argument
};



int main()
{
	  //Phonebook myFriends
	  //myFriends.PhoneBook();
	  //add (exists already?)
	  //delete (exist or no)
	  //list (print out)
	  //update phone number


	  system("PAUSE");
	  return 0;
}

PhoneBook::PhoneBook()//maybe int size
{
//create vector
}//end phonebook

void PhoneBook::add(Record pN)
{


}//end add
bool PhoneBook::search(PhoneNumber pN)
{
}//end search
void PhoneBook::list()
{
}//end list



PhoneNumber::PhoneNumber(int aC, int n)
{
}//end phonenumber
int PhoneNumber::getAreaCode()
{
}//end getAC
int PhoneNumber::getNumber()
{
}//end GN
void PhoneNumber::setAreaCode(int area)
{
}//end setAC
void PhoneNumber::setNumber(int num)
{
}//end setN
Record::Record(int i, PhoneNumber pN)
{
}//end record
int Record::getId()
{
}//end getID
PhoneNumber Record::getPhoneNumber()
{
}//end getPN
void Record::setID(int iD)
{
}//end setID
void Record::setPhoneNumber(PhoneNumber pN)
{
}//end setPN


Was This Post Helpful? 0
  • +
  • -

#7 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Confusing compiler error

Posted 14 February 2009 - 06:24 PM

I'm a little confused by your answer.

For the record class I have the constructor

Record (int i, PhoneNumber pN);



With private variables

int id;
PhoneNumber phoneNumber;



For the phonenumber class I have the "constructor"

PhoneNumber(int aC, int n);



with private variables

int areaCode;
int number;



Yet, one gives me the error and the other doesn't. You talk about specifying objects. I don't exactly understand what you mean by that. My confusion is that the situation with the record class and phonenumber class seem the same to me. Perhaps you could explain what the difference is.

Quote

You have this in your code:


PhoneBook::PhoneBook()//maybe int size
{
//create vector
}//end phonebook





But no corresponding empty constructor in your class definition. So either:

1. Add Phonebook() (as bsaunders pointed out)
2. Remove the above code snippet.


I think you are mistaken. I do have PhoneBook() defined in my PhoneBook class. Its the last member of the class.

You guys have been a big help. I understand what the problem is, I just don't understand why is all (and I would like to if you could explain it).

View Postbsaunders, on 14 Feb, 2009 - 05:00 PM, said:

In the code you have on your computer right now, are all the statements in main commented out.


This is intentional. I'm just trying to get the class format right.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Confusing compiler error

Posted 14 February 2009 - 06:32 PM

There is no default constructor in your PhoneNumber class:

class PhoneNumber
{
private:
		int areaCode;
		int number;
public:
	   PhoneNumber(int aC, int n);
	   int getAreaCode();
	   int getNumber();
	   void setAreaCode(int area);
	   void setNumber(int num);
};




you have a PhoneNumber class as a private member of record here:

class Record
{
private:
	   int id;
	   PhoneNumber phoneNumber;
public:
	   Record(int i, PhoneNumber pN);
	   int getId();
	   PhoneNumber getPhoneNumber();
	   void setID(int iD);
	   void setPhoneNumber(PhoneNumber pN);
};




That just says its there, you would create the object using a constructor in Record's constructor. It's not giving an error because its correct. So in Record's constructor you need to convert the PhoneNumber parameter to the private phoneNumber variable:

Record::Record(int i, PhoneNumber pN)
{
	  phoneNumber = pN; //probably will need a copy constructor, overload = as well, etc...
}//end record


Was This Post Helpful? 1
  • +
  • -

#9 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Confusing compiler error

Posted 14 February 2009 - 06:44 PM

View PostKYA, on 14 Feb, 2009 - 05:32 PM, said:

There is no default constructor in your PhoneNumber class:

class PhoneNumber
{
private:
		int areaCode;
		int number;
public:
	   PhoneNumber(int aC, int n);
	   int getAreaCode();
	   int getNumber();
	   void setAreaCode(int area);
	   void setNumber(int num);
};




you have a PhoneNumber class as a private member of record here:

class Record
{
private:
	   int id;
	   PhoneNumber phoneNumber;
public:
	   Record(int i, PhoneNumber pN);
	   int getId();
	   PhoneNumber getPhoneNumber();
	   void setID(int iD);
	   void setPhoneNumber(PhoneNumber pN);
};




That just says its there, you would create the object using a constructor in Record's constructor. It's not giving an error because its correct. So in Record's constructor you need to convert the PhoneNumber parameter to the private phoneNumber variable:

Record::Record(int i, PhoneNumber pN)
{
	  phoneNumber = pN; //probably will need a copy constructor, overload = as well, etc...
}//end record




OOOOOOOOOOOHHHHHHHHHHHHHHH!!!!! It finally clicked.

That leads to another question then. Could I correct it by changing all instances of PhoneNumber pN to PhoneNumber(int i, int n)?

For example I would change Record::Record(int i, PhoneNumber pN) to:

Record::Record(int i, PhoneNumber(int i, int n))



Not completely sure how that works.
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Confusing compiler error

Posted 14 February 2009 - 06:54 PM

That's overkill :) in addition to being a syntax/compilation error.

Get rid of:

PhoneBook::PhoneBook()//maybe int size
{
//create vector
}//end phonebook




This is causing your errors, assuming everything else is shipshape it should compile.


Record::Record takes a parameter of an int and a PhoneNumber. As far as Record is concerned PhoneNumber is simply some data, the creation of such an object is handled else where:

Record::Record(int i, PhoneNumber pN)
{
   //This is fine "as is" 
}




Somewhere in your main, when you create a PhoneNumber:

int main()
{
   PhoneNumber myHouse(267, 1234567);
  Record(2, myHouse); //etc...
}




edit you wouldn't do:
//BAD--Don't do this!!
int main()
{
   PhoneNumber myHouse(267, 1234567);
  Record(2, myHouse(267, 1234567)); //BAD!
}


This post has been edited by KYA: 14 February 2009 - 06:55 PM

Was This Post Helpful? 1
  • +
  • -

#11 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Confusing compiler error

Posted 14 February 2009 - 07:27 PM

View PostKYA, on 14 Feb, 2009 - 05:54 PM, said:

That's overkill :) in addition to being a syntax/compilation error.

Get rid of:

PhoneBook::PhoneBook()//maybe int size
{
//create vector
}//end phonebook




This is causing your errors, assuming everything else is shipshape it should compile.


This isn't causing the "no function for call to 'PhoneNumber::PhoneNumber" error. I commented them out and recompiled and I still get the same errors.

I DID however put "PhoneNumber();" in the PhoneNumber class and that fixed the problem. However, I'm worried that when I want to initialize the phone number, for some reason that constructor being there will override the one I intend on using... which is why I asked if I could solve the issue the other way.

Quote

Record::Record takes a parameter of an int and a PhoneNumber. As far as Record is concerned PhoneNumber is simply some data, the creation of such an object is handled else where:

Record::Record(int i, PhoneNumber pN)
{
   //This is fine "as is" 
}




Somewhere in your main, when you create a PhoneNumber:

int main()
{
   PhoneNumber myHouse(267, 1234567);
  Record(2, myHouse); //etc...
}



Great explanation. But does said code HAVE to be in the main? For example, could you create a PhoneNumber object inside of add function? Or do I need to do all that in the main function before?

Also... wouldn't your code technically be incorrect? Don't you need to create a Record object like you did a PhoneNumber object?

And one last thing, thank you guys for sticking with me. I know I have a lot of questions and sometimes it doesn't get through the first time, but it has been a big help. In the class he just kind of rushed through the slides and its this 60+ person class so sometimes its hard to ask these types of questions.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,541
  • Joined: 23-August 08

Re: Confusing compiler error

Posted 14 February 2009 - 07:41 PM

What I would do is inside main, you create a PhoneBook object.

PhoneBook pb;


Now you need to add a Record to your phonebook. Your PhoneBook::add method takes a Record as an argument, so you need to create a Record object. But you've made the Record require a PhoneNumber object, so you'll have to create that first.

PhoneNumber pn(212, 5551212);


And now you'll create the Record with that PhoneNumber

Record r(1, pn);


And add it to the PhoneBook

pb.add( r );

This post has been edited by JackOfAllTrades: 14 February 2009 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#13 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Confusing compiler error

Posted 14 February 2009 - 08:10 PM

View PostJackOfAllTrades, on 14 Feb, 2009 - 06:41 PM, said:

What I would do is inside main, you create a PhoneBook object.

PhoneBook pb;


Now you need to add a Record to your phonebook. Your PhoneBook::add method takes a Record as an argument, so you need to create a Record object. But you've made the Record require a PhoneNumber object, so you'll have to create that first.

PhoneNumber pn(212, 5551212);


And now you'll create the Record with that PhoneNumber

Record r(1, pn);


And add it to the PhoneBook

pb.add( r );


Thank you.

Is there any way to do all that jazz within the add function? My profs always would do this kind of code in the main like you are. But is that by convention or is it essentially impossible to do it like that? (I would probably need to change the arguments for the add function around).

And just so I know I'm doing this right, the next steps would be:
-pushback record vector once so that there will be room to add record
-put the "r" record object into the vector records as the last entry (the one that is empty)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1