Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ templates linking errors?

Name: Anonymous 2009-04-15 10:02

Hi /prog/

A nice question for you. I'd like to study C++ templates, and I've found something that sound strange. This is my simple code, it implements a matrix class:

Header file (matrix.h)

#ifndef __MATRIX_H__
#define __MATRIX_H__

template <typename T>
class Matrix {
    private:
        T **matrix;
        unsigned nrows;
        unsigned ncols;
    public:
        Matrix(unsigned nrows, unsigned ncols);
};

#endif /* __MATRIX_H__ */


Implementation file (matrix.cpp)

#include "matrix.h"

template <typename T>
Matrix<T>::Matrix(unsigned nrows, unsigned ncols)
{
    int i;

    matrix = new T* [ncols];
    for (i=0; i<ncols; i++)
        matrix[i] = new T;
}


[b]Main (main.cpp)[b]

#include <iostream>
using namespace std;

#include "matrix.h"

int main(int argc, char **argv)
{
    Matrix<int> x(10,20);
}


If I try compiling this stuff the compiler works well, but there's a linker error such

/tmp/ccBo2hkV.o: In function `main':
main.cpp:(.text+0x84): undefined reference to `Matrix<int>::Matrix(unsigned int, unsigned int)'
collect2: ld returned 1 exit status


Althrough this approach stinks, I've tried to include the '.cpp' file into main.cpp instead of including the '.h' file, and it works.

Can you explain me what's going on?

Name: Anonymous 2009-04-15 10:04

I'd like to study C++ templates, and I've found something that sound strange
Me too.

Name: Anonymous 2009-04-15 10:08

>>2

Oh really? I'm glad I've found a comrade. Does it happen the same thing to you?

Name: Anonymous 2009-04-15 10:10

>>3

You don't understand >>2.

Name: Anonymous 2009-04-15 10:11

>>4
>>3
>>2

Obvious Troll is Obvious. Back to /b/

Name: Anonymous 2009-04-15 10:13

>>5
One of these is not like the other. One of these does not belong.

Name: Anonymous 2009-04-15 10:15

>>5
Invoking X Y is X is grounds for going back to /b/, please.

HIBMT?

Name: Anonymous 2009-04-15 10:20

>>1
We do not discuss language basics here. You wouldn't ask this question if you'd actually read the STL headers you use. Go away.

Name: Anonymous 2009-04-15 10:23

I've tried to include the '.cpp' file into main.cpp instead of including the '.h' file
I wonder why? HIBT?

Name: Anonymous 2009-04-15 10:27

>>9
Basically because the problem is related to the linker, so if we put all the source code in the same object file there's no need to link it.

>>8
Tipical answer of people who are talking about programming without knowing how to program.

Name: Anonymous 2009-04-15 10:29

>>10
Typical troll and 10 year old answer.

Name: Anonymous 2009-04-15 10:37

>>11
Typical gaybrush threepwood.

Name: Anonymous 2009-04-15 10:41

>>11-12
You are making /prog/ shit! CEASE!

Name: Anonymous 2009-04-15 10:43

You cannot export template definitions between compilation units, only specific instantiations (except using the export keyword, which only the Comeau Sepples Compiler supports).

You could explicitly instantiate Matrix<int> in the main.cpp if you like, but you have to do it for each possible type of Matrix. This is one of the reasons why Sepples templates suck.

Explicit instantiation looks something like this:
template class Matrix<int>;

My advice to you: after you finish studying Sepples templates, leave Sepples and never look back.

Name: Anonymous 2009-04-15 10:49

Thanks for your answer >>14-san.

This is a pretty sucky story. Why on the earth did they do a so poor language implementation? That's ok, let's keep it as it is... we don't have no alternative, after all.

Anyway I still need to know how templates-like mechanism can be obtained.

Name: Anonymous 2009-04-15 11:37

>>15
Put the template definitions in the header file rather than having a separate compilation unit. It's how it has to be done.

Also, you might consider using a single-dimension array so that the entire matrix is one continuous block of memory. Might make it a bit easier to pass the stuff to SSE2 instructions. You weren't going to implement matrix manipulations without SSE2, were you?

Name: Anonymous 2009-04-15 12:16

Some Sepplists, when confronted with a problem, think "I know, I'll use templates." Now they have two problems.

Name: Anonymous 2009-04-15 12:51

>>17
Sepplists
problem
templates

that's three, bro

Name: Anonymous 2009-04-15 13:42

I come to this thread from distant lands to sage it.

Name: Anonymous 2009-04-15 19:01

I come to this thread from distant lands to hax your anus.

Name: Anonymous 2009-04-15 20:11

.hpp

Name: Anonymous 2009-04-16 3:04

>>16

Put the template definitions in the header file rather than having a separate compilation unit. It's how it has to be done.

Thanks for helping me, I'll try it now.

With regard to SSE optimization, don't worry about that: I'm just doing experiments, this is not going to become a great and widespread library! Thanks anyway.

Name: Anonymous 2009-04-16 4:00

OP Speaking: reading on "C++ Annotations" (http://www.icce.rug.nl/documents/cplusplus/) I've found this:

When templates are declared, the compiler will not have to process the template’s definitions again and again; and no instantiations will be created on the basis of template declarations alone. Any actually required instantiation must then be available elsewhere (of course, this holds true for declarations in general). Unlike the situation we encounter with ordinary functions, which are usually stored in libraries, it is currently not possible to store templates in libraries (although the compiler may construct precompiled header files). Consequently, using template declarations puts a burden on the shoulders of the software engineer, who has to make sure that the required instantiations exist

Name: Anonymous 2009-04-16 4:49

Name: Anonymous 2009-04-16 5:01

>>24

I owe you a beer, albeit I'm spewing out my own soul. Thanks anon.

Name: Anonymous 2009-04-16 5:51

>>25
Catch me later I'll buy you a beer

Name: Anonymous 2009-04-16 5:52

>>24
fqa
IHBT

Name: Bjarne Stroustrup 2009-04-16 5:56

>>24
Anus haxors have been dispatched to your location.

Name: HMA FAN 2009-04-16 9:32

>>28
Roger that Bjarne!

Name: Anonymous 2009-04-16 10:46

templates are so homo

Name: Anonymous 2009-04-18 4:50

bump

Name: Trollbot9000 2009-07-01 8:31

Lol are you need.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List