Template Question

J

Jack

Hi,
I am stuck on a strange problem:
Here is my .h file.
#define DEFSIZE 10

template <class T>
public ref class cliQ{
public:
cliQ();
cliQ(int size);
void addToQ(int num);
T getItem();

private:
array<int>^ mQ;
int count;

};

here is cpp for the class:

#include "stdafx.h"
#include "cliQ.h"

using namespace System;
template <class T>
cliQ<T>::cliQ(){
Console::WriteLine("Setting up the Q with the Default
Const...");
mQ = gcnew array<int>(DEFSIZE);
count = 0;
}

template <class T>
cliQ<T>::cliQ(int size){
Console::WriteLine("Setting up the Q with the SIZE Const...");
mQ = gcnew array<int>(size);
count = 0;
}

template <class T>
void cliQ<T>::addToQ(int num){
//(*mQ)[count] = num;
count++;
}

template <class T>
T cliQ<T>::getItem(){
return 0;

}

and finally here is the driver (main)

#include "cliQ.h"
using namespace System;

int main(array<System::String ^> ^args)
{
Console::Write("Enter the size of the Queue: ");
cliQ<int> ^ q = gcnew
cliQ<int>(int::parse(Console::ReadLine()));
q->addToQ(5);

Console::Write("Press any key to continue...");
Console::ReadKey();

return 0;

}

When I try to compile this I get the following errors
Generating Code...
Linking...
myClr.obj : error LNK2020: unresolved token (06000001)
cliQ<int>::.ctor
myClr.obj : error LNK2020: unresolved token (06000002)
cliQ<int>::addToQ
C:\Documents and Settings\sdastour\My Documents\Visual Studio
2008\Projects\myClr\Debug\myClr.exe : fatal error LNK1120: 2
unresolved externals

I have looked on the net but can't find the reason for this.

Any ideas?

Thanks
 
C

Carl Daniel [VC++ MVP]

Jack said:
Hi,
I am stuck on a strange problem:

Just as Igor replied in .vc.language, you need to put the implementation of
your template functions in the header file so that the compiler can see them
when compiling the call site (in main(), in your example).

Remember: templates are fancy macros understood only by the C++ compiler,
and the C++ compiler reads a single source file at a time, inserting
"headre" files by way of #include - anything that can't be reached via
#includes is not visible to the compiler.

-cd
 
J

Jack

Just as Igor replied in .vc.language, you need to put the implementation of
your template functions in the header file so that the compiler can see them
when compiling the call site (in main(), in your example).

Remember: templates are fancy macros understood only by the C++ compiler,
and the C++ compiler reads a single source file at a time, inserting
"headre" files by way of #include - anything that can't be reached via
#includes is not visible to the compiler.

-cd

Hi Carl,
So if I understand you correctly I am to get rid of my cliQ.cpp file
which includes all the function implementations for the class
functions. Instead I should write the function implementations in the
header file for cliQ. If this is true, doesn't this get rid of the
whole encapsulation and security of the functions (one of the benefits
of OOP) (the header can be viewed by everyone where the
implementations of the functions gets protected as it turns into a
binary code). My understanding has been that the .h files are like the
interfaces to the function implementations which can be viewed by
other developers if needed. Instead we create object files out of
the .CPP files. But if we put the implementations of the functions in
the .h files, wouldn't that expose the code?

Thanks
Jack
 
B

Ben Voigt [C++ MVP]

Jack said:
Hi Carl,
So if I understand you correctly I am to get rid of my cliQ.cpp file
which includes all the function implementations for the class
functions. Instead I should write the function implementations in the
header file for cliQ. If this is true, doesn't this get rid of the
whole encapsulation and security of the functions (one of the benefits
of OOP) (the header can be viewed by everyone where the
implementations of the functions gets protected as it turns into a
binary code). My understanding has been that the .h files are like the
interfaces to the function implementations which can be viewed by
other developers if needed. Instead we create object files out of
the .CPP files. But if we put the implementations of the functions in
the .h files, wouldn't that expose the code?

It doesn't harm encapsulation at all. All C++ visibilities are fully in
effect (public/private/protected). It does reveal the source code, but that
has nothing to do with security and actually enhances reusability by
providing the ultimate documentation.

In native code, the only security against other code is by the OS process
isolation. Security usually refers to not trusting inputs passes across
process boundaries (such as sockets, data files, etc).

So no, templates don't break OOP at all. They do impact intellectual
property and hiding proprietary algorithms, but a determined person can get
those from the compiled binaries too. It's the price you pay for
type-specific optimizations.
 

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

Top