The following is an abridged copy of the code I am using in a template function along with the linker error produced during the build. The compiler has no issues with the code, only the linker:
file DB.h -
-
enum TypeEnum
-
{
-
eDouble,
-
eInteger,
-
eBool,
-
eString,
-
eFloat,
-
eLong,
-
TypeEnumRange
-
};
-
-
template <class T>
-
class DataBusGenericOutputType
-
{
-
public:
-
-
DataBusGenericOutputType();
-
-
void SetValue(T data){ GT->SetValue(data); } ;
-
-
private:
-
-
DataBusGenericType<T>* GT;
-
};
-
-
class DataBus
-
{
-
public:
-
-
......
-
-
template <typename T> DataBusGenericOutputType<T> *Register2(std::string ID, TypeEnum type, T value);
-
-
......
-
-
private:
-
-
......
-
-
std::list< DataBusGenericType<double> > DoubleList;
-
std::list< DataBusGenericType<string> > StringList;
-
std::list< DataBusGenericType<int> > IntList;
-
std::list< DataBusGenericType<bool> > BoolList;
-
std::list< DataBusGenericType<float> > FloatList;
-
std::list< DataBusGenericType<long> > LongList;
-
-
......
-
};
-
file DB.cpp -
template <typename T>
-
DataBusGenericOutputType<T> *DataBus::Register2(std::string ID, TypeEnum type, T value)
-
{
-
DataBusGenericOutputType<T> *OutType = NULL;
-
DataBusGenericType<T> *GenType = NULL;
-
std::vector<string> IDparts;
-
-
IDparts = ParseID(ID);
-
-
GenType = new DataBusGenericType<T>(IDparts[ClassLocation], IDparts[VariableLocation], IDparts[TypeLocation], type, value);
-
-
switch(type)
-
{
-
case eDouble:
-
DoubleList.push_back(*GenType);
-
OutType = new DataBusGenericOutputType<T>(&DoubleList.back());
-
break;
-
-
case eInteger:
-
IntList.push_back(*GenType);
-
OutType = new DataBusGenericOutputType<T>(&IntList.back());
-
break;
-
-
case eBool:
-
BoolList.push_back(*GenType);
-
OutType = new DataBusGenericOutputType<T>(&BoolList.back());
-
break;
-
-
case eString:
-
StringList.push_back(*GenType);
-
OutType = new DataBusGenericOutputType<T>(&StringList.back());
-
break;
-
-
case eFloat:
-
FloatList.push_back(*GenType);
-
OutType = new DataBusGenericOutputType<T>(&FloatList.back());
-
break;
-
-
case eLong:
-
LongList.push_back(*GenType);
-
OutType = new DataBusGenericOutputType<T>(&LongList.back());
-
break;
-
-
default:
-
break;
-
-
}
-
return OutType;
-
}
-
file Dummy.cpp -
#include "DB.h"
-
-
void Dummy::Register()
-
{
-
DataBus *DB = new DataBus();
-
double mDummyDouble;
-
DataBusGenericOutputType<double> *oDummyDouble;
-
-
oDummyDouble = DB->Register2("Dummy.DummyDouble.double", eDouble, mDummyDouble); // <---This is line 115 which the linker error references
-
-
....
-
}
-
Error from the linker:
./Lib/libBigBastard.a(Dummy.o)(.text+0x6e6): In function `Dummy::Register()':
/home/jr/Dummy.cpp:115: undefined reference to `DataBusGenericOutputType<double>* DataBus::Register2<double>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, TypeEnum, double)'
Again, the compiler gives no errors so all local and stl headers have been properly included and all functions and types seem to be accessible by the code. For what it's worth, the code is being built using gcc version 3.2.2.
Thanks!
7 1606
That function is not a class method.It's outside of DataBusGenericOutputType class scope..
That function is not a class method.It's outside of DataBusGenericOutputType class scope..
I apologize for that, in my rush to post the question I did not clearly specify the class declaration. Please see the edited original post again.
Thanks!
I apologize for that, in my rush to post the question I did not clearly specify the class declaration. Please see the edited original post again.
Thanks!
Template functions must be defined in the same file where they are declared.
NOTE:
There are ways around it like using export keyword or including a cpp file with function definitions just after template class declarations,but they can really complicate your life.Export templates keyword is not supported in all compilers and including a definition cpp file can create mess in your project organization.
Template functions must be defined in the same file where they are declared.
NOTE:
There are ways around it like using export keyword or including a cpp file with function definitions just after template class declarations,but they can really complicate your life.Export templates keyword is not supported in all compilers and including a definition cpp file can create mess in your project organization.
Thanks! moving the definition did help. However, it turns out that the compiler doesn't like the use of the template type inside the switch statements so this code is not good anyway... On to a different approach.
There are ways around it like using export keyword
Quick tell me the name of any compiler that supports this. I have been telling my students there are no such compilers. Maybe I need an update.
Quick tell me the name of any compiler that supports this. I have been telling my students there are no such compilers. Maybe I need an update.
I'm not sure if Intel's 7.x compiler does support it,but I'm sure Comeau does.
Not very wide support indeed, making export for templates pretty useless.
Not very wide support indeed, making export for templates pretty useless.
Support or not, export for templates is very useless. Read up on Google on the problem with export. It does not work like you think it does.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David Rubin |
last post by:
I get an error when I compile the following code:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
string&
|
by: Thomas Matthews |
last post by:
Hi,
I would like to create a table (or vector) of pointers
to templated functions.
1. How do I declare a typedef of a pointer to a templated
function?
For example, I have some functions...
|
by: Patrick Kowalzick |
last post by:
Dear all,
IMO the following program is legal and should compile.
//***** code start *********
// Definition of class A
template <typename T> struct A
{
typedef T value_type;
};
|
by: Alex Drummond |
last post by:
Hello,
Is there any way of specializing a templated function on a type which
is itself templated? Here's the simplest example of the problem I can
think of. Say I have written an implementation...
|
by: shaun |
last post by:
Dear all,
I realized an error in a previous post, I reproduce it here because I'm
still not sure how to solve it:
I want to make a templated function which points to one-past-the-end of
a...
|
by: mrstephengross |
last post by:
Hi folks... I've been sifting through archived postings on this, but
haven't quite found an answer yet. I've got a templated stand-alone
function:
template<typename Tvoid access(const T & t) { ;...
|
by: giganut |
last post by:
Hi,
I'm using MS VS.Net2003, and I'm getting a linker error in a project for a
templated friend function
Specifically:
template <class Tclass Vec3
{...
|
by: john_owens |
last post by:
I have a templated function called splitAndSegment (templated on the
datatype). It's declared in a header file. I then include that header
file in two separate cpp files and link them separately....
|
by: Rahul |
last post by:
Hi Everyone,
I have the following polymorphic classes,
class Shape
{
public : virtual void draw()
{
}
virtual void sample();
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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: 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...
|
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,...
| |