我有以下代码指出的问题,我不知道它可能会导致它。在发布问题之前,我搜索了一下,我发现它可能是一个超出范围的东西,就像对释放内存位置的引用,但我无法自己找到它。 谢谢你帮助我。在std :: string :: assign(std :: string const&)中的分段错误
#include<iostream>
#include<string>
using namespace std;
class USR{
private:
string name;
public:
void setName(string name){
this->name = name;
}
string getName(){
return name;
}
};
class A{
private:
USR* * a;
public:
A(int size){
a = new USR*[size];
}
USR* getUser(){
return a[0];
}
};
int main(){
A test = A(5);
USR* u = test.getUser();
(*u).setName("test");
USR* u2 = test.getUser();
cout << (*u2).getName() << endl;
cout << (*u).getName() << endl;
}
谢谢你们每一个人,我接受了比约恩的答案,因为他是第一个回复。 –