By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Problem with static variable in template class

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.