1. Initialize sum to 0 in Line 4
2. What is aBag in Line 3?
3. Why is the return statement in line 8 outside the curly braces of the function?
The instruction is "Write a client function that computes the sum of the integers in the bag "aBag".
I am assuming that std::vector<ing> aBag is being passed as a parameter to your sumNums() function, which means, &bagVector should have the address of aBag (wherever it may be passed from). Or maybe, somewhere else, your instructor may have defined a class or struct named bag? If that is the case, bag should be passed as a parameter, not std::vector.
Anyways, line 3 will cause an error for 2 reasons.
1. In the local function, aBag was never defined. Also, toVector() was never defined.
2. You've marked the parameter as const in the function header, but you are trying to assign another "value" to it and therefore change the const parameter.
[EDIT]
Let's assume that your instructor did define a class Bag. Then this is how you would create the function
1 2 3 4 5 6 7 8 9 10 11
|
int sumNums( const Bag &aBag )
{
// Assuming that toVector() is a const member function of Bag
std::vector<int> bagVector = aBag.toVector()
int sum = 0;
for( auto x : bagVector )
{ sum += x; }
return sum;
}
|