tags:

views:

207

answers:

5

Hello!

I've got this code to port from windows to linux.

template<class T, int Size> 
class CVector {
 /* ... */
};

template<int n, int m>
class CTestClass {
public:
 enum { Size = 1 << n };
private:
 static CVector<int, Size> a; // main.cpp:19
};

template<int n, int m>
CVector<int, CTestClass<n, m>::Size> CTestClass<n, m>::a; // main.cpp:24

It compiles with VS2008, but doesn't with g++ 4.3.2. The error I receive is:

main.cpp:24: error: conflicting declaration ‘CVector CTestClass::alpha_to’

main.cpp:19: error: ‘CTestClass< n, m >::alpha_to’ has a previous declaration as ‘CVector< int, CTestClass< n, m >::Size > CTestClass< n, m >::alpha_to’

main.cpp:24: error: declaration of ‘CVector< int, CTestClass< n, m >::Size > CTestClass< n, m >::alpha_to’ outside of class is not definition

Does someone know how to make it compilable via g++?

Thanks!

A: 

If you take out the declaration of a outside the class, it compiles on cygwin with g++ v. 3.4.4.

By the way, why not use std::vector instead of CVector?

Dima
No I dont understand why he doesn't even need to initialize the static member. Could you explain a bit?
Prasoon Saurav
He is not initializing it, because he is not giving it any value. He is just declaring it.
Dima
Actually CTestClass::a does depends on template parameter 'n'. Look carefully to the code.why not use std::vector instead of CVector?It's not my code, but suppose there were some reasons. And in fact it doesn't matters - code won't compile with std::vector either.
Alex
How to delete an answer? I've posted text to answer instead of comment, now wont to delete answer.
Alex
A: 

Actually CTestClass::a does depends on template parameter 'n'. Look carefully to the code.

why not use std::vector instead of CVector?

It's not my code, but suppose there were some reasons. And in fact it doesn't matters - code won't compile with std::vector either.

Alex
You are right. Sorry, I missed it. But try taking out the declaration on line 24. I've just tried it, and it compiles with g++ on cygwin.
Dima
And does functionality of this code will be saved in this case?
Alex
Yeah there wont be any problem at the time of compilation(if he deletes that line) but his program wont link then.
Prasoon Saurav
A: 

In CTestClass, Size is an enum and not an int (by standard it's implementation dependant, enum doesnt have to be an integer). Try using a static const int Size = ...

rmn
does not work.got same error.
Alex
A: 

I think problem is with enum.

http://www2.research.att.com/~bs/C++0xFAQ.html#enumlink text

Ashish
I had a similar thought, but I changed size to a const int and still got the same error as the asker (at least on g++ 4.1.2)
Brian Young
+6  A: 

This works with gcc 3.4 & 4.3 as well as VC8:

template<class T, int Size> 
class CVector {
 /* ... */
};

template<int n, int m>
class CTestClass {
public:
    enum { Size = 1 << n };
    typedef CVector<int, Size> Vector;
private:
    static Vector a; 
};

template<int n, int m>
typename CTestClass<n,m>::Vector CTestClass<n,m>::a;
Georg Fritzsche
Seems to be solution to the problem.Thanks a lot!
Alex