the & sysmbol and 'const' in c++ and segmentation errors
Yep, another 'interesting' problem form me :laugh4:
I'm adapting someone else's code to fit my needs, now this person seems fond of using constructors like
State_Circle(const vector<double>& _pos ) {};
which uses two things I'm not used to (for the record, i started c++ less than half a year ago, cut me some slack ) which apparently means that instead of an actual vector an adress is given to the program and the const keeps you from changing the adress within the code of the program.
Is this explanation about right ? Any reason why the '&' is used to declare a pointer instead of '*' ? I couldn't find this particular structure back in any of the tutorials/manuals that I use.
Apparently segmentation errors are due to referring to faulty memory adresses, so I guess it has something to do with this structure (I also added log messages throughout the code and none of the ones in the class that should be called show up, so I'm assuming not even the constructor gets called).
Re: the & sysmbol and 'const' in c++ and segmentation errors
Quote:
Originally Posted by doc_bean
Yep, another 'interesting' problem form me :laugh4:
I'm adapting someone else's code to fit my needs, now this person seems fond of using constructors like
State_Circle(const vector<double>& _pos ) {};
which uses two things I'm not used to (for the record, i started c++ less than half a year ago, cut me some slack ) which apparently means that instead of an actual vector an adress is given to the program and the const keeps you from changing the adress within the code of the program.
Is this explanation about right ? Any reason why the '&' is used to declare a pointer instead of '*' ? I couldn't find this particular structure back in any of the tutorials/manuals that I use.
Apparently segmentation errors are due to referring to faulty memory adresses, so I guess it has something to do with this structure (I also added log messages throughout the code and none of the ones in the class that should be called show up, so I'm assuming not even the constructor gets called).
The '&' is a reference. It is similar in functionality to a pointer, the difference is that references are easier to use, and less prone to errors than pointers (they do not need to be allocated, freed, etc).
Sending in a reference allows you to modify the object, like you said. The 'const' qualifier prevents you from doing so.
As for your errors: well, debugging 101. Compile the code with "-g" if you're using gcc, or build a debug version if you're using MS stuff; and try to locate the error; under linux you can use debuggers like ddd or gdb.
Debugging with print statements, while sometimes useful and sometimes unavoidable, is going to be too time-consuming and not very efficient.
ddd is a graphical debugger, very easy to use.
Re: the & sysmbol and 'const' in c++ and segmentation errors
W00t !
I was wrong apparently, the problem was due to a variable I had forgotten to 'add' in the right place, strange error to get for something like that I must say.
Re: the & sysmbol and 'const' in c++ and segmentation errors
Have you tried Codeblocks yet doc? Lately I've been using it, was looking for a better (free/open source) IDE alternative to dev-c++, which is kind of aged right now.
Re: the & sysmbol and 'const' in c++ and segmentation errors
Nope. I don't use much fancy stuff i'm afraid, VIM for writing code and ccmake for compilen (well gcc for compiling, ccmake for file management when compiling) and that's about it.
Re: the & sysmbol and 'const' in c++ and segmentation errors
Ugh, a glutton for pain I see. :grin: I still have painful memories of learning C++ on old Sparc stations using Emacs way back in the day.