这几天学着用C++ 模版(Genetic programming)写程序,碰到一个很纠结的问题。描述如下:

通常情况下,我喜欢重载operator<<()用来输出类内部的信息,以方便调试。例如:

class A
{
public:
	A(string value): data(value){}
	friend ostream& operator<< (ostream & out, const A& me);
private:
	string data;
};

ostream& operator<< (ostream & out, const A& me)
{
	cout << me.data << endl;
	return out;
}

但是,如果给类型加上模版参数,那么友元函数那边也应该加上同样的参数,形式就变为了:

template<typename T>
class A
{
public:
	A(string value): data(value){}
	friend ostream& operator<< (ostream & out, const A<T>& me);
private:
	T data;
};

template<typename T>
ostream& operator<< (ostream & out, const A<T>& me)
{
	cout << me.data << endl;
	return out;
}

 

我们知道,template关键字的作用域是下面一个类型或函数的声明,因此,需要两个template才能得以实现。可是编译器(Visual Studio 2010)却不认为它们是相同类型的,因此报出了链接错误:

TemplateFriendFunction.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class
 std::basic_ostream<char,struct std::char_traits<char> > &,class A<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > con
st &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$A@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@@Z) referenced in fun
ction _main
TemplateFriendFunction.exe : fatal error LNK1120: 1 unresolved externals

经过一番分析,我发现链接不上的原因是:我们定义的友元函数还是一个模版,要实例化为参数变元后才能使用。也就是说,即使上面的友元看起来像函数定义,但是若没有经过参数化,编译器就不会生成该函数的代码。gcc的提示信息就显得人性化多了:

TemplateFriendFunction.cpp:14: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, const A<T>&)’ declares a non-template function
TemplateFriendFunction.cpp:14: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 

那么现在的问题就成为了如何实例化重载的操作符模版函数了,我们最先想到的是在函数后面加上模版参数:

	cout << <string> *a;

但是编译器不认这种表达式(语法错误、我推测甚至在词法分析阶段就报错了)。

解决方法有两种:(1)把友元函数体放到类声明内部,使其跟着类的实例化一同实例化:

template<typename T>
class A
{
public:
	A(string value): data(value){}
	friend ostream& operator<< (ostream & out, const A<T>& me)
	{
		cout << me.data << endl;
		return out;
	}
private:
	T data;
};

 

但是这样就破坏了类声明和定义分开的原则。

方法(2)把友元模版放到类模版声明之上,同样在类模版参数化时附带把友元函数参数化:

// 先声明模版类,以便在友元函数中引用.
template<typename T>
class A;

// 再声明友元函数.
template<typename T>
ostream& operator<< (ostream & out, const A<T>& me)

{

	cout << me.data << endl;

	return out;

}


// 最后在模版类中包含友元函数,并实例化友元函数.

template<typename T>

class A

{

public:

	A(T value): data(value){}

	friend ostream& operator<< <T> (ostream & out, const A<T>& me);

private:

	T data;

};

这样看起来虽然复杂,但是思路很清晰了。

综上所述,友元函数导致的链接错误是因为没有实例化模版函数变元,弄清了这一点,我们可以重新修改程序,同样使用C++的参数推测功能,使用最简短的代码,来实现这一功能:

template<typename T>
class A
{
public:
	A(string value): data(value){}
	friend ostream& operator<< <>(ostream & out, const A<T>& me);
private:
	T data;
};

template<typename T>
ostream& operator<< (ostream & out, const A<T>& me)
{
	cout << me.data << endl;
	return out;
}

 

在友元函数中显示实例化参数变元即可。其实这段程序比起一开始出错的那段程序来说,就多了”<>”这两个符号。

posted on 2011-10-29 16:52  chrihop  阅读(3128)  评论(2编辑  收藏  举报