我使用模板朋友时链接器错误? [英] linker errors when I use template friends?
问题描述
实际上,我还遇到了模板函数和模板
类的链接器错误。根据C ++常见问题解答,我将foo.cpp
倾注到foo.h中,我成功地避免了它们。
( http://www.parashift.com/c++-faq-lit ... templates.html )
然后,我在模板类的
定义之上预先声明了每个模板友元函数。但我仍然得到模板朋友链接器
错误。我的编译器是gcc 3.3.3。任何提示?谢谢,
-
William Xuuu
Actually, I also got linker errors with template functions and template
classes. And I avoided both of them successfully, by pouring foo.cpp
into foo.h, according to the C++ FAQ.
(http://www.parashift.com/c++-faq-lit...templates.html)
And then, I pre-declared each template friend function above the
definition of template class. But I still get template friends linker
error. My compiler is gcc 3.3.3. Any hints? Thanks,
--
William Xuuu
推荐答案
>
" william xuuu" < ab*@abc.abc>在消息新闻中写道:87 ************ @ abc.abc ...
"william xuuu" <ab*@abc.abc> wrote in message news:87************@abc.abc...
实际上,我也遇到了模板函数和模板的链接器错误
班。根据C ++常见问题解答,我通过将foo.cpp倾注到foo.h中来成功地避免了它们。
( http://www.parashift.com/c++-faq-lit...templates.html )
然后,我在模板类的定义之上预先声明了每个模板的友元函数。但我仍然得到模板朋友链接器错误。我的编译器是gcc 3.3.3。任何提示?谢谢,
Actually, I also got linker errors with template functions and template
classes. And I avoided both of them successfully, by pouring foo.cpp
into foo.h, according to the C++ FAQ.
(http://www.parashift.com/c++-faq-lit...templates.html)
And then, I pre-declared each template friend function above the
definition of template class. But I still get template friends linker
error. My compiler is gcc 3.3.3. Any hints? Thanks,
可能你的''预申报''与你的实际定义不一样。
所以链接器试图找到你的定义预先申报并发现
什么都没有。向我们展示代码。
不要在foo.h中#include foo.cpp,这只是让事情变得不必要了。
复杂。把它翻到foo.h并扔掉foo.cpp。你知道吗
是有道理的。
john
Probably your ''pre-declaration'' is not the same as your actual definition.
So the linker tries to find the definiton for you pre-declaration and finds
nothing. Show us the code.
Don''t #include foo.cpp in foo.h, that just makes things unnecessarily
complicated. Put everthing into foo.h and throw away foo.cpp. You know it
makes sense.
john
" John Harrison" <乔************* @ hotmail.com>写道:
"John Harrison" <jo*************@hotmail.com> writes:
可能你的''预申报''与你的实际定义不一样。
所以链接器试图找到你预定义的定义和发现什么都没有。向我们展示代码。
Probably your ''pre-declaration'' is not the same as your actual definition.
So the linker tries to find the definiton for you pre-declaration and finds
nothing. Show us the code.
好的,这里是:(只有两个文件)
// -------- ---------------" vector_set.h"
//预申报。
模板< typename T> class Vector_Set;
template< typename T> Vector_Set< T> operator +(const Vector_Set< T>& s1,
const Vector_Set< T>& s2);
//开始上课本身
模板< typename T> class Vector_Set
{
朋友Vector_Set< T> operator +(const Vector_Set< T>& s1,const
Vector_Set< T>& s2);
public:
...
};
//朋友实施
模板< typename T> Vector_Set< T> operator +(const Vector_Set< T>& s1,
const Vector_Set< T>& s2)
{
...
}
// -----------------------" vector_set_main.cpp"
typedef Vector_Set< int>设置;
设置a = Set(10),b = Set(10);
for(int i = 0; i< a.size (); i ++)
if(i%2 == 1)
a.add_member(i);
else
b.add_member(i);
//这里导致错误!
a + b;
警告和错误:
g ++ -c vector_set_main.cpp
vector_set_main.cpp中包含的文件:8:
vector_set.h:29:警告:朋友声明`Vector_Set< T> operator +(const
Vector_Set< T>&,const Vector_Set< T>&)''声明非模板函数
g ++ -g vector_set_main.o -O2 -lm -o vector_set_main
vector_set_main.o(。text + 0x172):在函数main中:
:未定义对`operator +的引用(Vector_Set< int> const& amp; ;,Vector_Set< int> const&)''
有帮助吗?
-
William Xuuu
Okay, here it is: (only two files)
//----------------------- "vector_set.h"
//pre declarations.
template <typename T> class Vector_Set;
template <typename T> Vector_Set<T> operator+ (const Vector_Set<T>& s1,
const Vector_Set<T>& s2);
//begin class itself
template <typename T> class Vector_Set
{
friend Vector_Set<T> operator+ (const Vector_Set<T>& s1, const
Vector_Set<T>& s2);
public:
...
};
// friends implementation
template <typename T> Vector_Set<T> operator+ (const Vector_Set<T>& s1,
const Vector_Set<T>& s2)
{
...
}
//----------------------- "vector_set_main.cpp"
typedef Vector_Set<int> Set;
Set a = Set(10), b = Set(10);
for(int i = 0; i < a.size(); i++)
if(i % 2 == 1)
a.add_member(i);
else
b.add_member(i);
// Here causes error!
a + b;
warnings and errors:
g++ -c vector_set_main.cpp
In file included from vector_set_main.cpp:8:
vector_set.h:29: warning: friend declaration `Vector_Set<T> operator+(const
Vector_Set<T>&, const Vector_Set<T>&)'' declares a non-template function
g++ -g vector_set_main.o -O2 -lm -o vector_set_main
vector_set_main.o(.text+0x172): In function `main'':
: undefined reference to `operator+(Vector_Set<int> const&, Vector_Set<int> const&)''
does this help?
--
William Xuuu
>模板< typename T> class Vector_Set
> template <typename T> class Vector_Set
{
朋友Vector_Set< T> operator +(const Vector_Set< T>& s1,const
Vector_Set< T>& s2);
这是一个非模板功能。它声明(作为朋友)这个函数
Vector_Set< int> operator +(const Vector_Set< int>& s1,const Vector_Set< int>&
s2);
typedef Vector_Set< int>设置;
设置a = Set(10),b = Set(10);
for(int i = 0; i< a.size(); i ++)
if(i%2 == 1)
a.add_member(i);
b.add_member(i);
//这里导致错误!
a + b;
{
friend Vector_Set<T> operator+ (const Vector_Set<T>& s1, const
Vector_Set<T>& s2);
This is a non-template function. It declares (as a friend) this function
Vector_Set<int> operator+ (const Vector_Set<int>& s1, const Vector_Set<int>&
s2);
typedef Vector_Set<int> Set;
Set a = Set(10), b = Set(10);
for(int i = 0; i < a.size(); i++)
if(i % 2 == 1)
a.add_member(i);
else
b.add_member(i);
// Here causes error!
a + b;
现在编译器认为有两个函数可供选择,
非模板(您宣称为朋友)
Vector_Set< int> operator +(const Vector_Set< int>& s1,const Vector_Set< int>&
s2);
和模板(你预先声明的) )
模板< class T>
Vector_Set< T> operator +(const Vector_Set< T>& s1,const Vector_Set< T>& s2);
给定此选项,编译器始终优先选择非模板。但是因为
你没有定义一个非模板你会得到一个链接错误。
答案是告诉编译器朋友声明引用到一个
模板函数。您可以通过在运算符+之后添加''< T>''或''<>''来实现这一点。
模板< typename T> class Vector_Set
{
朋友Vector_Set< T> operator +<>(const Vector_Set< T>& s1,const
Vector_Set< T>& s2);
这很容易忘记你可以使模板和非模板版本的
具有相同的功能。
john
Now the compiler thinks there are two functions to choose from, the
non-template (which you declared as a friend)
Vector_Set<int> operator+ (const Vector_Set<int>& s1, const Vector_Set<int>&
s2);
and the template (which you pre-declared)
template <class T>
Vector_Set<T> operator+ (const Vector_Set<T>& s1, const Vector_Set<T>& s2);
Given this choice the compiler always prefers the non-template. But since
you didn''t define a non-template you get a link error.
The answer is to tell the compiler that the friend declaration refers to a
template function. You do that by adding ''<T>'' or ''<>'' after operator+.
template <typename T> class Vector_Set
{
friend Vector_Set<T> operator+ <>(const Vector_Set<T>& s1, const
Vector_Set<T>& s2);
It''s easy to forget that you can have template and non-template versions of
the same function.
john
这篇关于我使用模板朋友时链接器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!