如何为模板类重载operator+?

I_AM_P_UNCLE 2014-08-01 09:50:52
RT,参考了Essential C++第六章的solution,同时搜索了各种网上的做法,仍然无法通过编译,报错:无法解析的外部命令,非模板函数倒是通过了编译,实在不明白是何原因,特此求教,先谢过各位。

#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;
}
...全文
408 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2014-08-05
  • 打赏
  • 举报
回复
引用 12 楼 u012302975 的回复:
谢lm_whales答复。 正好在看effective c++里面的第46条有讲到,方法1与方法3更适合,方法2似乎会根据编译器不同可能出现错误情况,就不使用了。
原来 effective c++ 有讲到,看来我还是没又仔细看这本书, 方法2似乎跟顺序相关,有些编译器,对模板定义声明的顺序,不能正确处理。 VC6 的 operator << ,operator>> 似乎也是这种情况 对于那个编译器,后来我又试验了一下 对于方法2 ,修改一下 #include <iostream> using namespace std; 的位置,就能够正常工作了 放在 模板类定义的后面,就可以正常工作了。
lm_whales 2014-08-05
  • 打赏
  • 举报
回复
引用 14 楼 u012302975 的回复:
[quote=引用 13 楼 lm_whales 的回复:] [quote=引用 12 楼 u012302975 的回复:] 谢lm_whales答复。 正好在看effective c++里面的第46条有讲到,方法1与方法3更适合,方法2似乎会根据编译器不同可能出现错误情况,就不使用了。
原来 effective c++ 有讲到,看来我还是没又仔细看这本书, 方法2似乎跟顺序相关,有些编译器,对模板定义声明的顺序,不能正确处理。 VC6 的 operator << ,operator>> 似乎也是这种情况 对于那个编译器,后来我又试验了一下 对于方法2 ,修改一下 #include <iostream> using namespace std; 的位置,就能够正常工作了 放在 模板类定义的后面,就可以正常工作了。 [/quote] 其实不使用方法2还有一个原因是,虽然该方法能够重载operator+,却在重载operator*时再次出现了问题:
#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 即便有解决方案,也只能是应付手段,不是好办法。
I_AM_P_UNCLE 2014-08-05
  • 打赏
  • 举报
回复
引用 13 楼 lm_whales 的回复:
[quote=引用 12 楼 u012302975 的回复:]
谢lm_whales答复。
正好在看effective c++里面的第46条有讲到,方法1与方法3更适合,方法2似乎会根据编译器不同可能出现错误情况,就不使用了。


原来 effective c++ 有讲到,看来我还是没又仔细看这本书,
方法2似乎跟顺序相关,有些编译器,对模板定义声明的顺序,不能正确处理。

VC6 的 operator << ,operator>> 似乎也是这种情况
对于那个编译器,后来我又试验了一下
对于方法2 ,修改一下
#include <iostream>
using namespace std;
的位置,就能够正常工作了
放在 模板类定义的后面,就可以正常工作了。





[/quote]

其实不使用方法2还有一个原因是,虽然该方法能够重载operator+,却在重载operator*时再次出现了问题:

#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;
}


I_AM_P_UNCLE 2014-08-04
  • 打赏
  • 举报
回复
引用 10 楼 lm_whales 的回复:
#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 上编译通过
谢lm_whales答复。 正好在看effective c++里面的第46条有讲到,方法1与方法3更适合,方法2似乎会根据编译器不同可能出现错误情况,就不使用了。
我看你有戏 2014-08-03
  • 打赏
  • 举报
回复

#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;
}
lm_whales 2014-08-03
  • 打赏
  • 举报
回复
#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 上编译通过
lm_whales 2014-08-03
  • 打赏
  • 举报
回复
1)通用方法,友元函数在类内部定义 #include <iostream> using namespace std; template <typename T> class testClass { public: //友元函数在类内部定义 friend testClass operator+ (const testClass& lhs, const testClass& rhs){ testClass<T> temp(lhs); temp._i += rhs._i; temp._j += rhs._j; return temp; }; 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; } int main(int argc, _TCHAR* argv[]) { testClass<int> a(1,2),b(3,4); testClass<int> c = a + b; c.print (); cin.get(); return 0; } 2)此方法,VC++2008可以通过, Code::Blocks 13.12 下的minGW编译器 的某个版本下不能通过,原因是和标准库有冲突: #include <iostream> using namespace std; //类模板前置声明 template<typename T> class testClass; //类外定义声明友元函数模板 template<typename T> testClass<T> operator+ (const testClass<T> & lhs, const testClass<T> & rhs); template <typename T> class testClass { public: // 函数名后加上<T> friend testClass operator+ <T>(const testClass& lhs, const testClass& 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(int argc, _TCHAR* argv[]) { testClass<int> a(1,2),b(3,4); testClass<int> c = a + b; c.print (); cin.get(); return 0; }
神奕 2014-08-02
  • 打赏
  • 举报
回复
引用 4 楼 u012302975 的回复:
多谢lisong694767315指教…… 才发现自己改了太多遍代码贴了错的,请问下为什么在类内部的friend声明还要再写一遍template<typename T>,因为Essential C++给出的solution里面的声明是这样的:
	friend myMatrix<elemType>
		operator+(const myMatrix<elemType>& lhs, const myMatrix<elemType>& rhs);
然后就出现了无法解析错误。
不是template<typename T>,而是template<T>,表示使用类模板的参数作为operator+函数模板的参数。也就是说,给定一个实参类型T,testClass<T>实例的友元函数是以T为实参实例化的operator+函数,即一对一的关系。
I_AM_P_UNCLE 2014-08-02
  • 打赏
  • 举报
回复
引用 3 楼 lisong694767315 的回复:

#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;
}
多谢lisong694767315指教…… 才发现自己改了太多遍代码贴了错的,请问下为什么在类内部的friend声明还要再写一遍template<typename T>,因为Essential C++给出的solution里面的声明是这样的:
	friend myMatrix<elemType>
		operator+(const myMatrix<elemType>& lhs, const myMatrix<elemType>& rhs);
然后就出现了无法解析错误。
I_AM_P_UNCLE 2014-08-02
  • 打赏
  • 举报
回复
额……我用的是VS2010
神奕 2014-08-02
  • 打赏
  • 举报
回复
引用 6 楼 u012302975 的回复:
#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;
}
我用g++编译失败:

[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>
           ^
I_AM_P_UNCLE 2014-08-02
  • 打赏
  • 举报
回复
引用 5 楼 lisong694767315 的回复:
[quote=引用 4 楼 u012302975 的回复:] 多谢lisong694767315指教…… 才发现自己改了太多遍代码贴了错的,请问下为什么在类内部的friend声明还要再写一遍template<typename T>,因为Essential C++给出的solution里面的声明是这样的:
	friend myMatrix<elemType>
		operator+(const myMatrix<elemType>& lhs, const myMatrix<elemType>& rhs);
然后就出现了无法解析错误。
不是template<typename T>,而是template<T>,表示使用类模板的参数作为operator+函数模板的参数。也就是说,给定一个实参类型T,testClass<T>实例的友元函数是以T为实参实例化的operator+函数,即一对一的关系。[/quote] lisong694767315大大,你给的代码似乎不对,因为我刚发现我贴出的代码里_i,_j都声明成了public,如果改成private或protected,就会出现无法访问成员的错误,也就意味着友元声明其实失败了,而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;
}
神奕 2014-08-01
  • 打赏
  • 举报
回复

#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;
}
I_AM_P_UNCLE 2014-08-01
  • 打赏
  • 举报
回复
引用 1 楼 taodm 的回复:
google herb sutter: be friending templates
有地址么?没搜到 用的vs2010
taodm 2014-08-01
  • 打赏
  • 举报
回复
google herb sutter: be friending templates

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧