未明确引用运算符<<在继承的类中

时间:2014-01-31 18:45:09

标签: c++ class inheritance operator-overloading

我正在尝试重载<<和>>多边形类和派生三角形类的运算符。问题是我的编译器返回以下错误:

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

我相当确定我确实定义了上面的运算符。我在triangle.h文件中有以下行:

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

此问题专门针对三角类发生。当我删除试图输出三角形的线时,我的程序正常工作。输出多边形没有问题。是什么导致了这个问题,我该如何解决?这是我的包含层次结构的问题吗?

我认为相关的文件是main.cpp,triangle.h和triangle.cpp,但我在下面包含了我的代码的完整副本,以防错误是由其他原因造成的。感谢您的帮助和耐心。

的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;
    for (size_t i = 0; i < 3; ++i)
    {
        is >> 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 and initialized to given polygon source
    polygon(const polygon & source);

    // pre:
    // post: return number of vertices in this polygon
    std::size_t size() const;

    // pre: 0 <= i < size()
    // post: return vertex i in this polygon
    vertex operator[](size_t i) const;

    // pre:
    // post: vertex is added to this 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)
{
    for (std::size_t i = 0; i < p.size(); ++i)
        os << p[i] << " ";
    return os;
}

std::istream & operator >> (std::istream & is, polygon & p)
{
    std::size_t n;
    vertex vx;

    is >> n;
    for (size_t i = 0; i < n; ++i)
    {
        is >> 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;
}

1 个答案:

答案 0 :(得分:2)

这是你的问题:

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

必须在使用它们的所有翻译单元中定义内联函数,并且这仅在一个中定义。删除inline,或将定义移到标题中,使其在使用的任何位置都可用。