相关疑难解决方法(0)

重载模板类的运算符时的隐式转换

我想知道为什么隐式类型转换不适用于类模板上的外部运算符重载.这是工作的非模板化版本:

class foo
{
public:

    foo() = default;

    foo(int that)
    {}

    foo& operator +=(foo rhs)
    {
        return *this;
    }
};

foo operator +(foo lhs, foo rhs)
{
    lhs += rhs;
    return lhs;
}
Run Code Online (Sandbox Code Playgroud)

正如所料,以下行正确编译:

foo f, g;
f = f + g; // OK
f += 5; // OK
f = f + 5; // OK
f = 5 + f; // OK
Run Code Online (Sandbox Code Playgroud)

另一方面,当类foo声明为这样的简单模板时:

template< typename T >
class foo
{
public:

    foo() = default;

    foo(int that)
    {} …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading implicit-conversion

20
推荐指数
2
解决办法
4325
查看次数

C++ - 如何为类模板声明函数模板的朋友

我有一个类模板,它将输出存储在数组中的对象列表.我收到以下错误,我很困惑,因为错误是在.obj和.exe文件中导致错误.

1个未解析的外部(proj08.exe第1行)
未解析的外部符号"class std :: basic_ostream>&__ cdecl operator <<(class std :: basic_ostream> &&,class MyVector)"(?? 6 @ YAAAV?$ basic_ostream @ DU? $ char_traits @ D @ std @@@ std @@ AAV01 @ V?$ MyVector @ N @@@ Z)在函数_main(porj08.obj第1行)中引用

proj08.cpp

#include "stdafx.h"
#include <string>
#include "MyVector.h"

const double FRACTION = 0.5; 

int main()
{
    cout << "\nCreating a vector of doubles named Sam\n";
    MyVector<double> sam;

    cout << "\nPush 12 values into the vector.";
    for (int i = 0; i < 12; i++) …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading ostream

4
推荐指数
2
解决办法
1871
查看次数