6

首先,我知道在 stackoverflow 上已经有类似的问题(这个这个这个),这就是为什么我理解我的问题的原因。不幸的是,这并不能帮助我解决它。

虽然上述问题都与默认的无参数构造函数有关,但我在使用具有默认值的双参数构造函数时遇到了问题- 我试图构造一个调用构造函数的对象,只给出第一个值,并将其解析为函数声明而不是对象。

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

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

漂亮、可爱、优雅,但它不起作用(错误与我链接的所有问题相同)。我发现这个论坛教程确实将该问题称为最令人烦恼的 parse,但它的解决方案(在参数周围加上括号)并没有解决问题(在这种情况下它是一长串错误消息,但我可以编辑如果它有帮助,它会在稍后的问题中出现 - 这些都与继承虚函数有关)。

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

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 36是线class 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

4 回答 4

3

algoContainer myAC(algo1Concrete(workData));

这条线是非法的。您不能将右值绑定到可变的左值引用。它必须是const——即使是这样,对象也会在它被除构造函数之外的任何函数使用之前就死掉了。

于 2012-03-19T14:52:06.883 回答
3

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

algoContainer( algo1Virtual &alg1,
               algo2Virtual &alg2 );

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

这将参数作为非常量引用。因此,当您拨打以下电话时:

algoContainer myAC(algo1Concrete(workData));

建设:

algo1Concrete(workData)

导致构建一个匿名临时。匿名临时对象不能绑定到非常量引用,这很简单,因为它们是临时的,您可能对它们所做的任何更改都会立即消失(这不是真正的原因,但似乎是有道理的。修改匿名并不意味着任何临时的,因为您以后无法使用它(没有名字)或最终(它的临时))。实际上,非常量引用只能绑定到左值,而匿名临时对象是右值。(详细信息:非常量引用只能绑定到左值

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

按值传递将如下所示:

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

注意:关于默认参数。

我通常建议避免函数的默认参数,因为它们导致更多的混乱而不是方便。所有默认参数都可以简单地通过重载函数来“模拟”。因此,在您的情况下,您将拥有三个 ctor:

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,它只是一个意见。

于 2012-03-20T15:13:47.107 回答
0

写这个:

algoContainer myAC((algo1Concrete(workData)));

然后在互联网上搜索“最令人烦恼的解析”。StackOverflow 上也有数百个此问题的重复项,但很难找到,因为问题本身从未发现问题。(如果是,那就毫无疑问了。)


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

algo1Concrete ac = algo1Concrete(workData);
algoContainer myAC(ac);
于 2012-03-19T14:45:31.293 回答
0

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

于 2012-03-19T14:48:42.947 回答