64,676
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
template <typename T>
class testClass
{
public:
friend testClass operator+ (const testClass& lhs, const testClass& rhs);
testClass(T a = 0, T b = 0):_i(a),_j(b){}
testClass(const testClass& rhs);
protected:
T _i,_j;
};
template<typename T>
testClass<T>::testClass(const testClass& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
template<typename T>
testClass<T>
operator+ (const testClass<T>& lhs, const testClass<T>& rhs)
{
testClass temp(lhs);
temp._i += rhs._i;
temp._j += rhs._j;
return temp;
}
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c = a + b;
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class testClass
{
friend const testClass operator*<T> (const testClass& lhs, const testClass& rhs);
public:
testClass(T a, T b):_i(a),_j(b){}
testClass(const testClass& rhs);
private:
T _i,_j;
};
template<typename T>
const testClass<T>
operator* (const testClass<T>& lhs, const testClass<T>& rhs)
{
testClass<T> temp(lhs._i * rhs._i, lhs._j * rhs._j);
return temp;
}
template<typename T>
testClass<T>::testClass(const testClass<T>& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c = a * b;
cout<<"pass"<<endl;
system("pause");
return 0;
}
虽然不明白方法2为什么会出现这样的问题,但相较之下,effective C++对于这个问题所推荐的方法,不论从封装性、扩展性上都优雅的多:
#include <iostream>
using namespace std;
template <typename T>
class testClass;
template <typename T>
const testClass<T>
doAddition (const testClass<T>& lhs, const testClass<T>& rhs);
template <typename T>
class testClass
{
friend const testClass operator* (const testClass& lhs, const testClass& rhs)
{ return doAddition(lhs,rhs); }
public:
testClass(T a, T b):_i(a),_j(b){}
testClass(const testClass& rhs);
void print(ostream& os = cout);
T getI()const{return _i;}
T getJ()const{return _j;}
private:
T _i,_j;
};
template<typename T>
const testClass<T>
doAddition (const testClass<T>& lhs, const testClass<T>& rhs)
{
testClass<T> temp(lhs.getI() + rhs.getI(), lhs.getJ() + rhs.getJ());
return temp;
}
template<typename T>
testClass<T>::testClass(const testClass<T>& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
template<typename T>
void testClass<T>::print(ostream& os = cout)
{
os<<_i<<","<<_j<<endl;
}
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c = a * b;
c.print();
cout<<"pass"<<endl;
system("pause");
return 0;
}
[/quote]
同意,effective C++对于这个问题所推荐的方法,不论从封装性、扩展性上都优雅的多。
你研究的比我要深入得多。
看来,方法2 即便有解决方案,也只能是应付手段,不是好办法。
#include <iostream>
using namespace std;
template <typename T>
class testClass
{
friend const testClass operator*<T> (const testClass& lhs, const testClass& rhs);
public:
testClass(T a, T b):_i(a),_j(b){}
testClass(const testClass& rhs);
private:
T _i,_j;
};
template<typename T>
const testClass<T>
operator* (const testClass<T>& lhs, const testClass<T>& rhs)
{
testClass<T> temp(lhs._i * rhs._i, lhs._j * rhs._j);
return temp;
}
template<typename T>
testClass<T>::testClass(const testClass<T>& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c = a * b;
cout<<"pass"<<endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class testClass;
template <typename T>
const testClass<T>
doAddition (const testClass<T>& lhs, const testClass<T>& rhs);
template <typename T>
class testClass
{
friend const testClass operator* (const testClass& lhs, const testClass& rhs)
{ return doAddition(lhs,rhs); }
public:
testClass(T a, T b):_i(a),_j(b){}
testClass(const testClass& rhs);
void print(ostream& os = cout);
T getI()const{return _i;}
T getJ()const{return _j;}
private:
T _i,_j;
};
template<typename T>
const testClass<T>
doAddition (const testClass<T>& lhs, const testClass<T>& rhs)
{
testClass<T> temp(lhs.getI() + rhs.getI(), lhs.getJ() + rhs.getJ());
return temp;
}
template<typename T>
testClass<T>::testClass(const testClass<T>& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
template<typename T>
void testClass<T>::print(ostream& os = cout)
{
os<<_i<<","<<_j<<endl;
}
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c = a * b;
c.print();
cout<<"pass"<<endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class testClass
{
public:
template<T>
friend testClass<T> operator+ (const testClass<T>& lhs, const testClass<T>& rhs);
testClass(T a = 0, T b = 0):_i(a),_j(b){}
testClass(const testClass& rhs);
protected:
T _i,_j;
};
template<typename T>
testClass<T>::testClass(const testClass& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
template<typename T>
testClass<T> operator+ (const testClass<T>& lhs, const testClass<T>& rhs)
{
testClass<T> temp(lhs);
temp._i += rhs._i;
temp._j += rhs._j;
return temp;
}
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c(a);
c = a + b;
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class testClass
{
public:
template<typename U> //定义一个和 T不同的模板参数
friend //friend 写在 template<typename U> 和函数定义之间
// 友元函数的模板参数是U,而不是T
testClass<U> operator+ (const testClass<U>& lhs, const testClass<U>& rhs);
testClass(T a = 0, T b = 0):_i(a),_j(b){};
testClass(const testClass& rhs);
void print(){
cout<<_i<<","<<_j<<endl;
}
protected:
T _i,_j;
};
template<typename T>
testClass<T>::testClass(const testClass& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
template <typename T>
testClass<T>
operator+ (const testClass<T>& lhs, const testClass<T>& rhs){
testClass<T> temp(lhs);
temp._i += rhs._i;
temp._j += rhs._j;
return temp;
};
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c = a + b;
c.print ();
cin.get();
return 0;
}
以上代码在VC2008 和 Code::Blocks 上的某个minGW 上编译通过
friend myMatrix<elemType>
operator+(const myMatrix<elemType>& lhs, const myMatrix<elemType>& rhs);
然后就出现了无法解析错误。
[fedora@localhost ~]$ g++ -o test test.cpp
test.cpp:9:14: 错误:‘class T’的声明
template<typename T>
^
test.cpp:5:11: 错误:隐藏了模版形参‘class T’
template <typename T>
^
#include <iostream>
using namespace std;
template <typename T>
class testClass
{
public:
template<typename T>
friend testClass<T> operator+ (const testClass<T>& lhs, const testClass<T>& rhs);
testClass(T a, T b):_i(a),_j(b){}
testClass(const testClass& rhs);
private:
T _i,_j;
};
template<typename T>
testClass<T>::testClass(const testClass<T>& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
template<typename T>
testClass<T>
operator+ (const testClass<T>& lhs, const testClass<T>& rhs)
{
testClass<T> temp(lhs);
temp._i += rhs._i;
temp._j += rhs._j;
return temp;
}
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c = a + b;
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class testClass
{
public:
template<T>
friend testClass<T> operator+ (const testClass<T>& lhs, const testClass<T>& rhs);
testClass(T a, T b):_i(a),_j(b){}
testClass(const testClass& rhs);
public:
T _i,_j;
};
template<typename T>
testClass<T>::testClass(const testClass<T>& rhs)
{
_i = rhs._i;
_j = rhs._j;
}
template<typename T>
testClass<T>
operator+ (const testClass<T>& lhs, const testClass<T>& rhs)
{
testClass<T> temp(lhs);
temp._i += rhs._i;
temp._j += rhs._j;
return temp;
}
int main()
{
testClass<int> a(1,2),b(3,4);
testClass<int> c = a + b;
return 0;
}