vector 做参数问题?
下面是我的程序,想在两个函数中对同一个vector 做操作,怎么老出错??
#include <stdlib.h>
#include <afxwin.h>
#include <vector>
using namespace std;
typedef vector<CPoint> vectorPoint;
int AddPoint(vectorPoint* _setPoint)
{
_setPoint->push_back(CPoint(0,0));
_setPoint->push_back(CPoint(10,10));
_setPoint->push_back(CPoint(20,20));
_setPoint->push_back(CPoint(30,30));
_setPoint->push_back(CPoint(40,40));
return _setPoint->size();
}
int RemovePoint(vectorPoint* setPoint)
{
setPoint->erase(setPoint->end() - 1);
return setPoint->size();
}
int main()
{
vectorPoint* _setPoint = 0;
//_setPoint->clear();
int a= AddPoint(_setPoint);
int b = RemovePoint(_setPoint);
printf("%d,%d",a,b);
return 0;
}
[解决办法]
_setPoint是个指针啊。
你没有把它指向一个实际的对象啊
[解决办法]
#include <stdlib.h> #include <vector> using namespace std; class Point{public: Point(const int x, const int y) :m_x(x), m_y(y) { }private: int m_x; int m_y;};typedef vector<Point>vectorPoint; int AddPoint(vectorPoint* _setPoint) { _setPoint->push_back(Point(0,0)); _setPoint->push_back(Point(10,10)); _setPoint->push_back(Point(20,20)); _setPoint->push_back(Point(30,30)); _setPoint->push_back(Point(40,40)); return _setPoint->size(); } int RemovePoint(vectorPoint* setPoint) { setPoint->erase(setPoint->end()-1); return setPoint-> size(); } int main() { vectorPoint* _setPoint = 0; _setPoint=new vectorPoint; //申请空间; // _setPoint-> clear(); int a=AddPoint(_setPoint); int b=RemovePoint(_setPoint); printf("%d,%d\n",a,b); return 0; }
[解决办法]
int main() { // vectorPoint* _setPoint = 0; vectorPoint* _setPoint = new vectorPoint(); // _setPoint-> clear(); int a= AddPoint(_setPoint); int b = RemovePoint(_setPoint); printf("%d,%d",a,b); return 0; }