As usual, the answer is probably in the code that you haven't show. Try paring the code down to a minimum that demonstrates the problem, but is yet fully compileable as-is. Then we can provide real answers. Otherwise we play 20 questions until you randomly post the problem spot. As a bonus, you may receive advice on how to better other unrelated portions of your code.
Sorry i didn't post the full code only because it involve too much other classes, I will try to make a compilable short version here: 3 files, Command.h, Command.cpp, test.cpp
#ifndef _USER_COMMAND_
#define _USER_COMMAND_
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class UserCommand{
string cHandle; //definition of command
int numPara;
vector<int> cPara; //parameter of command
public:
UserCommand();
UserCommand(string ucData);
//copy constructor
UserCommand(const UserCommand& uc);
void commandParser(string ucData);
inline string getHandle() const {return cHandle;}
inline int getPara(int i)const {return cPara[i];}
inline int getNumPara() const {return numPara;}
}; //end of class definition
#endif
#include "Command.h"
using namespace std;
UserCommand::UserCommand():cHandle(""), numPara(0) { }
UserCommand::UserCommand(string ucData):cHandle(""), numPara(0){
commandParser(ucData);
}
//Copy constructor
UserCommand::UserCommand(const UserCommand& ucc){
cHandle=ucc.cHandle;
numPara=ucc.numPara;
for(int i=0;i<ucc.numPara;i++) cPara[i] = ucc.cPara[i];
}
void UserCommand::commandParser(string ucData){
//in fact this function will extract para from string, now just make it simple
cHandle="handle";
numPara=5;
for(int i=0; i<5; i++) cPara.push_back(i);
}
#include "Command.h"
using namespace std;
int main(){
UserCommand uc;
vector<UserCommand> ucv;
string input=" ";
uc.commandParser(input);
ucv.push_back(uc); //segmentation fault here
return 0;
}
After long debug I found that if I delete the copy constructor, it will work.
Or if I don't delelte cc, then only initialize <cHandle> in UserCommand::commandParser( ) method and leave <cPara> empty, it will work too. So I must have some missing
knowledge about copy constructor?