Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compiler specific: gcc and templates

Reply
Thread Tools

compiler specific: gcc and templates

 
 
Chameleon
Guest
Posts: n/a
 
      05-02-2006
This code below compiles fine with VS.2005 but with gcc 3.4.2 not.
------------------
template<class T>
static void Wastage1D::clever_erase(vector<T> &v, vector<typename
vector<T>::iterator> &its, vector<T> &vo)
{ ........ }
------------------

I run gcc like this:
gcc -c wastage1d.cpp

The error message of gcc:
------------------
wastage1d.cpp:140: error: cannot declare member function `static void
wastage::Wastage1D::clever_erase(std::vector<T, std::allocator<_CharT>
>&, std::vector<typename std::vector<T, std::allocator<_CharT>
>::iterator, std::allocator<typename std::vector<T,

std::allocator<_CharT> >::iterator> >&, std::vector<T,
std::allocator<_CharT> >&)' to have static linkage
wastage1d.cpp: In static member function `static void
wastage::Wastage1D::clever_erase(std::vector<T, std::allocator<_CharT>
>&, std::vector<typename std::vector<T, std::allocator<_CharT>
>::iterator, std::allocator<typename std::vector<T,

std::allocator<_CharT> >::iterator> >&, std::vector<T,
std::allocator<_CharT> >&)':
------------------

What's wrong?

thanks for your time
 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      05-02-2006
Chameleon wrote:
> This code below compiles fine with VS.2005 but with gcc 3.4.2 not.
> ------------------
> template<class T>
> static void Wastage1D::clever_erase(vector<T> &v, vector<typename
> vector<T>::iterator> &its, vector<T> &vo)
> { ........ }
> ------------------
>
> I run gcc like this:
> gcc -c wastage1d.cpp
>
> The error message of gcc:
> ------------------
> wastage1d.cpp:140: error: cannot declare member function `static void
> wastage::Wastage1D::clever_erase(std::vector<T, std::allocator<_CharT>
> >&, std::vector<typename std::vector<T, std::allocator<_CharT>
> >::iterator, std::allocator<typename std::vector<T,

> std::allocator<_CharT> >::iterator> >&, std::vector<T,
> std::allocator<_CharT> >&)' to have static linkage
> wastage1d.cpp: In static member function `static void
> wastage::Wastage1D::clever_erase(std::vector<T, std::allocator<_CharT>
> >&, std::vector<typename std::vector<T, std::allocator<_CharT>
> >::iterator, std::allocator<typename std::vector<T,

> std::allocator<_CharT> >::iterator> >&, std::vector<T,
> std::allocator<_CharT> >&)':
> ------------------
>
> What's wrong?


It seems like you put 'static' not only on the member function
declaration, but on the definition too. Drop the one on the definition.

class C
{
static void f();
};

void C::f() // <-- no static here
{
}


Jonathan

 
Reply With Quote
 
 
 
 
Chameleon
Guest
Posts: n/a
 
      05-02-2006
Jonathan Mcdougall wrote:
> Chameleon wrote:
>> This code below compiles fine with VS.2005 but with gcc 3.4.2 not.
>> ------------------
>> template<class T>
>> static void Wastage1D::clever_erase(vector<T> &v, vector<typename
>> vector<T>::iterator> &its, vector<T> &vo)
>> { ........ }
>> ------------------
>>
>> I run gcc like this:
>> gcc -c wastage1d.cpp
>>
>> The error message of gcc:
>> ------------------
>> wastage1d.cpp:140: error: cannot declare member function `static void
>> wastage::Wastage1D::clever_erase(std::vector<T, std::allocator<_CharT>
>> >&, std::vector<typename std::vector<T, std::allocator<_CharT>
>> >::iterator, std::allocator<typename std::vector<T,

>> std::allocator<_CharT> >::iterator> >&, std::vector<T,
>> std::allocator<_CharT> >&)' to have static linkage
>> wastage1d.cpp: In static member function `static void
>> wastage::Wastage1D::clever_erase(std::vector<T, std::allocator<_CharT>
>> >&, std::vector<typename std::vector<T, std::allocator<_CharT>
>> >::iterator, std::allocator<typename std::vector<T,

>> std::allocator<_CharT> >::iterator> >&, std::vector<T,
>> std::allocator<_CharT> >&)':
>> ------------------
>>
>> What's wrong?

>
> It seems like you put 'static' not only on the member function
> declaration, but on the definition too. Drop the one on the definition.
>
> class C
> {
> static void f();
> };
>
> void C::f() // <-- no static here
> {
> }


ok and thanks!
but I have one last problem (the same: VS.2005 does, gcc doesn't)
-------------------
template<class T>
void Wastage1D::clever_erase(vector<T> &v, vector<typename
vector<T>::iterator> &its, vector<T> &vo)
{
// ....... code .......
vector<T>::iterator cur = its.front(), curo = its.front();
vector<typename vector<T>::iterator>::iterator itscur = its.begin();
// ....... code .......
}
-------------------

gcc fails to compile with this message:
-------------------
wastage1d.cpp:148: error: expected `;' before "cur"
wastage1d.cpp:149: error: expected `;' before "itscur"
-------------------

how can I put ";" before "cur" or "itscur"?

after all these, I believe, for cross-platform code, its better to use
first gcc (until the completion of the program) and after VS.

thank you
 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      05-02-2006
Chameleon wrote:
> template<class T>
> void Wastage1D::clever_erase(vector<T> &v, vector<typename
> vector<T>::iterator> &its, vector<T> &vo)
> {
> // ....... code .......
> vector<T>::iterator cur = its.front(), curo = its.front();
> vector<typename vector<T>::iterator>::iterator itscur = its.begin();
> // ....... code .......
> }
> -------------------
>
> gcc fails to compile with this message:
> -------------------
> wastage1d.cpp:148: error: expected `;' before "cur"
> wastage1d.cpp:149: error: expected `;' before "itscur"
> -------------------
>
> how can I put ";" before "cur" or "itscur"?




Either you are inattentive or you didn't write part of the code because
the answer is right there :

vector<typename vector<T>::iterator>::iterator itscur
^^^^^^^^^^^

When a name depends on a template parameter, you must use typename to
indicate to the compiler that what follows is a type name and nothing
else. So your code becomes:

typename vector<T>::iterator cur = its.front(), curo = its.front();
typename vector<typename vector<T>::iterator>::iterator itscur =
its.begin();

> after all these, I believe, for cross-platform code, its better to use
> first gcc (until the completion of the program) and after VS.


The more conforming the compiler is, the better, yes.


Jonathan

 
Reply With Quote
 
Chameleon
Guest
Posts: n/a
 
      05-02-2006
simpler sample:

---------------
#include <vector>
using namespace std;

template <class T> class c1 {
public:
static void c() {
vector<T>::iterator f;
}
};

int main() { c1<int> c; }
---------------

if I change for instance the line
vector<T>::iterator f;
with
vector<T> f;
works.

the error message:
--------------
test.cpp:7: error: expected `;' before "f"
--------------
 
Reply With Quote
 
Chameleon
Guest
Posts: n/a
 
      05-02-2006
Jonathan Mcdougall wrote:
> Chameleon wrote:
>> template<class T>
>> void Wastage1D::clever_erase(vector<T> &v, vector<typename
>> vector<T>::iterator> &its, vector<T> &vo)
>> {
>> // ....... code .......
>> vector<T>::iterator cur = its.front(), curo = its.front();
>> vector<typename vector<T>::iterator>::iterator itscur = its.begin();
>> // ....... code .......
>> }
>> -------------------
>>
>> gcc fails to compile with this message:
>> -------------------
>> wastage1d.cpp:148: error: expected `;' before "cur"
>> wastage1d.cpp:149: error: expected `;' before "itscur"
>> -------------------
>>
>> how can I put ";" before "cur" or "itscur"?

>
>
>
> Either you are inattentive or you didn't write part of the code because
> the answer is right there :
>
> vector<typename vector<T>::iterator>::iterator itscur
> ^^^^^^^^^^^
>
> When a name depends on a template parameter, you must use typename to
> indicate to the compiler that what follows is a type name and nothing
> else. So your code becomes:
>
> typename vector<T>::iterator cur = its.front(), curo = its.front();
> typename vector<typename vector<T>::iterator>::iterator itscur =
> its.begin();
>
>> after all these, I believe, for cross-platform code, its better to use
>> first gcc (until the completion of the program) and after VS.

>
> The more conforming the compiler is, the better, yes.


it works! it works!
thanks a lot

Finally, the problem is that, I know only the basics about templates in
C++. The problem in above code, was only a symptom...

I must read.
 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      05-02-2006
Chameleon wrote:
> simpler sample:
>
> ---------------
> #include <vector>
> using namespace std;
>
> template <class T> class c1 {
> public:
> static void c() {
> vector<T>::iterator f;
> }
> };
>
> int main() { c1<int> c; }
> ---------------
>
> if I change for instance the line
> vector<T>::iterator f;
> with
> vector<T> f;
> works.
>
> the error message:
> --------------
> test.cpp:7: error: expected `;' before "f"
> --------------


Is that a question? If so, ask it plainly and remember to quote the
message are answering to.

The problem with the line

vector<T>::iterator f;

is that the compiler, at the time it sees this line, may be unable to
know what the definition of vector<> is for that specific T. Since
templates may be specialized, vector<A>::iterator could be a type and
vector<B>::iterator could be an object:

class A {};
class B {};

template <class T>
class vector;

template<>
class vector<A>
{
public:
typedef int iterator; // just an example
};

template<>
class vector<B>
{
public:
int iterator;
};

template <class T>
void f()
{
vector<T>::iterator itor; // what is 'iterator' here?
}

The language says that in this case, vector<T>::iterator sould be
considered as an object, not a type (it may be either one here,
depending on whether T is A or B). Adding 'typename' in front indicates
that 'iterator' is a type, not an object.

template <class T>
void f()
{
typename vector<T>::iterator itor; // 'iterator' is a type
}

As for using vector<T> directly, it poses no problem to the compiler
since that name must be declared before it is used. It is easy for the
compiler to decide whether it is a type or not.


Jonathan

 
Reply With Quote
 
Larry I Smith
Guest
Posts: n/a
 
      05-02-2006
Chameleon wrote:
> This code below compiles fine with VS.2005 but with gcc 3.4.2 not.
> ------------------
> template<class T>
> static void Wastage1D::clever_erase(vector<T> &v, vector<typename
> vector<T>::iterator> &its, vector<T> &vo)
> { ........ }
> ------------------
>
> I run gcc like this:
> gcc -c wastage1d.cpp
>

<snip>

Use 'gcc' to compile & link C code, and 'g++' to
compile & link C++ code. So...

g++ -c wastage1d.cpp

produces 'wastage1d.o' (or 'wastage1d.obj' on Windows?).


Than later, to link the executable 'wastage' (all on
one line - linux/unix example)...

g++ wastage1d.o other.o another.o -lsomelib -lanotherlib
-owastage

'g++' invokes the linker, passing it the correct options
and default libs. The syntax may differ slightly on Windows,
read the gcc/g++ docs.

Larry
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
friends, templates and comeau, gcc werasm C++ 4 11-30-2009 05:55 PM
GCC 3.4.3 and GCC 4.1.2 ashnin C++ 1 07-07-2008 01:10 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
gcc 2.95 and gcc 3.2 gouqizi.lvcha@gmail.com C++ 8 03-16-2005 02:34 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57