question about templates

P

pplppp

Hi,
I have a simple program which I wrote and compiled on SCO Unixware
7.1.1 (compiler is CC) with SGI STL and it compiled and worked fine.
Now I'm using GNU GCC 4.0.0 on linux, with the GCC implementation of
STL and I have some trouble trying compile the same code:
the following is a segemnt of my program:


[StringUtil.h]
....
#include <string>
using namespace std;
....
template<typename T>basic_string<T> trimCopy(const basic_string<T>&
str);
....

[StringUtil.cpp]
....
template<typename T>basic_string<T> trimCopy(const basic_string<T>&
str)
{
basic_string<T>::size_type front = str.find_first_not_of(" \t\n");
// line 97
basic_string<T>::size_type end = str.find_last_not_of(" \t\n"); //
line 98
return str.substr(front, end-front+1); // line 99
}
....

when I try to compile the program, g++ gives me an error

/usr/local/bin/g++ -g -frepo -O -I. -I/usr/local/include -c
StringUtil.cpp
StringUtil.cpp: In function 'std::basic_string<T,
std::allocator<_CharT> > trimCopy(const said:
StringUtil.cpp:97: error: expected `;' before 'front'
StringUtil.cpp:98: error: expected `;' before 'end'
StringUtil.cpp:99: error: 'front' was not declared in this scope
StringUtil.cpp:99: error: 'end' was not declared in this scope


I would like to know why I'm getting this error, since with the SGI STL
implementation and SCO Unixware CC compiler I have no problem with the
same piece of code. Any help is appreciated

Thanks in advance

newbie
 
V

Victor Bazarov

I have a simple program which I wrote and compiled on SCO Unixware
7.1.1 (compiler is CC) with SGI STL and it compiled and worked fine.
Now I'm using GNU GCC 4.0.0 on linux, with the GCC implementation of
STL and I have some trouble trying compile the same code:
the following is a segemnt of my program:


[StringUtil.h]
...
#include <string>
using namespace std;
...
template<typename T>basic_string<T> trimCopy(const basic_string<T>&
str);
...

[StringUtil.cpp]
...
template<typename T>basic_string<T> trimCopy(const basic_string<T>&
str)
{

Add keyword 'typename' here
basic_string<T>::size_type front = str.find_first_not_of(" \t\n");
// line 97

Add keyword 'typename' here too
basic_string<T>::size_type end = str.find_last_not_of(" \t\n"); //
line 98
return str.substr(front, end-front+1); // line 99
}
...

Both 'front' and 'end' are declared to have a _dependent_type_. It is
dependent on the meaning of 'T'. To help the compiler decide what you
mean there, you need to say

typename basic_string said:
when I try to compile the program, g++ gives me an error

/usr/local/bin/g++ -g -frepo -O -I. -I/usr/local/include -c
StringUtil.cpp
StringUtil.cpp: In function 'std::basic_string<T,


StringUtil.cpp:97: error: expected `;' before 'front'
StringUtil.cpp:98: error: expected `;' before 'end'
StringUtil.cpp:99: error: 'front' was not declared in this scope
StringUtil.cpp:99: error: 'end' was not declared in this scope


I would like to know why I'm getting this error, since with the SGI STL
implementation and SCO Unixware CC compiler I have no problem with the
same piece of code. Any help is appreciated

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top