2

I've been trying to create a class cartesian which objects are 2 points on the a cartesian point (int or double). Then I want to overload <<. I get the error message :

Undefined symbols for architecture x86_64:
  "cartesian<double, int>::cartesian(double, int)", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I dont understand where is My mistake.

THE HEADER

#include <iostream>    

using namespace std;

template <class first, class second>
class cartesian
{

public:
    cartesian(first, second);

    //double getslope(cartesian &obj1, cartesian &obj2);

    friend ostream& operator<< (ostream &out, cartesian &cPoint);                

private:
    first x;
    second y;

};

THE CPP file

#include "cartesian.h"
#include <iostream>

using namespace std;


template<class first,class second>

cartesian<first, second>::cartesian(first a, second b)
:x(a), y(b)
{}

/*
// between obj1 and obj2
template<class first,class second>
double cartesian<first, second>::getslope(cartesian &obj1, cartesian &obj2){

    return ((obj2.y-obj1.y)/(obj2.x-obj1.y));
}

*/


template<class first,class second>

ostream&  operator<< (ostream &out,const cartesian<first, second> &cPoint)
{
    // Since operator<< is a friend of the Point class, we can access
    // Point's members directly.
    out << "(" << cPoint.x << ", " <<
    cPoint.y << ")";

    return out;
}

THE MAIN

#include <iostream>
#include "cartesian.h"

using namespace std;

int main()
{

    cartesian<double, int> ob11(3.4, 6);
    return 0;
}
EEstud
  • 309
  • 1
  • 3
  • 12
  • possible duplicate of [C++ template, linking error](http://stackoverflow.com/questions/1353973/c-template-linking-error) – juanchopanza Aug 19 '12 at 17:30
  • 1
    The problem is that working with large templated projects in C++ is terrible. Try `#include cartesian.cpp` in main. – James Aug 20 '12 at 00:14

1 Answers1

4

You need to put the implementation in the header file or in a file included by the header. The compiler needs access to the code in order to "build" the cartesian<double, int> specialization you require in main.

For example, here we put the constructor's implementation in the class declaration:

template <class first, class second>
class cartesian
{

public:
    cartesian(first, second) :x(a), y(b) {}
};

It doesn't have to go inside the class declaration itself, but the code must be accessible from the header file.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • What to you mean by putting the implementation in the header file? what implementation? sorry for my noobness. – EEstud Aug 19 '12 at 17:32
  • @EEstud I mean the definitions of the functions, i.e. the stuff you currently have in the ``.cpp`` file. – juanchopanza Aug 19 '12 at 17:35
  • So, I need to construct my functions in the header file as well? Normally we do this in the cpp, but because it is a template, I need to construct it in the header file? Did I understand well? – EEstud Aug 19 '12 at 18:01
  • @EEstud correct, and remove the ``.cpp``, some build systems might try to compile it automatically. – juanchopanza Aug 19 '12 at 19:08