C ++声明了一个函数,而不是调用一个复杂的构造函数

时间:2012-03-19 14:38:21

标签: c++ constructor function-declaration most-vexing-parse

首先,我知道stackoverflow(thisthisthis one)上已经存在类似的问题,这就是我理解问题原因的原因。不幸的是,这并没有帮助我解决它。

虽然以上问题都与默认的无参数构造函数有关,但在使用带有默认值的两个参数构造函数时我遇到了问题 - 我试图构造一个只调用构造函数的对象给定的第一个值,它被解析为函数声明而不是对象。

以下是我的代码的一些片段(我重命名了类名,因为它们很长且不相关):

class algoContainer{
public:
algoContainer(algo1Virtual &alg1 = algo1Concrete::emptyInstance(),
      algo2Virtual &alg2 = algo2Concrete::instance());

someUsefulFunction();
};

class algo1Concrete : public algo1Virtual{
    private:
    algo1Concrete();
    public:
    static algo1Concrete &emptyInstance(); // "empty" instance treated
                                           // specifically
                                           //  -- uses private no arg constructor
    algo1Concrete(const std::vector<data> &myData); // construcotr
};

class algo1Virtual{
    // ... all functions virtual, no implementations ...
};


// ... similar for algo2Virtual/Concrete ...

Concrete类中的所有函数都已实现,而Virtual类中的所有函数都不是(构造函数和析构函数除外)。

所以,我现在的问题是我想做

之类的事情
std::vector <data> workData;
// fill workData
algoContainer myAC(algo1Concrete(workData));
myAC.someUsefulFunction(); // this line gives compile error

漂亮,可爱和优雅,但它不起作用(错误与我linked的所有问题相同。我发现这个forum-tutorial确实将问题称为最烦恼的解析,但它的解决方案(将括号括起来)并不能解决问题问题(在这种情况下,它是一大堆错误消息,但如果有帮助的话,我可以稍后在问题中编辑它 - 这些都与继承虚函数有关。)

如果我使用默认情况下所有参数的构造函数,我已经测试了我的代码,即使我只是单独构造第一个参数:

std::vector <data> workData;
// fill workData
algo1Concrete myA1(workData);
algoContainer myAC(myA1);

myAC.someUsefulFunction(); // now it works fine

algoContainer myAC2;
myAC2.someUsefulFunction(); // this also works

我可以按原样使用代码,但如果有人能给我一个更优雅的解决方案,我会非常感激。我现在正在使用它。


编辑:修复最烦恼的解析

时出现的错误信息

如果我使用带括号的代码:

algoContainer myAC((algo1Concrete(workData)));

我的错误是:

/some_path/main.cpp:47:65: error: no matching function for call to ‘algoContainer::algoContainer(algo1Concrete)’
/some_path/main.cpp:47:65: note: candidates are:
/some_path/algo/algocont.h:45:5: note: algoContainer::algoContainer(algo1Virtual&, algo2Virtual&)
/some_path/algo/algocont.h:45:5: note:   no known conversion for argument 1 from ‘algo1Concrete’ to ‘algo1Virtual&’
/some_path/algo/algocont.h:36:7: note: algoContainer::algoContainer(const algoContainer&)
/some_path/algo/algocont.h:36:7: note:   no known conversion for argument 1 from ‘algo1Concrete’ to ‘const algoContainer&’

为了便于阅读,我重命名了路径并插入了示例文件和类名(与上面相同)。只是一句话:line 45是有问题的构造函数的定义。另一方面,line 36class algoContainer行。

我也试过这段代码:

algoContainer myDect((algo1Virtual)(algo1Concrete(workData)));

然后错误完全不同:

/some_path/main.cpp:47:86: error: cannot allocate an object of abstract type ‘algo1Virtual’
/some_path/algo/alg1/algo1virtual.h:31:7: note:   because the following virtual functions are pure within ‘algo1Virtual’:
/some_path/algo/alg1/algo1virtual.h:42:8: note:     virtual algo1Virtual::~algo1Virtual()
/some_path/algo/alg1/algo1virtual.h:39:18: note:    virtual void algo1Virtual::someAlgo1Function(std::vector<data>&)
/some_path/main.cpp:47:87: error: no matching function for call to ‘algoContainer::algoContainer(algo1Virtual)’
/some_path/main.cpp:47:87: note: candidates are:
/some_path/algo/algocont.h:45:5: note: algoContainer::algoContiner(algo1Virtual&, algo2Virtual&)
/some_path/algo/algocont.h:45:5: note:   no known conversion for argument 1 from ‘algo1Virtual’ to ‘algo1Virtual&’
/some_path/algo/algocont.h:36:7: note: algo1Virtual::algo1Virtual(const algo1Virtual&)
/some_path/algo/algocont.h:36:7: note:   no known conversion for argument 1 from ‘algo1Virtual’ to ‘const algo1Virtual&’

希望这有帮助。

4 个答案:

答案 0 :(得分:3)

  

algoContainer myAC(algo1Concrete(workData));

这条线是非法的。您不能将rvalue绑定到可变左值引用。它必须是const - 即使它是,但是除了构造函数之外,任何函数都可以使用该对象。

答案 1 :(得分:3)

问题似乎是由于构造函数所采用的参数:

algoContainer( algo1Virtual &alg1,
               algo2Virtual &alg2 );

注意:为了简洁,我删除了默认参数。

这将参数作为非const引用。所以,当你拨打电话时:

algoContainer myAC(algo1Concrete(workData));

建设:

algo1Concrete(workData)

导致匿名临时的建设。匿名临时文件不能绑定到非const引用,简单因为它们是临时的,你可能对它们做的任何更改都会立即消失(这不是真正的原因,但似乎有意义。这并不意味着修改匿名暂时的,因为你以后无法使用它(没有名字)或最终(它的临时))。实际上,非const引用只能绑定到l值,而匿名临时值是r值。 (详情:Non-const reference may only be bound to an lvalue

通常,这种使用意味着想要将正在构造的对象的完全所有权赋予该函数。这可以通过值传递(昂贵)或在C ++ 11中通过rvalue引用来完成。

按值传递将如下所示:

algoContainer( algo1Virtual alg1,
               algo2Virtual alg2 );

这将导致不必要的副本。

另一个选择是通过C ++ 11中的右值引用传递,如:

algoContainer( algo1Virtual &&alg1,
               algo2Virtual &&alg2 );

现在您的第一次使用将开箱即用:

std::vector <data> workData;
// fill workData
algoContainer myAC(algo1Concrete(workData));
myAC.someUsefulFunction();

但是你需要修改第二次使用,以便你的对象被“移动”到构造函数中,并且algoContainer取得数据的所有权(名称局部变量然后是“坏”,不应该被使用)。

std::vector <data> workData;
// fill workData
algo1Concrete myA1(workData);
algoContainer myAC(std::move(myA1)); //NOTICE THE std::move call.
//myA1 is now a dummy, and unusable as all the internals have gone.
myAC.someUsefulFunction(); 

要使上述示例正常工作,您必须使用以下签名为algo1Concrete实现移动构造函数:

algo1Concrete ( algo1Concrete&& other )

将简单地将内部转移到当前并使“其他”处于未定义状态。 (详情:http://msdn.microsoft.com/en-us/library/dd293665.aspx

注意:关于默认参数。

我通常建议避免使用函数的默认参数,因为它们会导致比方便更多的混淆。只需重载函数即可“模拟”所有默认参数。因此,在您的情况下,您将有三个ctors:

algoContainer(); //This assumes that the args were both the statics
algoContainer( algo1Virtual alg1 ); //This assumes that arg2 was the static.
algoContainer( algo1Virtual alg1, algo2Virtual alg2 ); //This uses both input.

我同意它更详细,并且当前没有很多编译器实现继承构造函数,因此我们也经常复制代码。但这会将一个问题与调查问题时弹出的一些调试/魔术值问题隔离开来。但是,FWIW,这只是一个意见。

答案 2 :(得分:0)

写下这个:

algoContainer myAC((algo1Concrete(workData)));

然后在网上搜索“最烦恼的解析”。 StackOverflow上也有数百个这个问题的副本,但很难找到它们,因为问题本身并没有发现问题。 (如果是的话,毫无疑问。)


(编辑之后)临时对象(例如由值返回的函数调用创建的对象)不会绑定到非常量引用。你需要说:

algo1Concrete ac = algo1Concrete(workData);
algoContainer myAC(ac);

答案 3 :(得分:0)

作为当前解决方案的替代方案,您可以使用额外的一对括号,也可以使用复制初始化。