I wrote this simple program.
I get an error saying that the member variable 'value' is not initialized, so how do I initialize it.
Secondly, I'd really appreciate any tips on improving the code. I really want to develop my C++ skills.
References have to be initialized in the constructor's initializer list.
1 2 3 4
Multiples::Multiples(int*& number) // the param would need to be the same type
: value(number) // value needs to be constructed in the initializer list
{
}
But you have bigger conceptual problems here.
- Why are you using pointers everywhere? 'value' has no reason to be a pointer. Neither does 'm'.
- You can't have a non-const reference to '5'. The whole point of having a reference is that it's an alias for another variable name. What you're trying to do is similar to this:
1 2 3 4
int& myref = 5; // nonsense
myref = 10; // this line is the same as saying "5 = 10;"
// it's trying to change the value of the literal 5. Not possible... 5 is 5, it cannot be 10
Really, I don't see the need for pointers or references anywhere in this code. 'value' should just be a normal int. 'm' should be an object, not a pointer. Everywhere you're using m-> should change to m., and everywhere you're using *value should change to value.