| Kainis (3) | |||
Ok so I am supposed to make a class that represents car information however it worked up until I put all my Get and Set functions into the header file now I have 117 errors so if someone could point me in the right direction or tell me what exactly I am doing wrong that would be awesome
| |||
|
Last edited on
|
|||
| Disch (7385) | |
|
Put your code in code tags: http://cplusplus.com/articles/firedraco1/ Then try actually reading the compiler errors. You should be able to figure out all of these for yourself. Don't just give up once the compiler spits an error at you. EDIT: Also.. *cringes at the sight of such horrid get/set abuse* | |
|
Last edited on
|
|
| Kainis (3) | |
|
Well thanks for the help but I mean it just says all my Identifiers don't exist which I don't understand. As well says my Identifiers are undeclared which again I font understand how to fix. Then its says I need ; where they shouldn't be well I dont think they should be. And what am I doing that makes my Get/Set abusive? | |
|
Last edited on
|
|
| Disch (7385) | ||||
The code tags are nice... but why on earth did you comment everything? that takes away the syntax highlighting >_>
Look at the lines giving you the error. Line 31 above is one of them It's saying "m_Color" isn't declared, right? Think about it... where are you declaring m_Color? (hint: you're not. Look more closely at how you named your vars in your class: m_ExteriorColor). You did this with like 4 or 5 of your variables.
This error isn't 100% believable. It could be one of any number of problems. When you get this error (or really, any error), look over the erroring line(s) of code and look for anything that's wrong. Don't just look for a missing semicolon. Take a close look at some of the lines that are giving you this error. Specifically lines 31, 32, 33, etc. See any missing semicolons there? I sure do. Line 164 probably gives you a similar error. There, though, it's not a missing semicolon that's a problem, but a missing comma. ---------- some other things: 1) You shouldn't have semicolons before the opening brace for function definitions. See lines 114, 123, etc 2) Make sure your class definitions (in the .cpp file) match the prototype (in the .h file). Notice that all of your Set() functions should return void according to the .h, but you have them returning string in the .cpp 3) Similar problem with your Get functions. They're all returning strings, when some should be returning bool/int/etc 4) For default parameters.... if you have them in the function prototype, don't put them in the function definition. IE: your construtor in the .cpp file should not have the default parameters listed.
This isn't "wrong" per se. It's just a style thing. What's the point of writing a dozen functions to set/get individual member variables? Why not just make the member variables public and access them directly? Saves you tons of time and makes your code way smaller. There's nothing wrong with public data members in some cases. IMO this is one of those cases. | ||||
|
Last edited on
|
||||
| guestgulkan (2560) | |
|
An upside(good) thing about get/set and particular set functions is that you can do input validation - rather than have public variables where the values could be set to any old possible incorrect value directly. | |
|
|
|
| Disch (7385) | |
|
Oh, there's definately upsides, don't get me wrong. I'm not saying you should always make such members public. I'm just saying you shouldn't always make them private. One extreme is just as bad as the other. | |
|
|
|
| gcampton (859) | |
|
Gets and sets only need to be used for data validation and data hiding. Typically if only a few of your variables will be accessed outside the class then it's fine to have them as public but for the most part sets should be private anyway and only the class should be able to change them. So think if you need them ALL to be public or not. Most scenarios constructors can be used to set them as private setters. if you need to change the data via the setter outside the class then public is ok, but it makes the accessor/mutator kinda pointless.(edit: unless of course your doing validation as guest* suggested) | |
|
Last edited on
|
|