无法输出线条C++的对象点
Can't output object points of a Line C++
嗨,我有以下代码,我必须将对象点传递到行并提供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;
}
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 使用"std::unordereded_map"映射到"std::list"对象