Question

I'm honestly embarassed I have to ask but I'm stuck on that.

#include <iostream>
using namespace std;

class Obj
{
};


class Test
{
private:
  Obj a;

public:
  Test(Obj _a)
    : a(_a) 
  {}
};


int main() {

  Obj ob();
  Test t(ob);

  return 0;

}

I get this error:

t.cpp:24: error: no matching function for call to ‘Test::Test(Obj (&)())’
t.cpp:15: note: candidates are: Test::Test(Obj)
t.cpp:10: note:                 Test::Test(const Test&)

I don't get it. The same snippet works just fine with built in types (integers and stuff).

Was it helpful?

Solution

Obj ob(); declares a ob to be a function taking no parameters and returning Obj.

If you want to default construct an Obj, use Obj ob; or Obj ob{};.

OTHER TIPS

This line

Obj ob();

does not create an object ob. It declares a function that takes nothing as input and returns a Obj.

Change it to:

Obj ob;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top