473,508 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5777
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3121
by: Graham Dumpleton | last post by:
When you have a template class with a static member variable, ie., template<typename T> class handle { public: static void* id() { return &id_; } private: static int id_; };
7
1985
by: Hendrik Schober | last post by:
Hi, I have a problem, that boils down to the following code: #include <iostream> #include <typeinfo> class Test1 {}; class Test2 {}; class Test3 {};
16
2953
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
7
12440
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
8
1622
by: Jon Slaughter | last post by:
Is there any way to declare a member of a base class to make the itself in the derived class to be static? i.e., I have two classes class Base { protected: char *name;
4
1969
by: santosh | last post by:
Hello, I have a doubt about static variable. class B { private: static int x; public: static set(int y) {
2
4427
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
3
5692
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
17
8352
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.