Problem with static variable in template class

S

StephQ

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
 
S

StephQ

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
 
F

Fei Liu

StephQ said:
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).
 
S

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).

Thank you, now it works.
I was confused about the syntax, but it completely makes sense.

StephQ
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top