【问题标题】:Friend operator + : no matching function for call朋友运算符 + : 没有匹配的通话功能
【发布时间】:2014-05-08 12:00:05
【问题描述】:

最小版本:

我创建了一个最小版本的程序,它显示与以前相同的错误。谁能解释我为什么会收到这些错误?

#include <stdio.h>
#include <string>

using namespace std;

template<typename DataType> class Test
{
        public:
                Test<DataType>(DataType Data): Data(Data)
                {
                }
                Test<DataType>(Test<DataType> & Source): Data(Source.Data)
                {
                }
                friend Test<DataType> operator + (const Test<DataType> & T1, const Test<DataType> & T2)
                {
                        DataType NewData = T1.Data + T2.Data;
                        return Test<DataType>(NewData);
                }
        protected:
                DataType Data;
};

int main()
{
        Test<string> T1(string("Foo"));
        Test<string> T2(string("Bar"));

        auto T3 = T1 + T2;
        return 0;
}

编译命令:

g++ test.cpp -std=c++0x -Wall

编译结果:

test.cpp: In function ‘int main()’:
test.cpp:29:17: error: no matching function for call to ‘Test<std::basic_string<char> >::Test(Test<std::basic_string<char> >)’
test.cpp:29:17: note: candidates are:
test.cpp:12:3: note: Test<DataType>::Test(Test<DataType>&) [with DataType = std::basic_string<char>]
test.cpp:12:3: note:   no known conversion for argument 1 from ‘Test<std::basic_string<char> >’ to ‘Test<std::basic_string<char> >&’
test.cpp:9:3: note: Test<DataType>::Test(DataType) [with DataType = std::basic_string<char>]
test.cpp:9:3: note:   no known conversion for argument 1 from ‘Test<std::basic_string<char> >’ to ‘std::basic_string<char>’
test.cpp: In function ‘Test<std::basic_string<char> > operator+(const Test<std::basic_string<char> >&, const Test<std::basic_string<char> >&)’:
test.cpp:29:17:   instantiated from here
test.cpp:18:33: error: no matching function for call to ‘Test<std::basic_string<char> >::Test(Test<std::basic_string<char> >)’
test.cpp:18:33: note: candidates are:
test.cpp:12:3: note: Test<DataType>::Test(Test<DataType>&) [with DataType = std::basic_string<char>]
test.cpp:12:3: note:   no known conversion for argument 1 from ‘Test<std::basic_string<char> >’ to ‘Test<std::basic_string<char> >&’
test.cpp:9:3: note: Test<DataType>::Test(DataType) [with DataType = std::basic_string<char>]
test.cpp:9:3: note:   no known conversion for argument 1 from ‘Test<std::basic_string<char> >’ to ‘std::basic_string<char>’

旧版本:

我有一个名为 Buffer 的模板类。我在头文件中实现了。我想为 + 运算符创建一个重载以便能够调用让我们说

auto Buffer3 = Buffer1 + Buffer2
//Where Buffer1 and Buffer2 are Buffer<string>

我在 class{} 中创建了一个函数;:

friend Buffer<ElementType> operator + (Buffer<ElementType> & B1, Buffer<ElementType> & B2)
{
    Buffer Output(B1.GetElementsNum() + B2.GetElementsNum(), B1.Overwrite || B2.Overwrite);
    Output += B1;
    Output += B2;
    return Output;
}

当我在 Visual Studio 中编译它时,一切正常,运行良好等等。

当我使用带有 -std=c++0x 的 g++ 编译它时,我得到:

Testing.h:57:30: error: no matching function for call to ‘Buffer<std::basic_string<char> >::Buffer(Buffer<std::basic_string<char> >)’
Testing.h:57:30: note: candidates are:
Buffer.h:16:3: note: Buffer<ElementType>::Buffer(Buffer<ElementType>&) [with ElementType = std::basic_string<char>]
Buffer.h:16:3: note:   no known conversion for argument 1 from ‘Buffer<std::basic_string<char> >’ to ‘Buffer<std::basic_string<char> >&’

Testing.h:57 是:

auto B3 = B1 + B2;

Buffer.h:16 是我对 Buffer 类的复制构造

解决方案:

创建一个复制构造函数参数const

【问题讨论】:

  • const 正确性:使参数const Buffer&lt;ElementType&gt; &amp; - msvc 在这里是错误的
  • @DieterLücking Aw,cmets 中的答案...
  • @luk32 经过一些发展,我的回答(与我的评论相同)变得无用 - 这就是原因。
  • @peku33 无需在此处将解决方案编辑为您的问题。它已经在答案中了。

标签: c++


【解决方案1】:

你的复制构造函数的参数不是const,所以加法返回的临时值不能绑定到它。

将临时引用绑定到非常量引用的能力是该语言的臭名昭著的 Visual C++“扩展”。

【讨论】:

  • 一个“扩展”只是为了让任何试图将某些东西移植到 Linux 的人遭受可怕的痛苦。女士,您的另一个“好”想法。
【解决方案2】:

关于您的后续问题:与以前相同的问题,但与您的副本 ctor:

// Make that a const &
Test<DataType>(const Test<DataType> & Source)

将参数设为 const 后,编译正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2015-11-24
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    相关资源
    最近更新 更多