c++ - Undefined reference to operator << in inherited class -


i'm trying overload << , >> operators polygon class , derived triangle class. problem compiler returning following error:

error: undefined reference `operator<<(std::ostream&, triangle const&)' 

i did define above operator however. have following line in triangle.h file:

std::ostream & operator << (std::ostream & os, triangle const&); 

this problem occurs triangle class. when remove line trying output triangle, program works correctly. there no problem outputting polygon. causing problem , how fix it? problem include hierarchy?

i think relevant files main.cpp, triangle.h, , triangle.cpp included full copy of code below in case error caused else. thank , patience.

main.cpp

#include <iostream> #include "triangle.h"  using namespace std;  int main() {     triangle t (vertex (0, 0), vertex (5, 0), vertex (0, 5));     triangle l (t);      cout << l[0] << endl;     cout << "l: " << l << endl; //not working      polygon p;     p.add (vertex (0,0));     cout << "p: " << p << endl; //working     return 0; } 

triangle.h

#include "polygon.h" #include <iostream>  class triangle : public polygon { public:     triangle(vertex = vertex(), vertex = vertex(), vertex = vertex());      triangle(const triangle &);      double area() const;      vertex operator[](size_t i) const;  private:     size_t size() const;     void add(vertex iv);     std::vector<vertex> v; };  std::ostream & operator << (std::ostream & os, triangle const&); std::istream & operator >> (std::istream & is, triangle & t); 

triangle.cpp

#include "triangle.h" #include <math.h> #include <cassert>   triangle::triangle(vertex ta, vertex tb, vertex tc) {     v.push_back(ta);     v.push_back(tb);     v.push_back(tc); }  triangle::triangle(const triangle & t) {     v=t.v; }  double triangle::area() const {     double a=sqrt((v[0].x-v[1].x)*(v[0].x-v[1].x)+(v[0].y-v[1].y)*(v[0].y-v[1].y));     double b=sqrt((v[1].x-v[2].x)*(v[1].x-v[2].x)+(v[1].y-v[2].y)*(v[1].y-v[2].y));     double c=sqrt((v[2].x-v[0].x)*(v[2].x-v[0].x)+(v[2].y-v[0].y)*(v[2].y-v[0].y));     double s=((a+b+c)/2);     return sqrt(s*(s-a)*(s-b)*(s-c)); }  vertex triangle::operator[] (std::size_t i) const {     assert (i<3);     return v[i]; }  inline std::ostream & operator << (std::ostream & os, triangle const & t) {     std::cout << "test" << std::endl;     return os << t[0]; }  std::istream & operator >> (std::istream & is, triangle & t) {     vertex vx;     (size_t = 0; < 3; ++i)     {         >> vx.x >> vx.y;         //t.v.push_back(vx);     }     return is; } 

polygon.h

#include <iostream> #include <vector> #include "vertex.h"  class polygon  {     public:     // pre:     // post: empty polygon created     polygon();      // pre:     // post: polygon created , initialized given polygon source     polygon(const polygon & source);      // pre:     // post: return number of vertices in polygon     std::size_t size() const;      // pre: 0 <= < size()     // post: return vertex in polygon     vertex operator[](size_t i) const;      // pre:     // post: vertex added polygon     void add(const vertex & v);      private:      std::vector<vertex> v;  };  std::ostream & operator << (std::ostream & os, const polygon & p); std::istream & operator >> (std::istream & is, polygon & p); 

polygon.cpp

#include <cassert> #include "polygon.h"  polygon::polygon() {     v = std::vector<vertex> (); }  polygon::polygon(const polygon & p) {     v = p.v; }  std::size_t polygon::size() const {     return v.size(); }  vertex polygon::operator[] (std::size_t i) const {     assert(i < size());     return v[i]; }  void polygon::add(const vertex & vx) {     v.push_back(vx); }  std::ostream & operator << (std::ostream & os, const polygon & p) {     (std::size_t = 0; < p.size(); ++i)         os << p[i] << " ";     return os; }  std::istream & operator >> (std::istream & is, polygon & p) {     std::size_t n;     vertex vx;      >> n;     (size_t = 0; < n; ++i)     {         >> vx.x >> vx.y;         p.add(vx);     }     return is; } 

vertex.h

#include <iostream>  struct vertex {     double x, y;      vertex(double ix = 0.0, double iy = 0.0)     {         x = ix;         y = iy;     } };  std::ostream & operator << (std::ostream & os, const vertex & v); 

vertex.cpp

#include "vertex.h"  std::ostream & operator << (std::ostream & os, const vertex & v) {     os << "(" << v.x << ", " << v.y << ")";     return os; } 

here's problem:

// triangle.cpp inline std::ostream & operator << (std::ostream & os, triangle const & t) ^^^^^^ 

inline functions must defined in translation units use them, , defined in one. either remove inline, or move definition header make available anywhere used.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -