简体   繁体   English

C++ 代码中的错误“未定义对运算符 << 的引用”

[英]Error “Undefined reference to operator<<” in C++ code

I have written a simple operator overloaded function, which is working fine if I define it within the class (block commented), but getting error, if define outside the class.我编写了一个简单的运算符重载 function,如果我在 class 中定义它,它工作正常(块注释),但如果在 class 之外定义,则会出错。 Any idea where is the mistake?知道错误在哪里吗?

#include<iostream>

using namespace std;


template<class T>
class vector{
    
    int d;
public:
    vector(){
        d = 0;
    }
    vector(T data){
        d = data;
    }
    
    friend void operator << (vector &v, T data);
    /*{
        cout << data << endl;
    }*/
    
};

template<class T>
void operator << (vector<T> &v, T data){
    
    cout <<  data;
}


int main(){
    
    vector<int> v1;
    v1 << 10;
    
    return 0;
}

This friend function declaration within the class template definition这个朋友在 class 模板定义中声明 function

friend void operator << (vector &v, T data);

is not a template function (though it is a templated entity if it is defined within the class).不是模板 function (尽管如果在类中定义它是模板实体)。

On the other hand, this declaration outside the class template definition另一方面,这个声明在 class 模板定义之外

template<class T>
void operator << (vector<T> &v, T data){
    
    cout <<  data;
}

defines a template function.定义了一个模板 function。

Either define the friend function in the class definition.在 class 定义中定义朋友 function。 And in this case the compiler will generate the definition of the function for each used specialization of the class template.在这种情况下,编译器将为 class 模板的每个使用的特化生成 function 的定义。 Or for each used class specialization you have to define (provide) a separate non-template friend function explicitly yourself.或者对于每个使用的 class 专业化,您必须自己明确定义(提供)一个单独的非模板朋友 function。

Here is a demonstrative program这是一个演示程序

#include<iostream>

template<class T>
class vector{
    
    int d;
public:
    vector(){
        d = 0;
    }
    vector(T data){
        d = data;
    }
    
    friend void operator << (vector &v, T data);
    /*{
        cout << data << endl;
    }*/
    
};

template<class T>
void operator << (vector<T> &v, T data)
{    
    std::cout <<  data;
}

void operator << (vector<int> &v, int data)
{    
    std::cout <<  data;
}

int main()
{
    
    vector<int> v1;
    v1 << 10;
    
    return 0;
}

The program output is程序 output 是

10

In the program there are two overloaded functions operator << .在程序中有两个重载函数operator << The first one is a friend non-template function declared in the class definition and the second one is a template function that is not a friend function of the class. The first one is a friend non-template function declared in the class definition and the second one is a template function that is not a friend function of the class. For the class specialization vector<int> used in main you have to provide the non-template friend function definition outside the class.对于 main 中使用的 class 特化vector<int> ,您必须在 class 之外提供非模板友元 function 定义。

Or you could define the friend function within the class.或者您可以在 class 中定义朋友 function。

Problem is how to friend a template to other template.问题是如何将模板与其他模板联系起来。

Without friendship your code works (you do no access private parts).没有友谊,您的代码就可以工作(您无法访问私有部分)。

To fix it you have to friend a template and not to a regular function.要修复它,您必须使用模板而不是常规 function。 Since function you are defining is a template friendship statement have to express that.由于您定义的 function 是模板友谊声明,因此必须表达这一点。


template<class T>
class vector{
    
    int d;
public:
    ...

    template<typename U>
    friend void operator<<(vector<U> &v, U data);
};

https://godbolt.org/z/oxcojb https://godbolt.org/z/oxcojb

Offtopic :题外话
Personally I avoid friendships (I do not remember to ever use it in my code).我个人避免友谊(我不记得在我的代码中使用过它)。 Your code I would do like this .你的代码我会这样做

if you are using namespace std and then defining a class called vector, which is a name from the std namespace, the compiler will not know which one to use and this will result in undefined behaviour, or so I think.如果您using namespace std ,然后定义一个名为 vector 的 class ,这是来自 std 命名空间的名称,编译器将不知道要使用哪一个,这将导致未定义的行为,或者我认为。

also, you are getting the error because you need to define that function inside the class because you are using a template.另外,您收到错误是因为您需要在 class 中定义 function ,因为您使用的是模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM