I am working on an array of classes so I can store some information that will be used later into my program. This is what I have for my array.
Car carArray[] = {Car ("Porsche", "911", "Silver", 2005, 50000),
Car ("Ford", "Mustang", "Red", 2007, 25000),
Car ("Voltzwagon", "Jetta", "Black", 2006, 16500)};
-- This is how I am calling it to check the information
cout %26lt;%26lt; "Testing this array: " %26lt;%26lt; carArray[0].getMake()
.getModel()
.getColor().getYear()
.getordervalue();
(had to split it up to make it show, or it would cut part of this line up.)
If I run it with just one of the .get, it will work. But once I add more than one, I get errors such as this.
'getModel' : is not a member of 'basic_string%26lt;char,struct std::char_traits%26lt;char%26gt;,class std::allocator%26lt;char%26gt; %26gt;'
left of '.getColor' must have class/struct/union type
left of '.getYear' must have class/struct/union type
left of '.getordervalue' must have class/struct/union type
How to fix a program with an array of classes [C++]?
Each of those method calls requires the carArray[ ] object instance. As shown, those after getMake do not have one.
cout %26lt;%26lt; "Testing this array: " %26lt;%26lt; carArray[0].getMake(),
carArray[0].getModel(),
carArray[0].getColor(), carArray[0].getYear(),
carArray[0].getordervalue();
Hope that helps.
Reply:you would need to refer to carArray[0].function for all method calls. not only the first one.
Reply:I'm not sure if the problem is as trivial as I imagined it, but did you try repeating the "carArray[0]" ?
like this:
cout %26lt;%26lt; "Make: " %26lt;%26lt; carArray[0].getMake();
cout %26lt;%26lt; "Model: " %26lt;%26lt; carArray[0].getModel();
....
if you want to combine them in one statement, do the following
cout %26lt;%26lt; "Make:" %26lt;%26lt; carArray[0].getMake() %26lt;%26lt; " Model: " %26lt;%26lt; carArray[0].getModel() %26lt;%26lt; " Color:" %26lt;%26lt; carArray[0].getColor();
This will NOT work:
"carArray[0].getMake().getModel().getC...
you have to specify on which object to call the function, for every function.
Good luck!
edible flowers
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment