Question

What is the syntax for dynamically allocating an object with an overloaded constructor in C++?

If I have a class Foo:

class Foo{
public:
    Foo(string str, int nbr);    // Overloaded constructor
};

And a second class Foo2 (using Foo):

#include "Foo"
class Foo2{
public:
    Foo* myFoo;    // Wrong!
    Foo2(){
        myFoo = new Foo(myStr, myNbr);
    }
};

The error displayed is the following :

no matching function for call to "Foo::Foo()"

How can I, when creating myFoo object pointer, specify that it will use the Foo(string str, int nbr) constructor, not Foo() constructor.

Is it possible without using constructor delegation?

Was it helpful?

Solution

Your syntax to construct the object is correct.

It's hard to say for sure since you haven't told the error, but my guess is that your problem is that the constructor is private. That means you cannot use the constructor outside the class.

Edit concerning the error message:

Here's a complete example that compiles. I've added some example lines that would produce the error: no matching function for call to 'Foo::Foo()'.

#include <string>

class Foo{
public:
    Foo(std::string str, int nbr);
};

// empty definition
Foo::Foo(std::string str, int nbr) {}

// delegating constructor (c++11 feature)
// this would produce error: no matching function for call to 'Foo::Foo()'
//Foo::Foo(std::string str, int nbr) : Foo::Foo() {}

int main() {
    Foo* myFoo;
    myFoo = new Foo("testString", -1); // this is correct
    // trying to construct with default constructor
    //Foo myFoo2; // this would produce error: no matching function for call to 'Foo::Foo()'
    //Foo* myFoo3 = new Foo(); // so would this
}

Given the error, your code is trying to use the default constructor somewhere.

Edit2 concerning your new Foo2 example. Your declaration of Foo* and the call to the constructor are still correct and the code should compile if you fix the method visibility and missing semicolons. Following example compiles:

#include <string>

class Foo{
public:
    Foo(std::string str, int nbr);    // Overloaded constructor
};

Foo::Foo(std::string str, int nbr){}

class Foo2{
    Foo* myFoo;    // This is still correct
public:
    Foo2() {
        myFoo = new Foo("", 1);
    }
};

int main() {
    Foo2 foo2;
}

OTHER TIPS

Syntax is correct. There are many possibilities as you have not written full class definition. 1. check whether default cnstsructor is written. 2. Check whether both constructors are inside public section. 3. Alternatively you can change call to constructor as below,

Foo* myFoo = new Foo("testString", -1);

The following code should work.

class Foo
{

string str;
int num;
public:
    Foo(string p_str, int nbr):str(p_str),num(nbr){};    // Overloaded constructor
    Foo() {};    // Default constructor
};

The correct way is initializing the members in each constructor. You could extract their common code in a private init() member function and call it in each constructor like the following:

class Foo {
    public:
        Foo(string x);
        Foo(string x, int y);
        ...
    private:
        void init(string x, int y);
};

Foo::Foo(string x)
{
    init(x, int(x) + 3);
    ...
}

Foo::Foo(string x, int y)
{
    init(x, y);
    ...
}

void Foo::init(string x, int y)
{
    ...
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top