如何为模板类重载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;
}
...全文
481 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; }
神奕(deprecated) 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
神奕(deprecated) 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;
}
神奕(deprecated) 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
源码下载地址: https://pan.quark.cn/s/8d2c461c797c JavaWeb程序设计构成了掌握Web交互式应用程序开发的核心领域,对于初学者来说,精通这一技术具有决定性意义。在“JavaWeb程序设计(第三版)作业答案”中,我们可以预期获得针对该教材习题的一系列深入解析,从而协助学习者强化知识体系。 JavaWeb所包含的技术组件涵盖了Servlet、JSP(JavaServer Pages)、JDBC(Java Database Connectivity)以及各类框架如Spring MVC、Struts等。Servlet是Java平台提供的一种扩展服务器功能的接口,能够处理HTTP请求并生成相应的反馈。JSP则是一种用于构建动态网页的工具,它支持开发者将HTML代码与Java代码进行整合编写,从而简化了Web应用程序的开发流程。 作业答案通常会涉及以下几个核心内容: 1. **Servlet基础**:可能包含Servlet生命周期、init(), service(), destroy()方法的应用,以及如何在web.xml文件中设定Servlet的映射关系。 2. **JSP基础**:JSP的九大内置对象,如request、response、session、application等的使用,以及EL(Expression Language)和JSTL(JavaServer Pages Standard Tag Library)的实际操作。 3. **HTTP协议理解**:GET和POST请求方法的差异,请求头与响应头的应用,以及会话管理的概念阐释。 4. **JDBC数据库操作**:与数据库建立连接,执行SQL指令,处理查询结果集,以及...
源码链接: https://pan.quark.cn/s/a4b39357ea24 斐讯K2是一款广受用户青睐的无线路由器,其运行表现稳定且具备较高的可操作性,在DIY爱好者群体中拥有极高的声誉。本资料将系统性地阐述斐讯K2的固件刷机方法及其关联的技术要点。固件升级是路由器爱好者改善设备性能、扩展功能的一种普遍手段,经由替换出厂固件,能够达成更加个性化的网络配置、增强安全防护等目标。斐讯K2固件资源库涵盖了多种知名的非官方固件,诸如Tomato Pheonix 不死鸟、高恪、PandoraBox 潘多拉等,这些固件均具备独特的优势,能够适配不同用户的需求。 1. Tomato Pheonix 不死鸟:Tomato是一款立足于Linux的开源固件,以其精巧、高效而备受推崇。不死鸟版本是专门为华硕及斐讯路由器优化的分支,提供了卓越的QoS(服务质量)配置、详尽的图表监控以及便捷的固件升级途径。对于那些需要精准调控带宽和监测网络状态的用户而言,这是一个理想的选项。 2. 高恪:高恪固件是OpenWrt的定制化版本,着重于操作的便捷性和运行的可靠性,特别适合对路由器操作不甚熟悉的用户群体。它提供了一些实用的功能,例如内置的广告屏蔽、快速测速工具等,同时保留了OpenWrt的适应性。 3. PandoraBox 潘多拉:潘多拉盒是另一款基于OpenWrt的固件,它以丰富的插件库和强大的自定义潜力而闻名。用户能够依据个人需求安装各类插件,实现更多功能,如远程接入、DDNS(动态域名解析服务)等。 4. 官方固件的纯净版本与定制版本:官方固件通常更侧重于稳定性,纯净版意味着未预置额外的应用或服务,适合注重稳定性的用户。定制版则可能包含了制造商的特色功能或优...

65,210

社区成员

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

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