473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Template usage problem

i have one structure SPageDetails.
i have a template class CStack.

Now i have a pointer of this stack class in another class
"CAnotherClass" as:
class CAnotherClass
{
CStack<SPageDetails*m_pStack_PageManager;
};

i am trying to instantiate this m_pStack_PageManager pointer as:
CAnotherClass::CAnotherClass()
{
m_pStack_PageManager = new CStack<SPageDetails>();
}

i am getting the following linker error :

AnotherClass.obj : error LNK2001: unresolved external symbol "public:
__thiscall CStack<class SPageDetails>::CStack<class SPageDetails>(int)"
(??0?$CStack@VSPageDetails@@@@QAE@H@Z)
New.dll : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Also i am a little confused with using this m_pStack_PageManager
pointer :
Can we use it like this:
m_pStackX_PageManager->push(a_sPD_);
or should we use it as:
m_pStackX_PageManager->push<SPageDetails>(a_sPD_);

Can anyone please help...

---
Veeru

Dec 25 '06 #1
3 1380
Veeru napsal:
i have one structure SPageDetails.
i have a template class CStack.

Now i have a pointer of this stack class in another class
"CAnotherClass" as:
class CAnotherClass
{
CStack<SPageDetails*m_pStack_PageManager;
};

i am trying to instantiate this m_pStack_PageManager pointer as:
CAnotherClass::CAnotherClass()
{
m_pStack_PageManager = new CStack<SPageDetails>();
}

i am getting the following linker error :

AnotherClass.obj : error LNK2001: unresolved external symbol "public:
__thiscall CStack<class SPageDetails>::CStack<class SPageDetails>(int)"
(??0?$CStack@VSPageDetails@@@@QAE@H@Z)
New.dll : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
When you are defining template class, you must write implementation of
methods in header file.
Also i am a little confused with using this m_pStack_PageManager
pointer :
Can we use it like this:
m_pStackX_PageManager->push(a_sPD_);
Yes, this way.
or should we use it as:
m_pStackX_PageManager->push<SPageDetails>(a_sPD_);
No, exact type of m_pStackX_PageManager is decided already in
declaration and compiler knows, what type it is.

Dec 25 '06 #2
Hey Ondra,
thanks a lot... its working now.
Earlier i had used a .h file for template class declaration and
implementation was done in .cpp file.
But as you said i made all the implementation in the .h file itself and
it worked. But i still didnt understand the reason for the linker
error. why was it not able to link for the template.
can you please brief me on this...

Thanks in advance,
veeru

On Dec 25, 7:25 pm, "Ondra Holub" <ondra.ho...@post.czwrote:
Veeru napsal:


i have one structure SPageDetails.
i have a template class CStack.
Now i have a pointer of this stack class in another class
"CAnotherClass" as:
class CAnotherClass
{
CStack<SPageDetails*m_pStack_PageManager;
};
i am trying to instantiate this m_pStack_PageManager pointer as:
CAnotherClass::CAnotherClass()
{
m_pStack_PageManager = new CStack<SPageDetails>();
}
i am getting the following linker error :
AnotherClass.obj : error LNK2001: unresolved external symbol "public:
__thiscall CStack<class SPageDetails>::CStack<class SPageDetails>(int)"
(??0?$CStack@VSPageDetails@@@@QAE@H@Z)
New.dll : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.When you are defining template class, you must write implementation of
methods in header file.
Also i am a little confused with using this m_pStack_PageManager
pointer :
Can we use it like this:
m_pStackX_PageManager->push(a_sPD_);Yes, this way.
or should we use it as:
m_pStackX_PageManager->push<SPageDetails>(a_sPD_);No, exact type of m_pStackX_PageManager is decided already in
declaration and compiler knows, what type it is.- Hide quoted text -- Show quoted text -
Dec 25 '06 #3
Hello!

Veeru wrote:
Hey Ondra,
thanks a lot... its working now.
Earlier i had used a .h file for template class declaration and
implementation was done in .cpp file.
But as you said i made all the implementation in the .h file itself and
it worked. But i still didnt understand the reason for the linker
error. why was it not able to link for the template.
can you please brief me on this...
The idea is that you /should/ be able to do it the way you tried.
There's a keyword for it, 'export', so that you can 'export' templates
from translation units. However, many C++ compilers don't implement
'export', even though it's part of the C++ standard.

To understand why, consider the following little story.

Once upon a time, Alice wrote part of a program, in C++. She declared
some templates in a header file ("alice.hpp"), and defined those same
templates in the source files for the implementation of her part of that
program ("alice1.cpp", "alice2.cpp", and so on). She compiled that part
of the program (producing "alice.o"), and sent the header file to Bob.
Included in the header file was the following declaration:-

template<typename Tclass thingy {

// Blah, blah, blah.

some_type<T *foo (some_other_type);
some_other_type bar (some_type<T *>);
};

As it's just a header file, the definitions of thingy<>::foo() and
thingy<>::bar() are not included. They're in the stuff that Alice
compiled, as normal.

So, Bob received that header file from Alice, and proceeded to write his
part of the program ("bob.cpp"). He defined some new classes, and used
them as template parameters in specialisations of thingy<>. One of his
new classes was class shiny, and he used it as follows:-

#include "alice.hpp"

class puzzler {
thingy<shinyt;

// Blah, blah, blah.

public:
void f (some_other_type x) { t.bar(t.foo(x)); }
};

puzzler g (puzzler x, some_other_type y) { x.f(y); return x; }

Now, when Bob's g() gets called, it'll call puzzler::f(), which, in
turn, will call thingy<shiny>::foo() and thingy<shiny>::bar(). So,
thingy<shiny>::foo() and thingy<shiny>::bar() will have to be
instantiated - but how? Bob's class shiny didn't even exist when Alice
compiled her part of the program, so those member functions can't exist
in the part she compiled ("alice.o"). Bob only has Alice's header file,
so doesn't have the definitions of thingy<>::foo() and thingy<>::bar()
available. So, how are thingy<shiny>::foo() and thingy<shiny>::bar()
going to get instantiated?

:-)

Simon

--
What happens if I mention Leader Kibo in my .signature?
Dec 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Andrea A | last post by:
Hi, i'm developing a website that will have an huge amount of visitors a day --> it will be a contest with 100.000$ of prize. I'm concerning about using a template class (and which one do you...
4
by: Marc Schellens | last post by:
I posted a similar question some time ago, but didn't get an satisfying answer. Lets say I have a template and five integer and two floating types. template <typename T> class A { A() {}...
6
by: Dmitry Epstein | last post by:
Here is an example of a problem, which I tried to reduce to its bare essentials: // begin test1.cpp class E { public: template<class T> static void f(); }; template<class T> void E::f() {}
4
by: wkaras | last post by:
I would like to propose the following changes to the C++ Standard, the goal of which are to provide an improved ability to specify the constraints on type parameters to templates. Let me say from...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
3
by: Larry Beck | last post by:
Below is a problem I am having with templates. I am trying to use a template to avoid having multiple class methods that differ only in the return type of the pointer. I have tried putting in...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
2
by: adrian.hawryluk | last post by:
Hi everyone, I've been using templates for a while, but I'm not at full power yet (knowledge=power) ;). Does anyone know where I can get information on this 'new' template usage? template<a...
8
by: vectorizor | last post by:
Hi all, I have a slight problem with the usage of function templates. It shoud be really easy for you guys to explain me what's wrong. I have one base class and 2 derived classes: /*code*/...
3
by: Anonymous | last post by:
Could someone please explain template template classes, showing: 1). Why they are needed / i.e what problem do they solve ? 2). A simple example I have read various articles etc, but it still...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
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...

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.