| | |
save self-defined object in vector problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I define a class which hold some variables first:
Then I declare an object of Command and wanna store it in a vector
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.
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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.
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
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?
if I comment this function out, everything in peace
C++ Syntax (Toggle Plain Text)
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?
•
•
•
•
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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); }
C++ Syntax (Toggle Plain Text)
#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?
![]() |
Similar Threads
- Search problem (Java)
- problem save UTF-8 into DB (Oracle)
- Problems using a php generator (PHP)
- Object-Oriented Programming (C++)
- Saving and opeing a JTable (Java)
Other Threads in the C++ Forum
- Previous Thread: Can you call delete across functions?
- Next Thread: bank account
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






