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

Linker problem with templated function

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
Expand|Select|Wrap|Line Numbers
  1.  
  2. enum TypeEnum
  3.    {
  4.       eDouble,
  5.       eInteger,
  6.       eBool,
  7.       eString,
  8.       eFloat,
  9.       eLong,
  10.       TypeEnumRange
  11.    };
  12.  
  13. template <class T>
  14. class DataBusGenericOutputType
  15. {
  16.       public:
  17.  
  18.          DataBusGenericOutputType();
  19.  
  20.          void    SetValue(T data){ GT->SetValue(data); } ;
  21.  
  22.       private:
  23.  
  24.          DataBusGenericType<T>* GT;
  25. };
  26.  
  27. class DataBus
  28. {
  29.    public:
  30.  
  31.    ......
  32.  
  33.       template <typename T> DataBusGenericOutputType<T> *Register2(std::string ID, TypeEnum type, T value);
  34.  
  35.    ......
  36.  
  37.    private:
  38.  
  39.    ......
  40.  
  41.         std::list< DataBusGenericType<double> >  DoubleList;
  42.         std::list< DataBusGenericType<string> >  StringList;
  43.         std::list< DataBusGenericType<int>    >  IntList;
  44.         std::list< DataBusGenericType<bool>   >  BoolList;
  45.         std::list< DataBusGenericType<float>  >  FloatList;
  46.         std::list< DataBusGenericType<long>   >  LongList;
  47.  
  48.    ......
  49. };
  50.  
file DB.cpp
Expand|Select|Wrap|Line Numbers
  1. template <typename T> 
  2. DataBusGenericOutputType<T> *DataBus::Register2(std::string ID, TypeEnum type, T value)
  3. {
  4.    DataBusGenericOutputType<T> *OutType = NULL;
  5.    DataBusGenericType<T>       *GenType = NULL;
  6.    std::vector<string> IDparts;
  7.  
  8.    IDparts = ParseID(ID); 
  9.  
  10.    GenType = new DataBusGenericType<T>(IDparts[ClassLocation], IDparts[VariableLocation], IDparts[TypeLocation], type, value);
  11.  
  12.    switch(type)
  13.    {
  14.       case eDouble:
  15.            DoubleList.push_back(*GenType);
  16.            OutType = new DataBusGenericOutputType<T>(&DoubleList.back());
  17.            break;
  18.  
  19.       case eInteger:
  20.            IntList.push_back(*GenType);
  21.            OutType = new DataBusGenericOutputType<T>(&IntList.back());
  22.            break;
  23.  
  24.       case eBool:
  25.            BoolList.push_back(*GenType);
  26.            OutType = new DataBusGenericOutputType<T>(&BoolList.back());
  27.            break;
  28.  
  29.       case eString:
  30.            StringList.push_back(*GenType);
  31.            OutType = new DataBusGenericOutputType<T>(&StringList.back());
  32.            break;
  33.  
  34.       case eFloat:
  35.            FloatList.push_back(*GenType);
  36.            OutType = new DataBusGenericOutputType<T>(&FloatList.back());
  37.            break;
  38.  
  39.       case eLong:
  40.            LongList.push_back(*GenType);
  41.            OutType = new DataBusGenericOutputType<T>(&LongList.back());
  42.            break;
  43.  
  44.       default: 
  45.            break;
  46.  
  47.    }
  48.    return OutType;
  49. }
  50.  
file Dummy.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "DB.h"
  2.  
  3. void Dummy::Register()
  4. {
  5.    DataBus *DB = new DataBus();
  6.    double mDummyDouble;
  7.    DataBusGenericOutputType<double> *oDummyDouble;
  8.  
  9. oDummyDouble = DB->Register2("Dummy.DummyDouble.double", eDouble, mDummyDouble); // <---This is line 115 which the linker error references
  10.  
  11. ....
  12. }
  13.  

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!
Jul 21 '08 #1
7 1606
Savage
1,764 Expert 1GB
That function is not a class method.It's outside of DataBusGenericOutputType class scope..
Jul 21 '08 #2
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!
Jul 21 '08 #3
Savage
1,764 Expert 1GB
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.
Jul 21 '08 #4
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.
Jul 22 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 22 '08 #6
Savage
1,764 Expert 1GB
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.
Jul 22 '08 #7
oler1s
671 Expert 512MB
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.
Jul 22 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

13
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&
2
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...
13
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; };
2
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...
9
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...
3
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) { ;...
1
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 {...
3
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....
3
by: Rahul | last post by:
Hi Everyone, I have the following polymorphic classes, class Shape { public : virtual void draw() { } virtual void sample();
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
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...
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
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...
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...
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.