473,834 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::QInitiali se<struct
adsq::Data>(str uct adsq::Head<stru ct 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(Hea d<R>* theQueueHead);
//Specialiasation
//template <> int QInitialise(Hea d<Data>* theQueueHead);
}

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

#include "adsq.h"

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

//template <> int adsq::QInitiali se(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 5308

"nifsmith" <ha*********@ly cos.co.uk> wrote in message
news:cj******** *@titan.btinter net.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::QInitiali se<struct
adsq::Data>(str uct adsq::Head<stru ct 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*********@ly cos.co.uk> wrote in message
news:cj******** *@titan.btinter net.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::QInitiali se<struct
adsq::Data>(str uct adsq::Head<stru ct 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********@vid eotron.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_speci alisations_ 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.********@com Acast.net> wrote in message
news:dnK5d.1168 83$MQ5.24909@at tbi_s52...
"Maitre Bart" <ma********@vid eotron.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_speci alisations_ 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
10198
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 code... TCHAR myArray; DoStuff(myArray);
31
3547
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 return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x : y;
8
11370
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 f2(ANY, ANY) ANY f3(ANY, ANY, ANY)
3
2755
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 default value, which is always_true(). but gcc says: no matching function for call to `x::do_something()' and msvs says: could not deduce template argument for '_Tp'
4
4263
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 complementary to one another. So I proceeded to decompose the tasks that deal with user options into two functions. Each of the functions do something and towards the end they do supplementary tasks that depend on the server option. The whole things...
7
12595
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. template <int x, int yclass M { public: template <int xRet, int yRet, int xR, int yR> M<xRet, yRetoperator *(const M<xR, yR&b) const { M<xRet,yReta; return a;
18
2886
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 write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything about f, except how to call it.
16
3965
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
1941
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*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from. so here is the error I'm getting: TwoDMap.cpp: In member function 'void TwoDMap::setArc(const...
0
9797
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9644
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10509
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10547
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10219
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9331
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5626
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.