在继承的类中对运算符<<的未定义引用

丹尼尔·翁(Daniel Ong)

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

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;
}

三角形

#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);

三角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;
}

多边形

#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);

多边形

#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;
}

顶点

#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);

顶点

#include "vertex.h"

std::ostream & operator << (std::ostream & os, const vertex & v)
{
    os << "(" << v.x << ", " << v.y << ")";
    return os;
}
迈克·西摩

这是您的问题:

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C ++-对基类运算符的未定义引用

来自分类Dev

jsoncpp运算符中的未定义引用=

来自分类Dev

未定义对类的引用

来自分类Dev

未定义从dll引用非成员运算符

来自分类Dev

未定义对朋友运算符的引用

来自分类Dev

来自libc ++的istringstream析构函数中对运算符delete的未定义引用

来自分类Dev

提取运算符重载中的c ++未定义引用

来自分类Dev

未定义对继承函数的引用

来自分类Dev

Smalltalk引用尚未定义的类

来自分类Dev

未定义对类方法的引用

来自分类Dev

如何引用尚未定义的类?

来自分类Dev

CMake 对类的未定义引用

来自分类Dev

JavaScript中未定义的对象引用

来自分类Dev

主Makefile中的未定义引用

来自分类Dev

Cygwin中对WinMain的未定义引用

来自分类Dev

在componentDidUpdate引用中未定义

来自分类Dev

构造函数中的未定义引用

来自分类Dev

主Makefile中的未定义引用

来自分类Dev

在main()中未定义对函数的引用

来自分类Dev

未定义对Makefile中的“函数”的引用

来自分类Dev

在Qt中未定义对QDeclarativePropertyMap的引用

来自分类Dev

GSL中的未定义引用

来自分类Dev

C Opengl中的未定义引用

来自分类Dev

对C ++中函数的未定义引用

来自分类Dev

对C ++中函数的未定义引用

来自分类Dev

在 make 中对“main”的未定义引用

来自分类Dev

对vtable的未定义引用,用于继承类

来自分类Dev

C ++:对函子的重载调用运算符的未定义引用

来自分类Dev

对静态库中的类变量的未定义引用