Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Template Linking Help
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Saad




PostPosted: Mon Mar 17, 2008 8:46 pm   Post subject: Template Linking Help

In file Foo.h

c++:

template<int Qux> class Foo {
    public:
    void baz();
};


In file Foo.cpp

c++:

#include <iostream>

#include "Foo.h"

template <int Qux>
void Foo<Qux>::baz() {
    std::cout << Qux << std::endl;
}


In main.cpp

c++:

#include "Foo.h"

int main(){
    Foo<10> ninja;
    ninja.baz();
    return 0;
}



code:

g++ Main.cpp Foo.cpp
>g++ Main.cpp Foo.cpp
C:\DOCUME~1\Saad\LOCALS~1\Temp/ccABbaaa.o:Main.cpp:(.text+0x31): undefined reference to `Foo<10>::baz()'
collect2: ld returned 1 exit status
>Exit code: 1



However if I change Foo.h to

c++:

template<int Qux> class Foo {
    public:
    void baz();
};
#include "Foo.cpp"


It compiles

Can't anyone provide some insight to why this occurs?

Furthermore, is there a way to force makefile to use the updated version of Foo.cpp, since when I change the definition of Foo.cpp, Foo.h isn't modified and uses the old file.
Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: Mon Mar 17, 2008 9:17 pm   Post subject: Re: Template Linking Help

Seriously saad....what's with the craze of 'foobaz' the past few..umm..MONTHS!!!!!
Clayton




PostPosted: Mon Mar 17, 2008 9:21 pm   Post subject: RE:Template Linking Help

What is wrong with it? It's just a generic (and fairly standard) way to name things in example code.
A.J




PostPosted: Mon Mar 17, 2008 9:29 pm   Post subject: Re: Template Linking Help

yeah sure, but if you know saad like I do, you'll notice that he uses it for more than examples........
Saad




PostPosted: Mon Mar 17, 2008 9:37 pm   Post subject: Re: Template Linking Help

A.J @ Mon Mar 17, 2008 9:29 pm wrote:
yeah sure, but if you know saad like I do, you'll notice that he uses it for more than examples........


Care to show me some examples?.

Furthermore, can we stay on topic? Smile
fishtastic




PostPosted: Mon Mar 17, 2008 10:09 pm   Post subject: RE:Template Linking Help

OMG Saad ! what are you doing with all the foos bars and ninjas? Shocked

here is my guess..

before change:
in main.cpp
#include "Foo.h" got all the stuff from "foo.h"
however in "foo.h", "baz" was not fully defined. and "main.cpp" was never linked with "foo.cpp." so it didn't know where to find more stuff.

but after the change:
"foo.h" can find further definition of "baz" inside "foo.cpp" so it worked.

btw i never used template probably will not use them unless i am making RPG in c++
(which will not likely to happen in the recent years.)
wtd




PostPosted: Tue Mar 18, 2008 12:04 am   Post subject: RE:Template Linking Help

I can do a bit better than guess. Smile

Non-templated classes in C++ get are typically divided into header and implementation files. This is done so that separate compilation is possible.

Definitions of templated class members do not give you code that can be compiled, and as such should not go in implementation files.

As an example, Saad's Foo.cpp file cannot be compiled separately. Once a template is instantiated, the compiler automatically generates code that can be compiled.
Saad




PostPosted: Tue Mar 18, 2008 5:52 am   Post subject: RE:Template Linking Help

Oh I see, Thanks a bunch.
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: Thu Mar 20, 2008 9:33 am   Post subject: Re: Template Linking Help

If only I had access to a working implementation of export...
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: