Computer Science Canada

Template Linking Help

Author:  Saad [ 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.

Author:  A.J [ 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!!!!!

Author:  Clayton [ 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.

Author:  A.J [ 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........

Author:  Saad [ 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

Author:  fishtastic [ 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.)

Author:  wtd [ 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.

Author:  Saad [ Tue Mar 18, 2008 5:52 am ]
Post subject:  RE:Template Linking Help

Oh I see, Thanks a bunch.

Author:  OneOffDriveByPoster [ Thu Mar 20, 2008 9:33 am ]
Post subject:  Re: Template Linking Help

If only I had access to a working implementation of export...


: