Template question

B

Bryan

I have a template class declared like this:

template <class T>
class CKineStore
{
public:
CKineStore(void) {};
virtual ~CKineStore(void) {};

public:
void Insert(CKineTypes::ECKineType type, T&);

private:
typedef std::pair <CKineTypes::ECKineType, T> m_pair;
typedef std::map <CKineTypes::ECKineType, T> m_map;

};

template <class T> void CKineStore<T>::Insert(CKineTypes::ECKineType
type, T& t)
{
m_map.insert(m_pair(type, t));
}

If I dont call Insert() it compiles okay, but when I call it I get an
unresolved external symbol error. What am I doing wrong?

Thanks,
B
 
M

ma740988

I have a template class declared like this:

template <class T>
class CKineStore
{
public:
CKineStore(void) {};
virtual ~CKineStore(void) {};

public:
void Insert(CKineTypes::ECKineType type, T&);

private:
typedef std::pair <CKineTypes::ECKineType, T> m_pair;
typedef std::map <CKineTypes::ECKineType, T> m_map;

};

template <class T> void CKineStore<T>::Insert(CKineTypes::ECKineType
type, T& t)
{
m_map.insert(m_pair(type, t));

}

If I dont call Insert() it compiles okay, but when I call it I get an
unresolved external symbol error. What am I doing wrong?

Thanks,
B

You didn't provide much information about CKineTypes, nonetheless,
try:

void Insert( typename CKineTypes::ECKineType type, T&);
template <class T>
void CKineStore<T>::Insert(
typename CKineTypes::ECKineType
type, T& t)
{}

Note the use of "typename"
 
B

Bryan

ma740988 said:
You didn't provide much information about CKineTypes, nonetheless,
try:

void Insert( typename CKineTypes::ECKineType type, T&);
template <class T>
void CKineStore<T>::Insert(
typename CKineTypes::ECKineType
type, T& t)
{}

Note the use of "typename"

Sorry, CKineTypes is just an enum.

Still the same error though. I did this:
template <class T>
class CKineStore
{
....
public:
void Insert(typename CKineTypes::ECKineType type, T&);
....
};

And in the cpp file:
template <class T>
void CKineStore<T>::Insert(typename CKineTypes::ECKineType type, T& t)
{
insert(m_pair(type, t));
}

Im not very experienced with templates obviously, what am I missing?

B
 
R

Robert Bauck Hamar

Bryan said:
I have a template class declared like this:

template <class T>
class CKineStore
{
public:
CKineStore(void) {};

Why the void? This isn't C.
virtual ~CKineStore(void) {};

public:
void Insert(CKineTypes::ECKineType type, T&);

What is CKineTypes and its ECKineType? Why this need for hungarian
notation (prefixing type names with a C and E)?
private:
typedef std::pair <CKineTypes::ECKineType, T> m_pair;
typedef std::map <CKineTypes::ECKineType, T> m_map;

Here, m_map is defined as a type.
};

template <class T> void CKineStore<T>::Insert(CKineTypes::ECKineType
type, T& t)
{
m_map.insert(m_pair(type, t));

Here, you are using m_map, the type, as an object.
}

If I dont call Insert() it compiles okay, but when I call it I get an
unresolved external symbol error. What am I doing wrong?

Try removing the typedef in m_map's definition.
 
B

Bryan

Robert said:
Why the void? This isn't C.

Dunno. Visual studio 2005 sticks it in there by default.
What is CKineTypes and its ECKineType? Why this need for hungarian
notation (prefixing type names with a C and E)?

CKineTypes is actually not hungarian, but you are right ECKineType is.
I am just used to it I suppose.
Here, m_map is defined as a type.

You are right, this is a typo on my part. It is actually:
std::map <CKineTypes::ECKineType, T> m_map;

This is a larger class, and I removed some portions for brevity.
Here, you are using m_map, the type, as an object.


Try removing the typedef in m_map's definition.

Error seems to persist... the errors you pointed out are valid- not
complaining at all, thanks for pointing them out.

But the error I initially reported is a signature issue. The
declaration of the Insert function doesnt match any known definition, so
the unresolved external symbol error shows up.

I can, in fact do this:
template <class T>
class CKineStore
{
....
public:
void Test();
....
};

template <class T>
void CKineStore<T>::Test()
{
}

And I get the same error, unresolved external symbol.
 
V

v.r.marinov

And in the cpp file:
template <class T>
void CKineStore<T>::Insert(typename CKineTypes::ECKineType type, T& t)
{
insert(m_pair(type, t));

}

Im not very experienced with templates obviously, what am I missing?

Try putting everything in the same file.
 
R

Robert Bauck Hamar

Bryan said:
Robert Bauck Hamar wrote:
But the error I initially reported is a signature issue. The
declaration of the Insert function doesnt match any known definition, so
the unresolved external symbol error shows up.

I can, in fact do this:
template <class T>
class CKineStore
{
...
public:
void Test();
...
};

template <class T>
void CKineStore<T>::Test()
{
}

And I get the same error, unresolved external symbol.

Compiles with my compiler, but I use g++. The only explanation I can
think of, is that the compiler can't see the definition of
CKineStore<T>::Test(). The definition is, of course, included together
with the CKineStore class declaration? Then again, it's five in the
morning here, so I'm beginning to feel tired.
 
V

v.r.marinov

Compiles with my compiler, but I use g++. The only explanation I can
think of, is that the compiler can't see the definition of
CKineStore<T>::Test(). The definition is, of course, included together
with the CKineStore class declaration? Then again, it's five in the
morning here, so I'm beginning to feel tired.

If the definition wasn't included you would have a link error.
This is not compiler dependent issue.

BTW some compilers like Cameau (and Intel?) support exported
templates.
The *export* keyword is part of the standard, but neither g++ nor vc++
support it.
 

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

Forum statistics

Threads
474,294
Messages
2,571,510
Members
48,195
Latest member
Tomjerry

Latest Threads

Top