0

I define a class which hold some variables first:

class Command{
	string cHandle;			
	int numPara;
	vector<int> cPara;		
public:
	Command();
	Command(string ucData);
        //...............more functions here	
}; //end of class definition

Then I declare an object of Command and wanna store it in a vector

vector<Command> cv;
Command c;
//... some operation of c, tested ok
cv.push_back(c);                                    //segmentation fault here

The thing is if I only initial c.cHandle, everything ok
if I initial both c.cHandle and c.cPara[ ]
I got a segmentation fault at the last line
what could be my mistake? thanks.

2
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by alone2005
0

I kind figure out where is the problem, cause I wrote a copy constructor in Command class
if I comment this function out, everything in peace

Command::Command(const Command& c){ 
	cHandle=c.getHandle();
	numPara=c.getNumPara();
	for(int i=0;i<numPara;i++) cPara[i]=c.getPara(i);
}

But now I am even more confused, cause this function is only defined but has never been
used, how could it cause a memory fault? help pls?

0

But now I am even more confused, cause this function is only defined but has never been used, how could it cause a memory fault? help pls?

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.

0

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?

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.