Creating a class - code won't compile

Solved
Last edited on
Please copy and paste the exact error message and indicate which line it is referring to.

I don't see anything that immediately jumps out to me, but it is hard to read your code without indentation.

Do you know about constructor initializer lists?
http://en.cppreference.com/w/cpp/language/initializer_list
Solved
Last edited on
Please read the link I provided, it is the only way to write the code in your case.
Solved
Last edited on
59
60
61
62
63
Student::Student(const std::string& name, StudentID id)
: name_(name)
, id_(id)
{
}
Solved
Last edited on
Hi,

What does putting the semi colons in front do? I know it's meant to intialize it but I can't really understand why it's needed compared to other codes?


The full colon tells the compiler that the following expressions form a constructor initialiser list. It initialises the class variables before the construction of the object is complete, which is better than assigning to them again afterwards. If any of these expressions throw an exception (one could be calling a base class constructor, not just simple stuff like this example), then construction of the object is not complete, and that means the object was never actually created. The definition of a complete object is when the constructor code (between the braces) successfully completes.

One should use the constructor code to validate the arguments and ensure all the class invariants are satisfied. Class invariants are a set conditions for member variables, that must be maintained throughout the lifetime of the object. If they aren't, then throw an exception so the object isn't created. Some don't like exceptions, so they might do something else, like set a global exception variable.

Also is the name_(name) just because my name was name_? If I had chosen newName inside my private: part of class, would it be newName(name)?


Yes.

The brackets are just a cast or something class specific?


It just another way of initialising a variable. One can also do brace to initialisation.

After I do that part of the code, what exactly have I done? If someone enters in Student(name, id) will it automatically convert it to my private class name_ and id_ or do I have to write code for that as well?


Yes, it will initialise those 2 class member variables, that is the purpose of the initialiser list.

However, one should take care to initialise all the class member data, and the initialiser list should appear in the same order as what they are listed in the class. Make sure to call the constructors for all objects that you class uses, and the base classes. If you don't initialise everything, then you are asking for trouble.

Provide an interface of public functions allow you to deal with your class data. But only provide what you need.

Don't be tempted to have public get / set functions for all of them though, you don't need it. I am not against trivial get functions, but am against having both get and set functions. Set functions are only necessary when an object needs to change after it has been created, and often one can come up with 1 function that alters several members. Think of a Shape class, functions like move, rotate, scale etc operate on all the class members at once. In the case of a Triangle class, we don't have MovePt1, MovePt2, MovePt3 for example.

A final thing, very often arguments to functions should all be const, unless they are passed by reference in order to change their value in an outer scope.

Hope this all helps you out :+)


Topic archived. No new replies allowed.