简体   繁体   English

无法输出Line C ++的对象点

[英]Can't output object points of a Line C++

Hi I have the following code where i have to pass object Point into line and provide the X & Y Output for the line with it's point. 嗨,我有以下代码,我必须将对象Point传递到线中,并为该点的线提供X&Y Output。 however the output of my code is -842150451 for all the X and Y co-ordinates, alternatively i can output just the two points however i am unsure how to do this. 但是,对于所有X和Y坐标,我的代码的输出为-842150451,或者我可以仅输出两个点,但是我不确定如何执行此操作。

Note: I cannot pass integers into Line, i need to place Object Point into line. 注意:我无法将整数传递到Line中,我需要将Object Point放置到line中。 Note*: i need to use a vector to store 10 lines with random points. 注意*:我需要使用向量来存储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;
}

I think you need to review your code, 我认为您需要检查您的代码,

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

This loop pushed one line 10 times in a vector?!? 此循环在向量中推了10条线?!?

Hi thanks for for the answer, i was creating 10 lines and storing them in a vector. 嗨,谢谢您的回答,我正在创建10行并将其存储在向量中。 I then printed out the vector. 然后,我打印出矢量。 all fixed now =) 全部固定=)

include 包括

include 包括

include "Point.h" 包括“ Point.h”

include "Line.h" 包括“ Line.h”

int main(){ vector lineVector; int main(){向量lineVector;

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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM