问题描述
如果这个应该转发给另一个团体,请告诉我......
我已经使用模板几周了已经能够使用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.
推荐答案
这里用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
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上的基本模板有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!