Im trying to learn c++ and i got a book which is not that great in my opinion, due to errors.
one of my problems im having with one of the exercises is trying to set values for food, land.
every time i run the program i get a bunch of errors all stating that everything in the nation method cannot be
used as a function. how can i initialize or set them to the values so it works. thanks in advance. I am
including the class i have and the method where the errors keep appearing.

I also have what i think should work if anyone wants or needs to see more of the code i have written
The code you can download with the book is useless in my opinion because it gives more errors than when i write it
from scratch.

class Nation

    int land;
    int troops;
    Nation(string lName);

    bool takeTurn();


    void menu();
    string name;
    int food, gold, people, farmers, merchants, blacksmiths;


 Nation nation1;
 Nation nation2;

Nation::Nation(string lName)
    name(lName), land(20), food(50), troops(15), gold(100),
    people(100), farmers(0), merchants(0), blacksmiths(0);

7 Years
Discussion Span
Last Post by Duoas

I think in the Nation function, you are trying to give values to some of your variables. If this is indeed the case, you are assigning values in the wrong manner.

You need to use variableName = value; to accomplish this

name = lname;
land = 20;
// etc .........

Hope that helps a bit.


Your constructor should look like this:

Nation::Nation(string lName):
    name(lName), land(20), food(50), troops(15), gold(100),
    people(100), farmers(0), merchants(0), blacksmiths(0)
    // Body left empty

The list of things are not functions (which is why the compiler is complaining, because you are using them like functions) but member fields.

The list is called an initialization list.

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.