C++ diary-the difference between the left shift template function under Windows and Linux

When learning C++, sometimes we will use the VS software provided by Microsoft, and we will encounter some problems when learning the template class to overload the left shift operator. Write a simple program as an example:

#include <iostream>
using namespace std;

template<class T>
class Complex
{
private:
    T a;
public:
    Complex(T a);
    friend ostream & operator<< <T>(ostream & os, const Complex<T> & obj); //VS中特殊的标志<T>
};

template<class T>
Complex<T>::Complex(T a)
{
    this->a = a;
}

template<class T>
ostream & operator<<(ostream & os, const Complex<T> & obj)
{
    os << "a = " << obj.a;
    return os;
}

int main(int argc, char const *argv[])
{
    Complex<int> c1(4);

    cout << c1 << endl; 

    system("pause");
    return 0;
}

This program is just a program written in VS2010. For convenience, I write these programs to one file. If you want to divide the declaration of the class, the realization of the class, and the realization of the main function into three files, you must The main function contains the file of the realization of the class, that is:
the declaration of the template class is in ah, the realization of the class is in a.cpp, and the main function is in main.cpp, then main.cpp must contain #include " "a.cpp" instead of "ah" header file, this is just a small detail, it is also applicable in the Linux environment.

In the above program, overloading the << operator is added in the declaration part to make the template class compile and pass. This is related to the implementation of the VS compiler, and it cannot be written when writing the implementation of the << operator.

This same program cannot run under Linux, because Linux uses the g++ compiler, and the compilation principle of the g++ compiler is different from that of the VS compiler. In Linux, the program should be written like this:

#include <iostream>

using namespace std;

template<class T>
class Complex
{
private:
    T a;
public:
    Complex(T a);

    template<class Mytype> 
    friend ostream & operator<<(ostream & os, const Complex<Mytype> & obj);//在使用g++编译器中需要加上类的的前置声明
};

template<class T>
Complex<T>::Complex(T a)
{
    this->a = a;
}

template<class T>
ostream & operator<<(ostream & os, const Complex<T> & obj) 
{
    os << "a = " << obj.a;
    return os;
}
/*
template<class Mytype>
ostream & operator<<(ostream & os, const Complex<Mytype> & obj)
{
    obj.count++;
    os << "count = " << obj.count << endl;
    os << "a = " << obj.c_a;
    return os;
}
*/
int main(int argc, char const *argv[])
{
    Complex<int> c1(8);

    cout << c1 << endl; 
    return 0;
}

This is the code written when using the g++ compiler under Linux. Among them, the other one I have checked on the Internet is to add <> in the middle of operator<<(...) ie: operator<< <>(...) This method cannot Compile and pass in g++ compiler.

The left shift operator in g++ needs to add the template function declaration of the class when it is declared, and the type of the declaration cannot be the same as the declaration type of the class, that is, the Mytype declaration friend function left shift operator is used in the program. Either the Mytype type can be used for implementation, or the T type flag can be used to implement the overloading of the left shift operator. The selection here is the same as the class declaration flag.

Guess you like

Origin blog.csdn.net/qq_32198277/article/details/77623270