Undefined reference error with template class.

Hello all,

I know this shouldn't be as difficult as it is simple but for some reason the solution eludes me and I've tried hacking this one for hours (yes hours) now.

Here's the code

object.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef OBJECT_H
#define OBJECT_H

template <const int value>
class object
{
    private:

    public:

    object();
    object( int dummy);
    ~object();
};
#endif 


object.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "object.h"
#include <iostream>

template <const int value>
object<value>::object()
{
    std::cout<<"value: "<<value<<std::endl;
}

template <const int value>
object<value>::object( int dummy)
{
    std::cout<<dummy<<" value: "<<value<<std::endl;
}

template <const int value>
object<value>::~object()
{

}



main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "object.h"

const int value = 1;

int main()
{
    object<value>* a();//compiles but doesn't output anything

    //a = new object< value>( value + 1);//compiler error only if H and cpp 
    //are separate   

    return 0;
}


I'm running this on Ubuntu Linux with the g++ compiler.
Last edited on
You cannot hide template implementations into .cpp (trivially). Keep them in the header.


Note: line 8 in main.cpp declares a function; see "C++ most vexing parse".
Thanks keskiverto,

I moved the methods from the cpp file below the class definitions (outside of the class) and it compiles and runs!
Topic archived. No new replies allowed.