我正在尝试为多边形类和派生的三角形类重载 << 和 >> 运算符。问题是我的编译器返回以下错误:
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,但我在下面包含了我的代码的完整拷贝,以防错误是由其他原因引起的。感谢您的帮助和耐心等待。
主要.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;
}
三角形.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);
三角形.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;
}
多边形.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);
多边形.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;
}
顶点.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);
顶点.cpp
#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
,要么将定义移到 header 中以使其在任何使用它的地方都可用。
关于c++ - 继承类中对运算符 << 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21487710/