如果有人能够帮助我并告诉我它为什么存在段错误,我将非常感谢。谢谢!
注意:不允许以任何方式修改main
函数。
#include <iostream>
#include <algorithm>
#include <functional>
#include <list>
#include <cmath>
class Object {
public:
private:
float d;
public:
Object(float n) : d(n) {}
Object() {}
struct PointType {
float x2;
float y2;
PointType(float x1, float y1) : x2(x1), y2(y1) {}
PointType() {}
float x() { return x2; }
float y() { return y2; }
PointType center() { return *this; }
friend std::ostream &operator<<(std::ostream &os, const PointType &p) {
os << p;
return os;
}
};
virtual void render() const {}
virtual ~Object() {}
};
class Point : public Object {
private:
PointType mpoint;
public:
Point(const PointType &pt, float &y1) : Object(y1), mpoint(pt) {}
Point(const PointType &pt) : mpoint(pt) {}
Point() {}
Point center() const { return *this; }
float x() { return mpoint.x2; }
float y() { return mpoint.y2; }
friend std::ostream &operator<<(std::ostream &os, const Point &p) {
os << p.center();
return os;
}
};
class VisiblePoint : public Point {
public:
VisiblePoint(const Object::PointType &point) : Point(point) {}
void render() const override {
std::cout << "Point at " << center() << std::endl;
}
};
// A simple pseudo-random number generator.
class PseudoRandom {
const int A = 101;
const int B = 919;
const int C = 10619863;
int _current;
public:
PseudoRandom() : _current(0) {}
int operator()() {
_current = (A * _current + B) % C;
_current += (_current < 0) ? C : 0;
return _current;
}
int operator()(int n) { return (*this)() % n; }
float operator()(float a, float b) {
int result = (*this)();
return a + (b - a) * static_cast<float>(result) / float(C);
}
};
int main() {
std::list<Object *> objects;
PseudoRandom random;
for (int i = 0; i < 10; ++i) {
switch (random(1)) {
case 0: {
const Object::PointType point(random(0, 1), random(0, 1));
objects.push_back(new VisiblePoint(point));
break;
}
case 1: {
}
case 2: {
}
case 3: {
}
case 4: {
}
}
}
std::cout << "BEGIN" << std::endl;
std::for_each(objects.begin(), objects.end(), std::mem_fn(&Object::render));
std::cout << "END" << std::endl;
for (const Object *object : objects) {
delete object;
}
objects.clear();
}
答案 0 :(得分:1)
由于以下原因,您的堆栈溢出
friend std::ostream& operator<<(std::ostream& os, const Point& p) { os << p.center(); return os; }
调用自身而没有结束,因为Point::center()
返回一个 Point ( center 仅返回该实例)
对于 PointType ,您有相同的堆栈溢出问题,可以将这些定义更改为例如:
friend std::ostream& operator<<(std::ostream& os, const Point& p)
{
os << p.mpoint;
return os;
}
friend std::ostream& operator<<(std::ostream& os, const
PointType& p)
{
os << "x=" << p.x2 << ", y=" << p.y2;
return os;
}
编译和执行:
pi@raspberrypi:/tmp $ g++ -g -pedantic -Wextra -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
BEGIN
Point at x=0.00882667, y=0.89158
Point at x=0.015971, y=0.613159
Point at x=0.845152, y=0.360472
Point at x=0.186335, y=0.819929
Point at x=0.108734, y=0.982257
Point at x=0.0107827, y=0.0891412
Point at x=0.337889, y=0.126905
Point at x=0.567967, y=0.364747
Point at x=0.788968, y=0.685901
Point at x=0.889105, y=0.799651
END
请注意,成员 d 在 Object 中是私有的,如果我添加
,则无法访问它(也不能编写它)friend std::ostream& operator<<(std::ostream& os, const Object & o) {
os << "d=" << o.d;
return os;
}
并将 Point 的operator<<
修改为:
friend std::ostream& operator<<(std::ostream& os, const Point& p)
{
os << p.mpoint << ", ";
os << (const Object &) p;
return os;
}
并在 Object 的默认构造函数中初始化 d :
Object() : d(0) {}
编译和执行:
pi@raspberrypi:/tmp $ g++ -g -pedantic -Wextra -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
BEGIN
Point at x=0.00882667, y=0.89158, d=0
Point at x=0.015971, y=0.613159, d=0
Point at x=0.845152, y=0.360472, d=0
Point at x=0.186335, y=0.819929, d=0
Point at x=0.108734, y=0.982257, d=0
Point at x=0.0107827, y=0.0891412, d=0
Point at x=0.337889, y=0.126905, d=0
Point at x=0.567967, y=0.364747, d=0
Point at x=0.788968, y=0.685901, d=0
Point at x=0.889105, y=0.799651, d=0
END
pi@raspberrypi:/tmp $