944,122 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3049
  • C++ RSS
Jul 22nd, 2005
0

save self-defined object in vector problem

Expand Post »
I define a class which hold some variables first:
C++ Syntax (Toggle Plain Text)
  1. class Command{
  2. string cHandle;
  3. int numPara;
  4. vector<int> cPara;
  5. public:
  6. Command();
  7. Command(string ucData);
  8. //...............more functions here
  9. }; //end of class definition

Then I declare an object of Command and wanna store it in a vector
C++ Syntax (Toggle Plain Text)
  1. vector<Command> cv;
  2. Command c;
  3. //... some operation of c, tested ok
  4. 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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
alone2005 is offline Offline
23 posts
since Jun 2005
Jul 22nd, 2005
0

Re: save self-defined object in vector problem

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
C++ Syntax (Toggle Plain Text)
  1. Command::Command(const Command& c){
  2. cHandle=c.getHandle();
  3. numPara=c.getNumPara();
  4. for(int i=0;i<numPara;i++) cPara[i]=c.getPara(i);
  5. }

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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
alone2005 is offline Offline
23 posts
since Jun 2005
Jul 22nd, 2005
0

Re: save self-defined object in vector problem

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jul 22nd, 2005
0

Re: save self-defined object in vector problem

Quote 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
C++ Syntax (Toggle Plain Text)
  1. #ifndef _USER_COMMAND_
  2. #define _USER_COMMAND_
  3.  
  4. #include <string>
  5. #include <vector>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. class UserCommand{
  11. string cHandle; //definition of command
  12. int numPara;
  13. vector<int> cPara; //parameter of command
  14. public:
  15. UserCommand();
  16. UserCommand(string ucData);
  17. //copy constructor
  18. UserCommand(const UserCommand& uc);
  19.  
  20. void commandParser(string ucData);
  21. inline string getHandle() const {return cHandle;}
  22. inline int getPara(int i)const {return cPara[i];}
  23. inline int getNumPara() const {return numPara;}
  24. }; //end of class definition
  25. #endif

C++ Syntax (Toggle Plain Text)
  1. #include "Command.h"
  2.  
  3. using namespace std;
  4.  
  5. UserCommand::UserCommand():cHandle(""), numPara(0) { }
  6.  
  7. UserCommand::UserCommand(string ucData):cHandle(""), numPara(0){
  8. commandParser(ucData);
  9. }
  10.  
  11. //Copy constructor
  12. UserCommand::UserCommand(const UserCommand& ucc){
  13. cHandle=ucc.cHandle;
  14. numPara=ucc.numPara;
  15. for(int i=0;i<ucc.numPara;i++) cPara[i] = ucc.cPara[i];
  16. }
  17.  
  18. void UserCommand::commandParser(string ucData){
  19. //in fact this function will extract para from string, now just make it simple
  20. cHandle="handle";
  21. numPara=5;
  22. for(int i=0; i<5; i++) cPara.push_back(i);
  23. }

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include "Command.h"
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7. UserCommand uc;
  8. vector<UserCommand> ucv;
  9.  
  10. string input=" ";
  11. uc.commandParser(input);
  12. ucv.push_back(uc); //segmentation fault here
  13.  
  14. return 0;
  15. }

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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
alone2005 is offline Offline
23 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can you call delete across functions?
Next Thread in C++ Forum Timeline: Is this c++ book right for me?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC