Constructor error . Whats going on ?

I learnt about constructors today I am a noob, so I ran into this problem
I declared a class called Cat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Cat{
    private :
        string m_name;
        string m_owner;
        int m_price;
        string m_color;
        double m_weight;
        void sellCat(string whoBought,int price);
    public :
        Cat(); //this is the default constructor notice no arguments
        Cat(const string name , const string color, int price, double weight, const string owner);
        void changeColor(string newColor);
        void setName(string name);
        void showCat();
};


Everything works fine with the default constructor. Heres my definition for the constructor

1
2
3
4
5
6
7
8
Cat::Cat(const string name , const string color, int price = 0 , double weight = 0, const string owner = "NotSoldYet"){
    m_name = name;
    m_owner = owner;
    m_color = color;
    m_price = price ;
    m_weight = weight;
}


Whenever I try to create a new object in the main function like this,

Cat newCat("Garfield,"Green");

I get this error

error: no matching function for call to 'Cat::Cat(const char [8], const char [3])'|

I tried removing const but still get the same error
Last edited on
Cat newCat("Garfield","Green");

This function call (I fixed the missing ") has two parameters. Neither of them are string objects. The constructor wants string objects, and your compiler appears not to know how to turn these two const char arrays into strings.

What compiler are you using? Is it very old?
Last edited on
Because you haven't declared a custom costructor with two string arguments thus:

 
Cat(const string, const string);


But I suspect that you intended to do the following:

 
Cat(const string name, const string color, int price = 0, double weight = 0, const string owner = "NotSoldYet");


then in the source file you would do:

1
2
3
4
Cat::Cat(const string name, const string color, int price /*=0*/, double weight /*=0*/, const string owner /*="NotSoldYet"*/)
{
   ...
}
Last edited on
@Moshchops That was a typo I made while posting . I am using GNU gcc compiler it's not old I guess.

@ajh32
My intention was to if the price , weight and owner are not provided they are set to some default values why doesn't it work that way ? Any other way to do that ? Also I created a new object something like this .
Cat newCat("Garfied","df",23,23.23,"Myself");
By providing values for all the arguments and it works. But shouldn't it work for if I just provided some arguments and rest to the default ?
You set the argument default values in the function prototype declaration, not in the source code. See my comment above for the correct way to do default argument values, i.e. what you intended.
The following works fine in a recent compiler.

http://ideone.com/TsyUwt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <string>
#include <iostream>

using namespace std;


class Cat{
    private :
        string m_name;
        string m_owner;
        int m_price;
        string m_color;
        double m_weight;
        void sellCat(string whoBought,int price);
    public :
        Cat(); //this is the default constructor notice no arguments
        Cat(const string name , const string color, int price, double weight, const string owner);
        void changeColor(string newColor);
        void setName(string name);
        void showCat();
};


Cat::Cat(const string name , const string color, int price = 0 , double weight = 0, const string owner = "NotSoldYet"){
    m_name = name;
    m_owner = owner;
    m_color = color;
    m_price = price ;
    m_weight = weight;
}

void Cat::showCat()
{
  cout << "Name is " << m_name << endl << "Owner is " << m_owner;
  
}



int main()
{
  Cat newCat("Garfield","Green"); 
  newCat.showCat();
  
}

Last edited on
@ajh32
Thank you so much, It worked . How come I didn't think of it ? I am so DUMB
thanks again
I am using GNU gcc compiler it's not old I guess.


The first version dates from 1987. That's a long time ago. How old is the one you're using?

Edit: Did you change the code in the original post? Please don't, it renders the entire thread nonsensical.
Last edited on
I don't know, it just says GNU GCC compiler
@Moschops


I am using GNU gcc compiler it's not old I guess.



The first version dates from 1987. That's a long time ago. How old is the one you're using?

Edit: Did you change the code in the original post? Please don't, it renders the entire thread nonsensical.


He didn't you just don't understand how c++ works for default arguments. The default argument value MUST be declared in the function prototype i.e. header and NOT NOT in the source. See my code in the comment above.
Last edited on
See my code in the comment above.
Likewise. The code I posted compiles and runs fine. The default arguments are not in the prototype.

The default argument value MUST be declared in the function prototype i.e. header and NOT NOT in the source.
That is NOT NOT NOT true. Putting them in the definition and not the declaration is a way to ensure that only those who can see the definition use the defaults. In this case above, the code can see the definition, so it's fine. Perhaps the OP had his code strewn across multiple files.
Last edited on
Topic archived. No new replies allowed.