473,386 Members | 1,803 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,386 software developers and data experts.

Simple instantiating of class templates

Hello at all,

i've got the following problem:
- I define a class template in a header file
- I implement the methods in a cpp file
- I want to build an instance of my class template

this works if the implementation of the constructor is either inside
the class or in the same file as the class. But if the implementation
is outside in another file, the linker complains, that there is an
unresolved external symbol (LNK2019).
I'm using visual studio 2005 on a xp machine. And all files where added
to the project. Same problem exists if i'm using gcc under cygwin.

Have got anyone an idea?

Here is my code:
-------------------------------
MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H

template <class T>
class MyClass {
public:
MyClass(T MyVal);
void MyFunction(void);

private:
T Val;
};

#endif // MYCLASS_H

MyClass.cpp
#include "MyClass.h"
#include <iostream>

template <class T>
MyClass<T>::MyClass(T MyVal) {
this->Val = MyVal;
}

template <class T>
void MyClass<T>::MyFunction() {
std::cout << "MyFunction";
}

MyTest.cpp
#include "MyClass.h"

int main(int argc, char* argv[]) {

MyClass<intvar(1);
return 0;
}

Oct 8 '06 #1
4 1731
Hi

How is the compiler supposed to know what your constructor does if you
include the header in another module. i.e. I it replaces T with the actual
type. The compiler usually isn't smart enough to try to find the C++ source
file of the template and replace the template with your actual type..

In other words move everything from the template CPP file into the template
header file.
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"asterixgallier" <as************@freenet.dewrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Hello at all,

i've got the following problem:
- I define a class template in a header file
- I implement the methods in a cpp file
- I want to build an instance of my class template

this works if the implementation of the constructor is either inside
the class or in the same file as the class. But if the implementation
is outside in another file, the linker complains, that there is an
unresolved external symbol (LNK2019).
I'm using visual studio 2005 on a xp machine. And all files where added
to the project. Same problem exists if i'm using gcc under cygwin.

Have got anyone an idea?

Here is my code:
-------------------------------
MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H

template <class T>
class MyClass {
public:
MyClass(T MyVal);
void MyFunction(void);

private:
T Val;
};

#endif // MYCLASS_H

MyClass.cpp
#include "MyClass.h"
#include <iostream>

template <class T>
MyClass<T>::MyClass(T MyVal) {
this->Val = MyVal;
}

template <class T>
void MyClass<T>::MyFunction() {
std::cout << "MyFunction";
}

MyTest.cpp
#include "MyClass.h"

int main(int argc, char* argv[]) {

MyClass<intvar(1);
return 0;
}

Oct 8 '06 #2

Ohh thank you very much for your fast reply.

This is although an explanation why i haven't found an example in this
way on the internet =)

Thank you

Moonlit wrote:
Hi

How is the compiler supposed to know what your constructor does if you
include the header in another module. i.e. I it replaces T with the actual
type. The compiler usually isn't smart enough to try to find the C++ source
file of the template and replace the template with your actual type..

In other words move everything from the template CPP file into the template
header file.
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"asterixgallier" <as************@freenet.dewrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Hello at all,

i've got the following problem:
- I define a class template in a header file
- I implement the methods in a cpp file
- I want to build an instance of my class template

this works if the implementation of the constructor is either inside
the class or in the same file as the class. But if the implementation
is outside in another file, the linker complains, that there is an
unresolved external symbol (LNK2019).
I'm using visual studio 2005 on a xp machine. And all files where added
to the project. Same problem exists if i'm using gcc under cygwin.

Have got anyone an idea?

Here is my code:
-------------------------------
MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H

template <class T>
class MyClass {
public:
MyClass(T MyVal);
void MyFunction(void);

private:
T Val;
};

#endif // MYCLASS_H

MyClass.cpp
#include "MyClass.h"
#include <iostream>

template <class T>
MyClass<T>::MyClass(T MyVal) {
this->Val = MyVal;
}

template <class T>
void MyClass<T>::MyFunction() {
std::cout << "MyFunction";
}

MyTest.cpp
#include "MyClass.h"

int main(int argc, char* argv[]) {

MyClass<intvar(1);
return 0;
}
Oct 8 '06 #3
asterixgallier wrote:
Hello at all,

i've got the following problem:
- I define a class template in a header file
- I implement the methods in a cpp file
- I want to build an instance of my class template

this works if the implementation of the constructor is either inside
the class or in the same file as the class. But if the implementation
is outside in another file, the linker complains, that there is an
unresolved external symbol (LNK2019).
I'm using visual studio 2005 on a xp machine. And all files where added
to the project. Same problem exists if i'm using gcc under cygwin.

Have got anyone an idea?
Yeah, http://www.parashift.com/c++-faq-lit...html#faq-35.13
Oct 8 '06 #4

red floyd wrote:
asterixgallier wrote:
Hello at all,

i've got the following problem:
- I define a class template in a header file
- I implement the methods in a cpp file
- I want to build an instance of my class template

this works if the implementation of the constructor is either inside
the class or in the same file as the class. But if the implementation
is outside in another file, the linker complains, that there is an
unresolved external symbol (LNK2019).
I'm using visual studio 2005 on a xp machine. And all files where added
to the project. Same problem exists if i'm using gcc under cygwin.

Have got anyone an idea?

Yeah, http://www.parashift.com/c++-faq-lit...html#faq-35.13
What the hell?
template void foo<int>(); ?
and no template specilization...

Oct 8 '06 #5

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
6
by: C++fan | last post by:
Hi all: Below is simple code: class bs { public: virtual void handle() = 0; }; class driv : public bs { public:
8
by: Phil D | last post by:
Hi, I am new to c# and oop and have a question about interfaces which I hope someone can help me with. The book I am using (C# Step by Step) explains how to create and implement interfaces...
0
by: Ian | last post by:
I have sub-classed the Page class in order to provide some base properties and methods that every page on my site will need access to. I would like to have these things show up in the Simple...
2
by: HarishP | last post by:
Hi, How to avoid instantiating the class more than 10 times Harish.P Sr. Software Engineer Comat Technologies Pvt. Ltd., Bangalore Email: harish.p@comat.com
3
by: Nagesh | last post by:
hi, I have seen the winvnc(tightvnc server) source code in this I seen that class member funtions are calling without instantiating the object i.e. like vncService::ShowDefaultProperties() where...
5
by: Support | last post by:
Hello: I have the following code that currently needs to go in every page. QUESTION 1) Can Page_Preinit go somewhere else more global like Global.asax so that I dont have to copy this code in...
2
by: samadams_2006 | last post by:
I have what seems to be a very simple straight-forward program (shown below), but I'm getting an error: Unable to cast object of type 'TestApp1.ParentTable' to type 'TestApp1.ChildTable'. on...
4
by: dascandy | last post by:
Hi, For a project I'm working on I'm kind-of-hacking my way around deriving a class from an interface or such to create a mock, but instead creating the mock directly. It is usable as the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.