From:
Http://topic.csdn.net/u/20071106/17/c562631f-fd24-4836-9e13-43c53157bd8c.html
First of all, I would like to thank you for your attention and answers, but I found that you are enthusiastic and conscientious. ------- no one has compiled my problem code ..
The problem code I gave in the post can be compiled as it removes all other irrelevant codes:
//// // Matrix. h file content (partial code )//////////////////
# Include
/// Matrix template Forward Declaration /////
Template
Class matrix;
/////////////////////////////
Template
STD: ostream & operator * (STD: ostream & OS, const Matrix & Mat); // compiled
Template
STD: ostream & operator & mat); // compiled through
Template
Class Matrix
{
Friend STD: ostream & operator * (STD: ostream &, const Matrix &); // Compiled
Friend STD: ostream & operator (STD: ostream &, const Matrix &); // Compiled
};
The reason why I failed to compile the Code is that I declared an operator * overload member function in my actual code,
It conflicts with the operator * template function of youyuan (but this is not the case. I have never seen C ++ have such syntax requirements .)
The code that fails to be compiled is as follows:
//// // Matrix. h file content (this Code cannot be compiled )//////////////////
# Include
/// Matrix template Forward Declaration /////
Template
Class matrix;
/////////////////////////////
Template
STD: ostream & operator * (STD: ostream & OS, const Matrix & Mat); // compiled
Template
STD: ostream & operator & mat); // compiled through
Template
Class Matrix
{
Const Matrix Operator * (const Matrix &) const; // note... this is the root cause of the problem !!!!!!
Friend STD: ostream & operator * (STD: ostream &, const Matrix &); // Compilation failed
Friend STD: ostream & operator (STD: ostream &, const Matrix &); // Compiled
};
After I found that compilation failed yesterday, I was introduced into the misunderstanding and thought it was a operator * problem.
Even operator +, operator-, and operator/are not allowed.
But operator later I found that if the class also declares an operator friend STD: ostream & operator (STD: ostream &, const Matrix &);
This section will also fail to be compiled...
So find the solution ------ write all the friend declarations before the class member definition. The Code is as follows:
///////////// Content of the matrix. h file (compiled successfully! Yeah )//////////////////
# Include
/// Matrix template Forward Declaration /////
Template
Class matrix;
/////////////////////////////
Template
STD: ostream & operator * (STD: ostream & OS, const Matrix & Mat); // compiled
Template
STD: ostream & operator & mat); // compiled through
Template
Class Matrix
{
//// Note that all the friend statements are placed before the member definition ..////////////
Friend STD: ostream & operator * (STD: ostream &, const Matrix &); // Compiled
Friend STD: ostream & operator (STD: ostream &, const Matrix &); // Compiled
Const Matrix Operator * (const Matrix &) const; // note... this is the root cause of the problem !!!!!!
};
Bytes ------------------------------------------------------------------------------------------------------------------------
// From eumatrix3.h
// History 1
// Friend function template for class templates
// Euvector3
Operator * (const euvector3
& Rkpoint, const eumatrix3
& Rkmatrix );
// Eumatrix3
Operator * (treal fscalar, const eumatrix3
& Rkmatrix );
// Refer to the three conditions of the membership statement in the C ++ primer class template
// Here, for second
// I use Dev and VC 2005, found that vc2005 support is poor, only when the two friends in the class Declaration placed in
// Functions of other overload operators * (eg: eumatrix3 operator * (const eumatrix3 & rkmatrix) const, etc)
// Front !!!!!
// Compile successfully!
// Dev no problem !!!!!!
// Reference http://topic.csdn.net/u/20071106/17/c562631f-fd24-4836-9e13-43c53157bd8c.html