【发布时间】:2015-12-30 01:21:32
【问题描述】:
我有一个名为test 的类,我想将一个大向量与大约百万个元素相关联。我尝试通过将指针传递给构造函数来做到这一点:
#include <iostream>
#include <vector>
using namespace std;
class test{
public:
vector<double>* oneVector;
test(vector<double>* v){
oneVector = v;
}
int nElem(){return oneVector->size();}
};
int main(){
vector<double> v(1000000);
cout << v.size() << endl;
vector<double>* ptr;
test t(ptr);
cout << t.nElem()<< endl;
return 0;
}
但是,这会导致Segmentation Fault:11,这正是我尝试执行t.nElem() 的时候。可能是什么问题?
【问题讨论】:
-
用
-Wall重新编译,你的编译器会告诉你哪里出了问题。 -
你认为指针指向什么?
-
@immibis 什么都没有。我才意识到这一点。谢谢。
标签: c++ class oop pointers vector