无法输出线条C++的对象点

Can't output object points of a Line C++

本文关键字:对象 C++ 输出      更新时间:2023-10-16

嗨,我有以下代码,我必须将对象点传递到行并提供X&y输出该线的点。但是,我的代码的输出是所有X和Y坐标的-842150451,或者,我只能输出两个点,但是我不确定该怎么做。

注意:我无法将整数进入,我需要将对象点放入线上。注意*:我需要使用矢量来存储10行随机点。

    // Creates randomly generated points on a number plane and generates lines between them.
#include <vector>
#include <iostream>
#include "Point.h"
#include "Line.h"
int main(){
    vector<Line> lineVector;
    int i;
    int counter = 1;
    Point p1;
    Point p2;
    Line *line;
    line = new Line;
    for (i = 0; i < 10; i++){
        p1.pCreate();
        p2.pCreate();
        lineVector.push_back(*line);
    }
    vector<Line>::iterator it;
    for (it = lineVector.begin(); it != lineVector.end(); ++it){
        cout << "line " << counter << endl;
        counter++;
        it->printLine();
        cout << endl;
    }
    system("PAUSE");
    return 0;
}
#ifndef __Point__
#define __Point__
#include<iostream>
#include<stdio.h>
using namespace std;
class Point{
private:
    int x, y;
public:
    Point();
    int getYPos(){ return y; }
    int getXPos(){ return x; }
    void pCreate();
};
#endif
Point::Point(){
    int x = 0, y = 0, pnumber = 0;
}
void Point::pCreate(){
    x = -50 + rand() % 100;
    y = -50 + rand() % 100;
}
#ifndef __Line__
#define __Line__
#include <iostream>
using namespace std;
class Line{
private:
    Point a, b;
public:
    Line();
    ~Line(){}
    void printLine();
};
#endif
Line::Line(){
}
void Line::printLine(){
    cout << "point1 x: " << a.getXPos() << "y: " 
        << a.getYPos() << endl
        << "point2 x: " << b.getXPos() << "y: "
        << b.getYPos() << endl;
}

我认为您需要查看您的代码,

    for (i = 0; i < 10; i++){
    p1.pCreate();
    p2.pCreate();
    lineVector.push_back(*line);
}

此循环在向量中推出了10次?!?

嗨,谢谢你的答案,我正在创建10行并将它们存储在向量中。然后,我打印出矢量。所有现在都修复了=)

包括

包括

包括" point.h"

包括" line.h"

int main(){ 向量线向量;

Point p;
int i;
int counter = 1;
for (i = 0; i < 10; i++){
    p.pCreate();
    Point p1(p.getXPos(), p.getYPos());
    p.pCreate();
    Point p2(p.getXPos(), p.getYPos());
    Line line(p1, p2);
    // input point into line
    lineVector.push_back(line);
}
vector<Line>::iterator it;
for (it = lineVector.begin(); it != lineVector.end(); ++it){
    cout << "line " << counter << endl;
    counter++;
    it->printLine();
    cout << endl;
}
system("PAUSE");
return 0;

}