470,619 Members | 1,485 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,619 developers. It's quick & easy.

Problem with static variable in template class

In utility.hpp I have:

namespace utility
{

template <class T, double (T::*F)(double) const>
class Display
{
private:
static double resolution;
static double margin;

public:
static void computePlot(stringstream& ss, const T& t, const
ClosedInterval& interval);
};

}

In utility.cpp:

#include <utility.hpp>
namespace utility
{

template <class T, double (T::*F)(double) const>
double Display<T,F>::resolution = 0.025;

template <class T, double (T::*F)(double) const>
double Display<T,F>::margin = 5;

}

Then I have another cpp file in which I'm including utility.hpp and
calling the computePlot function using:

utility::Display<EnvelopePiece, &EnvelopePiece::f>::computePlot( ss,
*this, interval );

(the *this because I'm inside a member function of the EnvelopePiece
class when I'm calling computePlot)

However when compiling I get:
1>envelope.obj : error LNK2001: unresolved external symbol "private:
static double utility::Display<class EnvelopePiece,&public: double
__thiscall EnvelopePiece::f(double)const >::resolution" (?resolution@?
$Display@VEnvelopePiece@@$1?f@1@QBENN@Z@utility@@0 NA)

Like if the static variables were not declared.
But isn't double Display<T,F>::resolution the proper way to declare
the "generic" template static variable, that I can then specialize if
I want to?

Thanks

StephQ

May 3 '07 #1
3 5515
On May 3, 9:00 pm, StephQ <askmeo...@mailinator.comwrote:
In utility.hpp I have:

namespace utility
{

template <class T, double (T::*F)(double) const>
class Display
{
private:
static double resolution;
static double margin;

public:
static void computePlot(stringstream& ss, const T& t, const
ClosedInterval& interval);

};
}

In utility.cpp:

#include <utility.hpp>
namespace utility
{

template <class T, double (T::*F)(double) const>
double Display<T,F>::resolution = 0.025;

template <class T, double (T::*F)(double) const>
double Display<T,F>::margin = 5;

}

Then I have another cpp file in which I'm including utility.hpp and
calling the computePlot function using:

utility::Display<EnvelopePiece, &EnvelopePiece::f>::computePlot( ss,
*this, interval );

(the *this because I'm inside a member function of the EnvelopePiece
class when I'm calling computePlot)

However when compiling I get:
1>envelope.obj : error LNK2001: unresolved external symbol "private:
static double utility::Display<class EnvelopePiece,&public: double
__thiscall EnvelopePiece::f(double)const >::resolution" (?resolution@?
$Display@VEnvelopePiece@@$1?f@1@QBENN@Z@utility@@0 NA)

Like if the static variables were not declared.
But isn't double Display<T,F>::resolution the proper way to declare
the "generic" template static variable, that I can then specialize if
I want to?

Thanks

StephQ
I forgot....
Should I put the declarations inside utility.hpp instead?

Cheers
StephQ

May 3 '07 #2
StephQ wrote:
On May 3, 9:00 pm, StephQ <askmeo...@mailinator.comwrote:
>In utility.hpp I have:

namespace utility
{

template <class T, double (T::*F)(double) const>
class Display
{
private:
static double resolution;
static double margin;

public:
static void computePlot(stringstream& ss, const T& t, const
ClosedInterval& interval);

};
}

In utility.cpp:

#include <utility.hpp>
namespace utility
{

template <class T, double (T::*F)(double) const>
double Display<T,F>::resolution = 0.025;

template <class T, double (T::*F)(double) const>
double Display<T,F>::margin = 5;

}

Then I have another cpp file in which I'm including utility.hpp and
calling the computePlot function using:

utility::Display<EnvelopePiece, &EnvelopePiece::f>::computePlot( ss,
*this, interval );

(the *this because I'm inside a member function of the EnvelopePiece
class when I'm calling computePlot)

However when compiling I get:
1>envelope.obj : error LNK2001: unresolved external symbol "private:
static double utility::Display<class EnvelopePiece,&public: double
__thiscall EnvelopePiece::f(double)const >::resolution" (?resolution@?
$Display@VEnvelopePiece@@$1?f@1@QBENN@Z@utility@@ 0NA)

Like if the static variables were not declared.
But isn't double Display<T,F>::resolution the proper way to declare
the "generic" template static variable, that I can then specialize if
I want to?

Thanks

StephQ

I forgot....
Should I put the declarations inside utility.hpp instead?

Cheers
StephQ
You shouldn't need utility.cpp. Put everything in utility.hpp. In your
case, the compiler couldn't see utility.cpp and couldn't properly
compile those static members (class variables).
May 3 '07 #3
You shouldn't need utility.cpp. Put everything in utility.hpp. In your
case, the compiler couldn't see utility.cpp and couldn't properly
compile those static members (class variables).
Thank you, now it works.
I was confused about the syntax, but it completely makes sense.

StephQ

May 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Graham Dumpleton | last post: by
7 posts views Thread by Hendrik Schober | last post: by
16 posts views Thread by Eric | last post: by
7 posts views Thread by ank | last post: by
8 posts views Thread by Jon Slaughter | last post: by
4 posts views Thread by santosh | last post: by
2 posts views Thread by ajikoe | last post: by
3 posts views Thread by Diebels | last post: by
17 posts views Thread by Juha Nieminen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.