GCC上的基本模板有问题 [英] Problem with basic templates on GCC

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

问题描述



如果这个应该转发给另一个团体,请告诉我......

我已经使用模板几周了已经能够使用Forte C ++编译器(版本7)在solaris上开发一些不错的代码。

然而,当我<时,与模板无关的任何内容似乎都没有正确编译br />
在netbsd上使用g ++。我无法判断它是否是:我的代码出现问题,NetBSD出现问题,或者是GCC问题。我试图创建

最基本的测试来说明问题。有人可以告诉我为什么

使用Forte编译器进行编译和运行,但是没有使用g ++?


MyTemp.h

--------------------------

#include< iostream>

模板< T类> class MyTemp

{

private:

T temp;


public:

MyTemp(T temp1);

void print();

};

MyTemp.cpp

----------------------------------------

#include" MyTemp.h"


template< class T> MyTemp< T> :: MyTemp(T temp1)

{

temp = temp1;

}

template< ; T类> void MyTemp< T> :: print()

{

cout<< temp;

}


Test.cpp

----------------------------------- -------

#include< unistd.h>

#include< iostream>

#include< string> ;

#include< MyTemp.h>

使用命名空间std;

int main()

{

cout<<" In Test"<< endl;

MyTemp< string> mt(TEST);

mt.print();

返回0;

}


---------------------------------------------- --------

g ++ -c -I./ -g -D_DEBUG -Wall -Wno-parentheses -c * .cpp

g ++ * .o -o测试

/usr/lib/libstdc++.so:警告:引用兼容性vfork();包括

< unistd.h>正确参考

Test.o:在函数`main'':

/arpa/ag/d//Test/Test.cpp:7:未定义引用

`MyTemp< basic_string< char,string_char_traits< char>,

__default_alloc_template< false,0> > > :: MyTemp(basic_string< char,

string_char_traits< char> ;,__default_alloc_template< false,0>>)''

/ arpa / ag / d // Test / Test.cpp:7:未定义的引用

`MyTemp< basic_string< char,string_char_traits< char>,

__default_alloc_template< false,0> > > :: MyTemp(basic_string< char,

string_char_traits< char> ;,__default_alloc_template< false,0>>)''

/ arpa / ag / d // Test / Test.cpp:8:未定义的引用

`MyTemp< basic_string< char,string_char_traits< char>,

__default_alloc_template< false,0> > > :: print(void)''

/arpa/ag/d//Test/Test.cpp:8:未定义的引用

`MyTemp< basic_string< char,string_char_traits< char>,

__default_alloc_template< false,0> > > :: print(void)''

***错误代码1


停止。

Hi,
If this should be directed to another group, please let me know...
I''ve been working with templates for a few weeks and have been able to
develop some nice code on solaris using the Forte C++ compiler (version 7).
However, nothing related to templates seems to be compiling correctly when I
use g++ on netbsd. I can''t tell if it is either: a problem with my code, a
problem with NetBSD, or a problem with GCC. I have tried to create the
most basic of tests to illustrate the problem. Could someone tell me why
this compiles and runs fine using the Forte compiler but does not with g++?

MyTemp.h
--------------------------
#include <iostream>
template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};
MyTemp.cpp
----------------------------------------
#include "MyTemp.h"

template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
cout<<temp;
}

Test.cpp
------------------------------------------
#include <unistd.h>
#include <iostream>
#include <string>
#include <MyTemp.h>
using namespace std;
int main()
{
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

------------------------------------------------------
g++ -c -I./ -g -D_DEBUG -Wall -Wno-parentheses -c *.cpp
g++ *.o -o Test
/usr/lib/libstdc++.so: warning: reference to compatibility vfork(); include
<unistd.h> for correct reference
Test.o: In function `main'':
/arpa/ag/d//Test/Test.cpp:7: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
string_char_traits<char>, __default_alloc_template<false, 0> >)''
/arpa/ag/d//Test/Test.cpp:7: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
string_char_traits<char>, __default_alloc_template<false, 0> >)''
/arpa/ag/d//Test/Test.cpp:8: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::print(void)''
/arpa/ag/d//Test/Test.cpp:8: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::print(void)''
*** Error code 1

Stop.

推荐答案

Winbatch写道:
Winbatch wrote:

如果这个应该指向另一个团体,请告诉我......
我已经已经使用模板工作了几个星期,并且能够使用Forte C ++编译器(版本7)在solaris上开发一些很好的代码。
然而,当我使用时,与模板无关的任何内容似乎都没有正确编译。
在netbsd上使用g ++。我无法判断它是否是:我的代码出现问题,NetBSD出现问题,或者GCC出现问题。我试图创建最基本的测试来说明问题。有人可以告诉我为什么
这个使用Forte编译器编译并运行良好但是没有使用g ++?

MyTemp.h
----------- ---------------
#include< iostream>
模板< class T> MyTemp课程
{
私人:
T temp;

公开:
MyTemp(T temp1);
void print();

};
MyTemp.cpp
------------------------------ ----------
#include" MyTemp.h"

模板< class T> MyTemp< T> :: MyTemp(T temp1)
{
temp = temp1;
}
模板< class T> void MyTemp< T> :: print()
{


std :: cout<< temp;

}

Test.cpp
---------------------------------------- -



//这是什么? #include< unistd.h>

#include< iostream>
#include< string>
#include< MyTemp.h>
使用命名空间std;
int main()
{
cout<<"在测试中<<< endl;
MyTemp< string> mt(" TEST");
mt.print();
返回0;
}

------------ ------------------------------------------
g ++ -c - I. / -g -D_DEBUG -Wall -Wno-parentheses -c * .cpp
g ++ * .o -o测试
/usr/lib/libstdc++.so:警告:引用兼容性vfork() ;包括
< unistd.h>正确的参考
Test.o:在函数`main'':
/arpa/ag/d//Test/Test.cpp:7:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: MyTemp(basic_string< char,
string_char_traits< char> ;,__default_alloc_template< false,0>>)''
/arpa/ag/d//Test/Test.cpp:7:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: MyTemp(basic_string< char,
string_char_traits< char> ;,__default_alloc_template< false,0>>)''
/arpa/ag/d//Test/Test.cpp:8:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: print(void)''
/arpa/ag/d//Test/Test.cpp:8:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__default_alloc_template< false,0> > > :: print(void)''
***错误代码1
Hi,
If this should be directed to another group, please let me know...
I''ve been working with templates for a few weeks and have been able to
develop some nice code on solaris using the Forte C++ compiler (version 7).
However, nothing related to templates seems to be compiling correctly when I
use g++ on netbsd. I can''t tell if it is either: a problem with my code, a
problem with NetBSD, or a problem with GCC. I have tried to create the
most basic of tests to illustrate the problem. Could someone tell me why
this compiles and runs fine using the Forte compiler but does not with g++?

MyTemp.h
--------------------------
#include <iostream>
template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};
MyTemp.cpp
----------------------------------------
#include "MyTemp.h"

template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
std::cout<<temp;
}

Test.cpp
------------------------------------------

// What''s this? #include <unistd.h>
#include <iostream>
#include <string>
#include <MyTemp.h>
using namespace std;
int main()
{
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

------------------------------------------------------
g++ -c -I./ -g -D_DEBUG -Wall -Wno-parentheses -c *.cpp
g++ *.o -o Test
/usr/lib/libstdc++.so: warning: reference to compatibility vfork(); include
<unistd.h> for correct reference
Test.o: In function `main'':
/arpa/ag/d//Test/Test.cpp:7: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
string_char_traits<char>, __default_alloc_template<false, 0> >)''
/arpa/ag/d//Test/Test.cpp:7: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
string_char_traits<char>, __default_alloc_template<false, 0> >)''
/arpa/ag/d//Test/Test.cpp:8: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::print(void)''
/arpa/ag/d//Test/Test.cpp:8: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::print(void)''
*** Error code 1




这里用g ++ 3.4.2编译好。你使用的是哪个版本?我的

确切代码:

#include< iostream>

#include< string>

template< ; T类> class MyTemp

{

private:

T temp;


public:

MyTemp(T temp1);

void print();

};

template< class T> MyTemp< T> :: MyTemp(T temp1)

{

temp = temp1;

}

template< ; T类> void MyTemp< T> :: print()

{

std :: cout<< temp;

}

int main()

{

using namespace std;

cout<<" In Test"<< endl ;

MyTemp< string> mt(TEST);

mt.print();

返回0;

}


-

Ioannis Vranos

http://www23.brinkster.com/noicys


问题是在实例化时(MyTemp< string>

mt(" TEST");,Test.cpp)编译器只看到模板类的声明

成员函数(来自MyTemp.h文件)而没有它们的定义。

您应该将MyTemp.cpp #include到Test.cpp文件中。因为对于模板

类所有成员的定义(不仅是声明)必须在

中显示实例化点。

所以你应该改变

#include< MyTemp.h>



#include" MyTemp.cpp"

in Test.cpp文件。


但#include''ing cpp文件看起来很难看。

我建议你按如下方式重写代码:


MyTemp.h

--------------------------

#include< iostream>

#include" MyTemp.hpp"


模板< class T> class MyTemp

{

private:

T temp;


public:

MyTemp(T temp1);

void print();

};


MyTemp.hpp hpp是常用的模板扩展

实现文件

--------------------------- -------------

模板< class T> MyTemp< T> :: MyTemp(T temp1)

{

temp = temp1;

}

template< ; T类> void MyTemp< T> :: print()

{

cout<< temp;

}


Test.cpp

----------------------------------- -------

#include< unistd.h>

#include< iostream>

#include< string> ;

#include" MyTemp.h"

using namespace std;

int main()

{

cout<<" In Test"<< endl;

MyTemp< string> mt(TEST);

mt.print();

返回0;

}


关于C ++模板有一本非常好的书C ++模板:完整的

指南作者:David Vandevoorde,Nicolai M. Josuttis,ISBN:0-201-73484-2


祝你好运,

HappyHippy

" Winbatch" <无线****** @ techie.com>在消息中写道

news:nd ********************* @ twister.nyc.rr.com ...
The problem is that at the point of instantiation (MyTemp<string>
mt("TEST");, Test.cpp) compiler sees only declaration of template class
member functions (from MyTemp.h file) and no their definitions.
You should #include MyTemp.cpp into Test.cpp file. Because for template
classes definions (not only declarations) of all members must be visible at
the point of instantiation.
So you should change
#include <MyTemp.h>
to
#include "MyTemp.cpp"
in Test.cpp file.

But #include ''ing cpp file looks ugly.
I would advise you to rewrite your code as follows:

MyTemp.h
--------------------------
#include <iostream>
#include "MyTemp.hpp"

template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};

MyTemp.hpp hpp is commonly used extension for templates
implementation files
----------------------------------------
template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
cout<<temp;
}

Test.cpp
------------------------------------------
#include <unistd.h>
#include <iostream>
#include <string>
#include "MyTemp.h"
using namespace std;
int main()
{
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

There is a VERY GOOD book on C++ templates "C++ Templates: The Complete
Guide" by David Vandevoorde, Nicolai M. Josuttis, ISBN : 0-201-73484-2

Good Luck,
HappyHippy
"Winbatch" <wi******@techie.com> wrote in message
news:nd*********************@twister.nyc.rr.com...

如果这个应该转发给另一个小组,请告诉我......
我已经使用模板几周了,并且能够
使用Forte C ++编译器(版本
7)在solaris上开发一些不错的代码。但是,当我在netbsd上使用g ++时,与模板无关的任何内容似乎都无法正确编译。我无法判断它是否是:我的代码存在问题,NetBSD出现问题或GCC出现问题。我试图创建最基本的测试来说明问题。有人可以告诉我为什么使用Forte编译器编译并运行良好,但不是用g ++吗?

MyTemp.h
------- -------------------
#include< iostream>
模板< class T> MyTemp课程
{
私人:
T temp;

公开:
MyTemp(T temp1);
void print();

};
MyTemp.cpp
------------------------------ ----------
#include" MyTemp.h"

模板< class T> MyTemp< T> :: MyTemp(T temp1)
{
temp = temp1;
}
模板< class T> void MyTemp< T> :: print()
{
cout<< temp;
}

Test.cpp
----- -------------------------------------
#include< unistd.h>
#include< iostream>
#include< string>
#include< MyTemp.h>
使用命名空间std;
int main()
{
cout<<" In Test"<<< endl;
MyTemp< string> mt(" TEST");
mt.print();
返回0;
}

------------ ------------------------------------------
g ++ -c - I. / -g -D_DEBUG -Wall -Wno-parentheses -c * .cpp
g ++ * .o -o测试
/usr/lib/libstdc++.so:警告:引用兼容性vfork() ;
包括< unistd.h>正确的参考
Test.o:在函数`main'':
/arpa/ag/d//Test/Test.cpp:7:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: MyTemp(basic_string< char,
string_char_traits< char> ;,__default_alloc_template< false,0>>)''
/arpa/ag/d//Test/Test.cpp:7:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: MyTemp(basic_string< char,
string_char_traits< char> ;,__default_alloc_template< false,0>>)''
/arpa/ag/d//Test/Test.cpp:8:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: print(void)''
/arpa/ag/d//Test/Test.cpp:8:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__default_alloc_template< false,0> > > :: print(void)''
***错误代码1

停止。
Hi,
If this should be directed to another group, please let me know...
I''ve been working with templates for a few weeks and have been able to
develop some nice code on solaris using the Forte C++ compiler (version
7). However, nothing related to templates seems to be compiling correctly
when I use g++ on netbsd. I can''t tell if it is either: a problem with my
code, a problem with NetBSD, or a problem with GCC. I have tried to
create the most basic of tests to illustrate the problem. Could someone
tell me why this compiles and runs fine using the Forte compiler but does
not with g++?

MyTemp.h
--------------------------
#include <iostream>
template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};
MyTemp.cpp
----------------------------------------
#include "MyTemp.h"

template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
cout<<temp;
}

Test.cpp
------------------------------------------
#include <unistd.h>
#include <iostream>
#include <string>
#include <MyTemp.h>
using namespace std;
int main()
{
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

------------------------------------------------------
g++ -c -I./ -g -D_DEBUG -Wall -Wno-parentheses -c *.cpp
g++ *.o -o Test
/usr/lib/libstdc++.so: warning: reference to compatibility vfork();
include <unistd.h> for correct reference
Test.o: In function `main'':
/arpa/ag/d//Test/Test.cpp:7: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
string_char_traits<char>, __default_alloc_template<false, 0> >)''
/arpa/ag/d//Test/Test.cpp:7: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
string_char_traits<char>, __default_alloc_template<false, 0> >)''
/arpa/ag/d//Test/Test.cpp:8: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::print(void)''
/arpa/ag/d//Test/Test.cpp:8: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::print(void)''
*** Error code 1

Stop.





" HappyHippy" < KA ****** @ mail.ru>在留言中写道

news:d1 ********** @ charm.magnus.acs.ohio-state.edu ...

"HappyHippy" <ka******@mail.ru> wrote in message
news:d1**********@charm.magnus.acs.ohio-state.edu...
问题是在实例化时(MyTemp< string>
mt(" TEST");),Test.cpp)编译器只看到模板类
成员函数的声明(来自MyTemp.h文件)而没有他们的定义。
你应该将MyTemp.cpp #include到Test.cpp文件中。因为对于模板
类所有成员的定义(不仅是声明)必须在实例化时可见。
所以你应该改变
#include< MyTemp.h>
在Test.cpp文件中#include" MyTemp.cpp"


但是#include''ing cpp文件看起来很难看。
我建议你按如下方式重写你的代码:

MyTemp.h
------------------------ -
#include< iostream>
#include" MyTemp.hpp"

模板< class T> MyTemp课程
{
私人:
T temp;

公开:
MyTemp(T temp1);
void print();

};

MyTemp.hpp hpp常用于模板的扩展
实现文件
------------- ---------------------------
模板< class T> MyTemp< T> :: MyTemp(T temp1)
{
temp = temp1;
}
模板< class T> void MyTemp< T> :: print()
{
cout<< temp;
}

Test.cpp
----- -------------------------------------
#include< unistd.h>
#include< iostream>
#include< string>
#include" MyTemp.h"
使用命名空间std;
int main()
{
cout<<" In Test"<<< endl;
MyTemp< string> mt(TEST);
mt.print();
返回0;
}

有一本关于C ++模板的非常好的书C ++模板:完整的
指南作者:David Vandevoorde,Nicolai M. Josuttis,ISBN:0-201-73484-2
好运,
HappyHippy

" Winbatch" <无线****** @ techie.com>在消息中写道
新闻:nd ********************* @ twister.nyc.rr.com ...
The problem is that at the point of instantiation (MyTemp<string>
mt("TEST");, Test.cpp) compiler sees only declaration of template class
member functions (from MyTemp.h file) and no their definitions.
You should #include MyTemp.cpp into Test.cpp file. Because for template
classes definions (not only declarations) of all members must be visible
at the point of instantiation.
So you should change
#include <MyTemp.h>
to
#include "MyTemp.cpp"
in Test.cpp file.

But #include ''ing cpp file looks ugly.
I would advise you to rewrite your code as follows:

MyTemp.h
--------------------------
#include <iostream>
#include "MyTemp.hpp"

template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};

MyTemp.hpp hpp is commonly used extension for templates
implementation files
----------------------------------------
template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
cout<<temp;
}

Test.cpp
------------------------------------------
#include <unistd.h>
#include <iostream>
#include <string>
#include "MyTemp.h"
using namespace std;
int main()
{
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

There is a VERY GOOD book on C++ templates "C++ Templates: The Complete
Guide" by David Vandevoorde, Nicolai M. Josuttis, ISBN : 0-201-73484-2

Good Luck,
HappyHippy
"Winbatch" <wi******@techie.com> wrote in message
news:nd*********************@twister.nyc.rr.com...

如果这个应该针对另一个小组,请告诉我......
我已经使用模板几周了,并且能够开发一些不错的使用Forte C ++编译器(版本
7)上的solaris代码。但是,当我在netbsd上使用g ++时,与模板无关的任何内容似乎都无法正确编译。我无法判断它是否是:我的代码出现问题,NetBSD出现问题或GCC出现问题。我试图创建最基本的测试来说明问题。有人可以告诉我为什么使用Forte编译器编译并运行良好,但不是用g ++吗?

MyTemp.h
------- -------------------
#include< iostream>
模板< class T> MyTemp课程
{
私人:
T temp;

公开:
MyTemp(T temp1);
void print();

};
MyTemp.cpp
------------------------------ ----------
#include" MyTemp.h"

模板< class T> MyTemp< T> :: MyTemp(T temp1)
{
temp = temp1;
}
模板< class T> void MyTemp< T> :: print()
{
cout<< temp;
}

Test.cpp
----- -------------------------------------
#include< unistd.h>
#include< iostream>
#include< string>
#include< MyTemp.h>
使用命名空间std;
int main()
{
cout<<" In Test"<<< endl;
MyTemp< string> mt(" TEST");
mt.print();
返回0;
}

------------ ------------------------------------------
g ++ -c - I. / -g -D_DEBUG -Wall -Wno-parentheses -c * .cpp
g ++ * .o -o测试
/usr/lib/libstdc++.so:警告:引用兼容性vfork() ;
包括< unistd.h>正确的参考
Test.o:在函数`main'':
/arpa/ag/d//Test/Test.cpp:7:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: MyTemp(basic_string< char,
string_char_traits< char> ;,__default_alloc_template< false,0>>)''
/arpa/ag/d//Test/Test.cpp:7:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: MyTemp(basic_string< char,
string_char_traits< char> ;,__default_alloc_template< false,0>>)''
/arpa/ag/d//Test/Test.cpp:8:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__ default_alloc_template< false,0> > > :: print(void)''
/arpa/ag/d//Test/Test.cpp:8:未定义的引用
`MyTemp< basic_string< char,string_char_traits< char>,
__default_alloc_template< false,0> > > :: print(void)''
***错误代码1

停止。
Hi,
If this should be directed to another group, please let me know...
I''ve been working with templates for a few weeks and have been able to
develop some nice code on solaris using the Forte C++ compiler (version
7). However, nothing related to templates seems to be compiling correctly
when I use g++ on netbsd. I can''t tell if it is either: a problem with
my code, a problem with NetBSD, or a problem with GCC. I have tried to
create the most basic of tests to illustrate the problem. Could someone
tell me why this compiles and runs fine using the Forte compiler but does
not with g++?

MyTemp.h
--------------------------
#include <iostream>
template <class T> class MyTemp
{
private:
T temp;

public:
MyTemp (T temp1);
void print();
};
MyTemp.cpp
----------------------------------------
#include "MyTemp.h"

template<class T> MyTemp<T>::MyTemp( T temp1 )
{
temp = temp1;
}
template<class T> void MyTemp<T>::print()
{
cout<<temp;
}

Test.cpp
------------------------------------------
#include <unistd.h>
#include <iostream>
#include <string>
#include <MyTemp.h>
using namespace std;
int main()
{
cout<<"In Test"<<endl;
MyTemp<string> mt("TEST");
mt.print();
return 0;
}

------------------------------------------------------
g++ -c -I./ -g -D_DEBUG -Wall -Wno-parentheses -c *.cpp
g++ *.o -o Test
/usr/lib/libstdc++.so: warning: reference to compatibility vfork();
include <unistd.h> for correct reference
Test.o: In function `main'':
/arpa/ag/d//Test/Test.cpp:7: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
string_char_traits<char>, __default_alloc_template<false, 0> >)''
/arpa/ag/d//Test/Test.cpp:7: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::MyTemp(basic_string<char,
string_char_traits<char>, __default_alloc_template<false, 0> >)''
/arpa/ag/d//Test/Test.cpp:8: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::print(void)''
/arpa/ag/d//Test/Test.cpp:8: undefined reference to
`MyTemp<basic_string<char, string_char_traits<char>,
__default_alloc_template<false, 0> > >::print(void)''
*** Error code 1

Stop.




HH,

谢谢,我会试一试。为什么它会在某些版本的gcc中起作用(如Ioannis报告的那样),但其他版本没有?它后来成为了

标准的一部分吗?


HH,
Thanks, I will try it. Why would it work in some versions of gcc (as
reported by Ioannis), but not by others? Did it later become part of the
standard?


这篇关于GCC上的基本模板有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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