linker error: undefine reference to <every single templated function>
everything's in the same directory...i even have object files for them...
am i just stupid or what? ..........................(sorry for posting such a long post.......oh yea, i know i have not implemented the destructors, but it says nothing else is defined either so, wtf?)
main.cpp
Code:
#include <iostream>
#include <stdlib.h>
#include "cbox.h"
using namespace std;
template <typename Number>
ostream & operator <<(ostream &ostr, const cRectangle2<Number> &srcRect)
{
ostr << srcRect.getLength() << "x" << srcRect.getWidth();
}
template <typename Number>
ostream & operator <<(ostream &ostr, const cBox<Number> &srcBox)
{
ostr << srcBox.getLength << "x" << getWidth() << "x" << srcBox.getHeight();
}
template <typename Number>
Number volume(const cBox<Number> &srcBox)
{
ostr << (length * width * height);
}
void dummy()
{
cout << "non template functoion" << endl;
}
int main(int argc, char *argv[])
{
cout.setf(ios::fixed | ios::showpoint);
cout.precision(1);
dummy();
cBox<double> b(4.0, 5.0, 9.0);
cout << "Output using b with rectangle and box member functions\n";
cout << "b is " << b << endl;
cout << "The length is " << b.getLength() << endl;
cout << "The width is " << b.getWidth() << endl;
cout << "The height is " << b.getHeight() << endl;
cout << "The surface area is " << b.area() << endl;
cout << "The area of the base is " << b.cRectangle2<double>::area();
cout << "The volume is " << volume(b) << endl;
cout << "The total perimeter of all sides is " << b.perimeter() << endl;
cout << "The perimeter of the base is " << b.cRectangle2<double>::area();
system("PAUSE");
return 0;
}
rect2.h
Code:
#ifndef cRECT2_H
#define cRECT2_H
#include <iostream>
using namespace std;
template <typename Number>
class cRectangle2
{
friend ostream & operator <<(ostream &, const cRectangle2<Number> &);
public:
cRectangle2(void);
cRectangle2(Number, Number);
cRectangle2(const cRectangle2<Number> &);
~cRectangle2(void);
Number perimeter(void) const;
Number area(void) const;
cRectangle2<Number> & setWidth(Number);
cRectangle2<Number> & setLength(Number);
Number getWidth(void) const;
Number getLength(void) const;
private:
Number length;
Number width;
};
#endif
rect2.cpp
Code:
#include "rect2.h"
using namespace std;
template <typename Number>
cRectangle2<Number>::cRectangle2(void)
{
length = 1;
width = 1;
}
template <typename Number>
cRectangle2<Number>::cRectangle2(Number l, Number w)
{
length = l;
width = w;
}
template <typename Number>
cRectangle2<Number>::cRectangle2(const cRectangle2<Number> &srcRect)
{
*this = srcRect;
}
template <typename Number>
cRectangle2<Number>::~cRectangle2(void)
{
//this is pointless
}
template <typename Number>
Number cRectangle2<Number>::perimeter(void) const
{
return ((length*2)+(width*2));
}
template <typename Number>
Number cRectangle2<Number>::area(void) const
{
return (length * width);
}
template <typename Number>
cRectangle2<Number> & cRectangle2<Number>::setWidth(Number w)
{
width = w;
return *this;
}
template <typename Number>
cRectangle2<Number> & cRectangle2<Number>::setLength(Number l)
{
length = l;
return *this;
}
template <typename Number>
Number cRectangle2<Number>::getWidth(void) const{ return width; }
template <typename Number>
Number cRectangle2<Number>::getLength(void) const{return length; }
cbox.h
Code:
#ifndef cBOX_H
#define cBOX_H
#include "rect2.h"
#include <iostream>
using namespace std;
template <typename Number>
class cBox: public cRectangle2<Number>
{
friend ostream & operator<<(ostream &, const cBox<Number> &);
friend Number volume(const cBox<Number> &);
public:
cBox(Number = 1.0, Number = 1.0, Number = 1.0);
cBox(const cBox<Number> &);
~cBox(void);
Number perimeter(void) const;
Number area(void) const;
cBox<Number> & setHeight(Number);
Number getHeight(void) const;
private:
Number height;
};
#endif
cbox.cpp
Code:
#include "cbox.h"
template <typename Number>
cBox<Number>::cBox(Number L, Number W, Number H)
{
setLength(L);
setWidth(W);
height = H;
}
template <typename Number>
cBox<Number>::cBox(const cBox<Number> &srcBox)
{
*this = srcBox;
}
template <typename Number>
cBox<Number>::~cBox(void){ /*this is pointless*/ }
template <typename Number>
Number cBox<Number>::perimeter(void) const
{
return (cBox::perimeter() + (2 * getWidth()));
}
template <typename Number>
Number cBox<Number>::area(void) const
{
return (cBox::area()*2) + (getHeight() * getWidth() * 2) + (getHeight() * getLength() * 2);
}
template <typename Number>
cBox<Number> & cBox<Number>::setHeight(Number H)
{
height = H;
return *this;
}
template <typename Number>
Number cBox<Number>::getHeight(void) const
{
return height;
}