473,396 Members | 1,693 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.

Passing template struct as an argument to a template function

Hi

I am trying to learn about Queues and use templates at the same time. I
have written the following code and I am getting a link error, stating

"unresolved external symbol, "int__cdecl adsq::QInitialise<struct
adsq::Data>(struct adsq::Head<struct adsq::Data> *)"

-----------adsq.h file

#ifndef ADSQ_H

#define ADSQ_H

#include <iostream>

namespace adsq
{
struct Data
{
int First;
int Second;
};

template <class T> struct Node
{
T itsData;
Node<T>* nextNode;
};

template <class T> struct Head
{
Node<T>* itsHead;
Node<T>* itsTail;
long Count;
];

template <class R> int QInitialise(Head<R>* theQueueHead);
//Specialiasation
//template <> int QInitialise(Head<Data>* theQueueHead);
}

-----------adsq.cpp

#include "adsq.h"

template <class R> int adsq::QInitialise(Head<R>* theQueueHead)
{
theQueueHead->itsHead = 0;
theQueueHead->itsTail = 0;
theQueueHead->Count = 0;
}

//template <> int adsq::QInitialise(Head<Data>* theQueueHead)
//{
//theQueueHead->itsHead = 0;
//theQueueHead->itsTail = 0;
//theQueueHead->Count = 0;
//}

-----------driver.h

#ifndef DRIVER_H

#define DRIVER_H

#include <iostream>

#include "adsq.h"

#endif

-----------driver.cpp

#include "driver.h"

using namespace std;
using namespace adsq;

int main()
{
Head<Data> *pH = new Head<Data>;
int i = QInitialise(pH); //This will only work if you implement
//the specialisation
}

I have tried to keep this as simple for myself as possible as I am still
new to this C++ game.

I have tried the FAQ and googling, but I can't find anything specific to
passing templates to templates

Can you pass a template to another template as I am trying to do above,
or is it not possible. Must I use specialisations, if I do how will I
know ever type that a user may put into my queue?

The above was created in a VS 2003 unmanaged project.

TYIA

nifsmith
Jul 22 '05 #1
5 5259

"nifsmith" <ha*********@lycos.co.uk> wrote in message
news:cj*********@titan.btinternet.com...
Hi

I am trying to learn about Queues and use templates at the same time. I
have written the following code and I am getting a link error, stating

"unresolved external symbol, "int__cdecl adsq::QInitialise<struct
adsq::Data>(struct adsq::Head<struct adsq::Data> *)"
[snip]
I have tried to keep this as simple for myself as possible as I am still
new to this C++ game.

I have tried the FAQ and googling, but I can't find anything specific to
passing templates to templates

Can you pass a template to another template as I am trying to do above,
or is it not possible. Must I use specialisations, if I do how will I
know ever type that a user may put into my queue?


The problem is covered in the FAQ but it has nothing to do with passing
templates to templates (which is perfectly OK). The problem is that template
code must always go in header files, take all the code from adsq.cpp and put
it in adsq.h. See the FAQ
http://www.parashift.com/c++-faq-lit...html#faq-34.12 and following.

john


Jul 22 '05 #2
John Harrison wrote:

The problem is covered in the FAQ but it has nothing to do with passing
templates to templates (which is perfectly OK). The problem is that template
code must always go in header files, take all the code from adsq.cpp and put
it in adsq.h. See the FAQ
http://www.parashift.com/c++-faq-lit...html#faq-34.12 and following.

john


Here I stand chastened and contrite.

I now intend to read everything in the FAQ

Many thanks

nifsmith.
Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2r*************@uni-berlin.de...

"nifsmith" <ha*********@lycos.co.uk> wrote in message
news:cj*********@titan.btinternet.com...
Hi

I am trying to learn about Queues and use templates at the same time. I
have written the following code and I am getting a link error, stating

"unresolved external symbol, "int__cdecl adsq::QInitialise<struct
adsq::Data>(struct adsq::Head<struct adsq::Data> *)"

[snip]

I have tried to keep this as simple for myself as possible as I am still
new to this C++ game.

I have tried the FAQ and googling, but I can't find anything specific to
passing templates to templates

Can you pass a template to another template as I am trying to do above,
or is it not possible. Must I use specialisations, if I do how will I
know ever type that a user may put into my queue?


The problem is covered in the FAQ but it has nothing to do with passing
templates to templates (which is perfectly OK). The problem is that
template
code must always go in header files, take all the code from adsq.cpp and
put
it in adsq.h. See the FAQ
http://www.parashift.com/c++-faq-lit...html#faq-34.12
and following.

john


I wish to go on this subject. Given the 3 rules from the FAQ:
1.. A template is not a class or a function. A template is a "pattern"
that the compiler uses to generate a family of classes or functions.
2.. In order for the compiler to generate the code, it must see both the
template definition (not just declaration) and the specific types/whatever
used to "fill in" the template. For example, if you're trying to use a
Foo<int>, the compiler must see both the Foo template and the fact that
you're trying to make a specific Foo<int>.
3.. Your compiler probably doesn't remember the details of one .cpp file
while it is compiling another .cpp file. It could, but most do not and if
you are reading this FAQ, it almost definitely does not. BTW this is called
the "separate compilation model."
Suppose I wish to make a library using templates (being classes or
functions). Of course, I don't want my source code (i.e. my little secrets)
being distributed along with my binaries (.lib, .dll, or .so, name it!). On
the other hand, I cannot start specifying all possible instantiations, in my
..cpp, any potential user will try to make up.

Can someone tell me what are my options, as a Foo provider, to sell my
product to the Bar implementers?

Thank you.
Jul 22 '05 #4
"Maitre Bart" <ma********@videotron.ca> wrote...
[...]
I wish to go on this subject. Given the 3 rules from the FAQ:
1.. A template is not a class or a function. A template is a "pattern"
that the compiler uses to generate a family of classes or functions.
2.. In order for the compiler to generate the code, it must see both the
template definition (not just declaration) and the specific types/whatever
used to "fill in" the template. For example, if you're trying to use a
Foo<int>, the compiler must see both the Foo template and the fact that
you're trying to make a specific Foo<int>.
3.. Your compiler probably doesn't remember the details of one .cpp file
while it is compiling another .cpp file. It could, but most do not and if
you are reading this FAQ, it almost definitely does not. BTW this is
called the "separate compilation model."
Suppose I wish to make a library using templates (being classes or
functions). Of course, I don't want my source code (i.e. my little
secrets) being distributed along with my binaries (.lib, .dll, or .so,
name it!). On the other hand, I cannot start specifying all possible
instantiations, in my .cpp, any potential user will try to make up.
You can't? Why can't you? Do you really have such a library that all
types could be used to instantiate templates?
Can someone tell me what are my options, as a Foo provider, to sell my
product to the Bar implementers?


You have three options.

One (just like many, if not all, other template code vendors do) is to
obfuscate the hell out of your code and still supply it all in the
headers you provide along with the rest of it. That's the most common
way. If you really want to protect your "secrets", get a patent. If
they are not patentable, they are not really secrets, are they?

Two is to limit your clients to use of a compiler that implements "export"
and make sure that all your object code (and template code) supplied is
compiled with that compiler.

Three is to limit your clients to instantiating your templates with
a short predetermined list of types. You library code has to contain the
necessary _explicit_specialisations_ of all the templates.

Both two and three are the ways to put yourself at a huge disadvantage
as far as software market goes. Since you said that you cannot rely on
option "three", then you are stuck with one or two, and limiting your
clients to a particular (potentially not very popular or even known)
compiler is in no way a good idea. Of course, the final decision is for
you to make.

Oh, the fourth option is to drop templates altogether and go with the
plain ol' set of functions and classes. Nothing essentially wrong with
that either. Majority of libraries on the market are still template-free.

Victor
Jul 22 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:dnK5d.116883$MQ5.24909@attbi_s52...
"Maitre Bart" <ma********@videotron.ca> wrote...
[...]
Suppose I wish to make a library using templates (being classes or
functions). Of course, I don't want my source code (i.e. my little
secrets) being distributed along with my binaries (.lib, .dll, or .so,
name it!). On the other hand, I cannot start specifying all possible
instantiations, in my .cpp, any potential user will try to make up.
You can't? Why can't you? Do you really have such a library that all
types could be used to instantiate templates?


Well, here is a simple case. Let's say that some of my template functions,
for example, contain a functor (or a predicate) a user has to provide as an
argument:

template <class _A_ , class _R_, class _Op_>
_R_ DoSomething(_A_ a_from, _A_ a_to, _Op_ a_converter);

How can I possibly think about all of the possible instantiations that may
exist in the whole world for 'a_converter'?
Can someone tell me what are my options, as a Foo provider, to sell my
product to the Bar implementers?


You have three options.

One (just like many, if not all, other template code vendors do) is to
obfuscate the hell out of your code and still supply it all in the
headers you provide along with the rest of it. That's the most common
way. If you really want to protect your "secrets", get a patent. If
they are not patentable, they are not really secrets, are they?


That is what I can observe in MS' STL implemention. Still, I wouldn't take
is solution if all my invested $$$ in development could be
reverse-engineered in a couple of hours. Would you? !!!
Two is to limit your clients to use of a compiler that implements "export"
and make sure that all your object code (and template code) supplied is
compiled with that compiler.

Then, I will need to read from Herb Sutter about 'export'.
Three is to limit your clients to instantiating your templates with
a short predetermined list of types. You library code has to contain the
necessary _explicit_specialisations_ of all the templates.

That is precisely my point!
Both two and three are the ways to put yourself at a huge disadvantage
as far as software market goes. Since you said that you cannot rely on
option "three", then you are stuck with one or two, and limiting your
clients to a particular (potentially not very popular or even known)
compiler is in no way a good idea. Of course, the final decision is for
you to make.

I think I will conclue rapidely on the subject (see below)...
Oh, the fourth option is to drop templates altogether and go with the
plain ol' set of functions and classes. Nothing essentially wrong with
that either. Majority of libraries on the market are still template-free.

Finaly, it all looks like that templates were not designed for "private"
libraries, unless they contain no secret and can be explicitely instantiated
for the basic types, which by nature is a "public" library.
Victor


Thanks for your opinion.
Jul 22 '05 #6

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
3
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the...
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
7
by: gretean | last post by:
I have a problem that's driving me crazy involving Microsoft's ability to deduce template parameters. I am using Visual Studio .NET (aka VC7?), and it gives an error compiling the following code....
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
0
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*....
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: 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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.