C++ C++;分段故障问题
如果有人能帮我,告诉我为什么会这样,我会非常感激。谢谢 注意:我不允许以任何方式修改C++ C++;分段故障问题,c++,C++,如果有人能帮我,告诉我为什么会这样,我会非常感激。谢谢 注意:我不允许以任何方式修改main功能 #include <iostream> #include <algorithm> #include <functional> #include <list> #include <cmath> class Object { public: private: float d; public: Object(float n) :
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();
}
#包括
#包括
#包括
#包括
#包括
类对象{
公众:
私人:
浮动d;
公众:
对象(float n):d(n){}
对象(){}
结构点类型{
浮动x2;
浮动y2;
点类型(浮点x1,浮点y1):x2(x1),y2(y1){
PointType(){}
浮点x(){return x2;}
浮点y(){返回y2;}
PointType center(){return*this;}
friend std::ostream&operator由于
汇编和执行:
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
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 $
注意,成员d在对象中是私有的,如果我添加,则无法访问它(也无法写入它)
friend std::ostream& operator<<(std::ostream& os, const Object & o) {
os << "d=" << o.d;
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
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 $
您好@bruno,谢谢。但是,在将std::endl添加到两者之后,我仍然面临seg故障问题。还有其他原因吗?@starrk问题不是endl,问题是std::ostream&operatorHi@bruno,非常感谢您的详细解释!这是商业上最好的。但是为什么在center()中返回一个实例呢与从main()开始返回点相比,保持调用自身想要中心…你介意进一步详细说明吗?我不明白。@starrk点是它的中心,因为点没有宽度/高度。你确定不想要直线或矩形等的中心,所以形状的中心由几个点定义吗?(我还重新编辑了我写d的答案)@对于多边形,starrk可能是运算符
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 $