static data members in template class problem

M

mati

Hi,
Following code gives a linker error:

test.obj : error LNK2001: unresolved external symbol "public: static int
const BresenhamCircle<2>::SIZE"


template <int R>
class BresenhamCircle
{
public:
static const int SIZE;
};

template <int R>
class Test
{
BresenhamCircle<R> circle;
public:
int testfunction();//[1] { return circle.SIZE+1000*R; }
};

template<int R>
int Test<R>::testfunction()
{
return circle.SIZE+1000*R;
}

template<int R> const int BresenhamCircle<R>::SIZE = -1;
const int BresenhamCircle<4>::SIZE = 4;

#include <iostream>
int main()
{
Test<2> obj;
//[2] Test<4> obj;
std::cout<<obj.testfunction()<<std::endl;
}

And two interesting things happen:
[1] - code compiles and links without errors when we make the
testfunction() inline.
[2] - all is ok for specialized versions;

I'm learning c++, and I have no idea if I'm wrong, or the compiler has a
bug, but I suppose the latter since gcc(3.3.6) compiles this code just
fine (or maybe it is UB?). I do not understand why the behaviour is
different for inline and non-inline functions.

I'm using
Microsoft Visual Studio 2005 (VC Express)
Version 8.0.50727.42 (RTM.050727-4200)
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,
Following code gives a linker error:

test.obj : error LNK2001: unresolved external symbol "public: static int
const BresenhamCircle<2>::SIZE"


template <int R>
class BresenhamCircle
{
public:
static const int SIZE;
};

template <int R>
class Test
{
BresenhamCircle<R> circle;
public:
int testfunction();//[1] { return circle.SIZE+1000*R; }

{ return BresenhamCircle said:
};

template<int R>
int Test<R>::testfunction()
{
return circle.SIZE+1000*R;

return BresenhamCircle said:
}

template<int R> const int BresenhamCircle<R>::SIZE = -1;
const int BresenhamCircle<4>::SIZE = 4;

template said:
#include <iostream>
int main()
{
Test<2> obj;
//[2] Test<4> obj;
std::cout<<obj.testfunction()<<std::endl;
}

And two interesting things happen:
[1] - code compiles and links without errors when we make the
testfunction() inline.
[2] - all is ok for specialized versions;

I'm learning c++, and I have no idea if I'm wrong, or the compiler has a
bug, but I suppose the latter since gcc(3.3.6) compiles this code just
fine (or maybe it is UB?). I do not understand why the behaviour is
different for inline and non-inline functions.

Might be a bug, or just gcc allowing the user to get away with faulty
code, did out use the -std=c++89 -pedantic options? Anyway, SIZE being a
static member you can access it through the class name instead of the
instance.
 
M

mati

Erik said:
return BresenhamCircle<R>::SIZE+1000*R;

That made things work.
template<> const int BresenhamCircle<4>::SIZE = 4;

Sure.


Might be a bug, or just gcc allowing the user to get away with faulty
code, did out use the -std=c++89 -pedantic options? Anyway, SIZE being a

You mean "-std=c++98 -pedantic"? Tested and compiles just fine.
static member you can access it through the class name instead of the
instance.

Sure. But is my way is also correct?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

That made things work.


You mean "-std=c++98 -pedantic"? Tested and compiles just fine.


Sure. But is my way is also correct?

Yes, but for some reason the lookup failed for you, I should point out
that your code (after fixing the template) works in VC++ 2008 beta, so
it's probably a bug in VC++ 2005.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top