我想用x和y值填充我的向量。但似乎并没有增加,只是首先覆盖了 。C++不能填充数据
的main.cpp
#include <iostream>
#include "Point.h"
using namespace std;
int x,y;
Point point;
string options;
void someMethods();
int main()
{
cout << "Please Enter x-Cordinate"<< endl;
cin >> x;
cout << "Please Enter y-Cordinate" << endl;
cin >> y;
cout << "Enter cords again? yes/no"<< endl;
cin >> options;
while (options == "yes") {
cout << "Please Enter x-Cordinate"<< endl;
cin >> x;
cout << "Please Enter y-Cordinate" << endl;
cin >> y;
cout << "Enter cords again? yes/no"<< endl;
cin >> options;
}
if(options == "no") {
Point Point(x,y);
Point.someMethods();
// break;
}
}
Point.h
#ifndef Point_Point_h
#define Point_Point_h
#include <vector>
class Point {
private:
int x,y;
public :
Point() {
x = 0;
y = 0;
} //default consrructor
Point(int x,int y);
int getX();
int getY();
void setX(int x);
void setY(int y);
std::vector<Point> storeData;
void someMethods();
};
#endif
Point.cpp
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(int x,int y) {
setX(x);
setY(y);
}
int Point::getX() {
return x;
}
int Point::getY() {
return y;
}
void Point::setX(int x) {
this->x = x;
}
void Point::setY(int y) {
this->y = y;
}
void Point::someMethods() {
x = getX();
y = getY();
Point Point(x,y);
storeData.push_back(Point);
for (int i=0; i<storeData.size(); i++) {
cout << "X "<< storeData[i].getX() <<"Y " << storeData[i].getY() << endl;
}
// do some methods here after getting the x and y cords;
}
我怎样才能让这样如(我输入x和y的3倍比方说1,1 2,2 3,3)
则输出
X: 1,Y: 1
X: 2,Y: 2
X: 3,Y: 3
这是因为您*覆盖了第一个值 - 数据仅在最后一次输入后才会输出。另外,你是否熟悉'do..while'循环?这在这里很有用。 – 2013-10-11 17:11:00
C++新手?一些基本的指针:避免使用名称空间标准,避免使用全局变量,并且Get/Set方法的使用与其他语言(如Java)不同。 – crashmstr
是的我是新来的C++,我慢慢学习它 – user2211678