链接器错误与模板 [英] linker errors with templates

查看:60
本文介绍了链接器错误与模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我无法理解为什么我无法编译以下简单的代码,其中

我想我已经应用了所有在不同文件中声明和定义的模板所需的规则(* .h和* .cpp)。


令我惊讶的是,我有已经有一些这样的代码在另一个

项目中,我没有收到任何错误,所以我很确定我错过了一些

愚蠢的事情。
< br $>
//文件templates.h


#ifndef _TMPLT_

#define _TMPLT_


#include< iostream>


模板< typename T>

class my_class;

template< typename T>

std :: ostream&运算符<<(std :: ostream& out,const my_class< T>& obj);


模板< typename T>

class my_class

{

public:

my_class(const T& v);

private:

朋友std :: ostream&运营商LT;< <> (std :: ostream& out,const

my_class< T>& obj);

const T val;

};


#endif


//文件templates.cpp


#include" templates.h"


模板类my_class< int> ;;


模板< typename T>

my_class< T>: :my_class(const T& v = T())

:val(v)

{}


template< typename T>

inline std :: ostream& operator<<(std :: ostream& out,const my_class< T>

& obj)

{

return out< ;< obj.val<< ''\ n'';

}


//文件main.cpp


#include" ; templates.h"


int main()

{

my_class< int> h1(99);

std :: cout<< h1;

返回0;

}


我用gcc-3.4.1编译它:


我自己@myhost /临时#g ++ -c templates.cpp -o templates.o -Wall

我自己@myhost /临时#g ++ main.cpp templates.o -Wall

/tmp/ccx0vMtF.o(.text+0x144):在函数main中:

:对`std :: basic_ostream< char,
的未定义引用
std :: char_traits< char> >&安培;运营商LT;< < int>(std :: basic_ostream< char,

std :: char_traits< char>>&,my_class< int> const&)''

collect2 :ld返回1退出状态


提前感谢所有指出代码错误的人。

Ciao,


Fabio De Francesco

解决方案

Fabio De Francesco写道:

我不能理解为什么我不能编译以下简单代码,其中我认为我已经为在不同文件中声明和定义的模板应用了所有必需的规则(* .h和* .cpp) 。
[...]




这在常见问题解答中有介绍。 http://www.parashift.com/c++-faq-lite/


V


由于我英语不好,也许我的帖子不清楚......


我已经阅读了FAQ,这就是问题所在。我所说的是我

有一些其他类似的代码,正如常见问题解答所说的那样遵循相同的

规则。


你能仔细看看代码吗?

谢谢你,

Fabio De Francesco


Fabio De Francesco写道:

由于我的英语不好,也许我的帖子不清楚......

我已经读过了常见问题,这就是问题所在。我所说的是,我有一些其他类似的代码遵循与常见问题解答相同的规则。




大多数今天的编译器,你需要在类''定义中定义一个类

模板'的成员函数,而不是在一个

实现文件中。请密切关注
http://www.parashift.com/c++-faq-lit...html#faq-34.13

及以下内容。当你在它的时候,浏览整个FAQ。


如果你的代码已经有效,那就意味着你使用的编译器是

可能能够实现export关键字。目前,afaik,只有

Comeau确实。

Jonathan


Hello.

I can''t understand why I can''t compile the following simple code, where
I think I have applied all the needed rules for templates that are
declared and defined in different files (*.h and *.cpp).

What amazes me is that I have already some code like this in another
project where I don''t get errors, so I am pretty sure I am missing some
stupid thing.

// file templates.h

#ifndef _TMPLT_
#define _TMPLT_

#include <iostream>

template <typename T>
class my_class;
template <typename T>
std::ostream& operator<<( std::ostream& out, const my_class<T>& obj );

template <typename T>
class my_class
{
public:
my_class( const T& v );
private:
friend std::ostream& operator<< <> (std::ostream& out,const
my_class<T>& obj);
const T val;
};

#endif

// file templates.cpp

#include "templates.h"

template class my_class<int>;

template <typename T>
my_class<T>::my_class( const T& v = T() )
: val( v )
{ }

template <typename T>
inline std::ostream& operator<<( std::ostream &out, const my_class<T>
&obj )
{
return out << obj.val << ''\n'';
}

// file main.cpp

#include "templates.h"

int main()
{
my_class<int> h1(99);
std::cout << h1;
return 0;
}

I compile it with gcc-3.4.1:

myself@myhost /temporary # g++ -c templates.cpp -o templates.o -Wall
myself@myhost /temporary # g++ main.cpp templates.o -Wall
/tmp/ccx0vMtF.o(.text+0x144): In function `main'':
: undefined reference to `std::basic_ostream<char,
std::char_traits<char> >& operator<< <int>(std::basic_ostream<char,
std::char_traits<char> >&, my_class<int> const&)''
collect2: ld returned 1 exit status

Thanks in advance to everyone that points me to the errors in code.
Ciao,

Fabio De Francesco

解决方案

Fabio De Francesco wrote:

I can''t understand why I can''t compile the following simple code, where
I think I have applied all the needed rules for templates that are
declared and defined in different files (*.h and *.cpp).
[...]



This is covered in the FAQ. http://www.parashift.com/c++-faq-lite/

V


Maybe my post wasn''t clear, due to my poor English...

I have already read the FAQ, this is the problem. What I said is that I
have some other similar code that is working by following the same
rules as the FAQ says.

Can you please have a closer look at the code?
Thank you,

Fabio De Francesco


Fabio De Francesco wrote:

Maybe my post wasn''t clear, due to my poor English...

I have already read the FAQ, this is the problem. What I said is that I
have some other similar code that is working by following the same
rules as the FAQ says.



In most compilers today, you need to put the definition of a class
template''s member functions in the class'' definition, *not* in an
implementation file. Please, look closely at
http://www.parashift.com/c++-faq-lit...html#faq-34.13
and the following. And while you''re at it, browse the whole FAQ.

If your code has already worked, it means the compiler you used was
probably able to implement the export keyword. Currently, afaik, only
Comeau does.
Jonathan


这篇关于链接器错误与模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆