朋友操作员+:没有匹配的呼叫功能

时间:2014-05-08 12:00:06

标签: c++

MINIMAL VERSION:

我创建了一个程序的最小版本,显示与以前相同的错误。任何人都可以解释我为什么会收到这些错误吗?

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

OLD VERSION:

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

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

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

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

的复制结构

SOLUTION:

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

2 个答案:

答案 0 :(得分:3)

复制构造函数的参数不是const,因此添加返回的临时值不能绑定到它。

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

答案 1 :(得分:1)

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

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

在使参数const后,它编译得很好。