3

老实说,我很尴尬,我不得不问,但我坚持下去。

#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;

}

我收到此错误:

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&)

我不明白。相同的片段适用于内置类型(整数和东西)。

4

2 回答 2

4

Obj ob();将 a声明为ob不带参数并返回的函数Obj

如果您想默认构造一个Obj,请使用Obj ob;Obj ob{};

于 2014-05-06T17:48:24.033 回答
2

这条线

Obj ob();

不创建对象ob。它声明了一个不接受任何输入的函数并返回一个Obj.

将其更改为:

Obj ob;
于 2014-05-06T17:49:18.150 回答