RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 1685 | Replies: 3
Reply
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

save self-defined object in vector problem

  #1  
Jul 22nd, 2005
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: save self-defined object in vector problem

  #2  
Jul 22nd, 2005
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?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,765
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 147
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: save self-defined object in vector problem

  #3  
Jul 22nd, 2005
Originally Posted by alone2005
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.
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
Reply With Quote  
Join Date: Jun 2005
Posts: 23
Reputation: alone2005 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
alone2005's Avatar
alone2005 alone2005 is offline Offline
Newbie Poster

Re: save self-defined object in vector problem

  #4  
Jul 22nd, 2005
Originally Posted by Dave Sinkula
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?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:41 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC