How to fix a program with an array of classes [C++]? - Mustang Evolution

Go Back   Mustang Evolution > Ford Mustang | Wrenching, Care and General Topics > General Mustang Discussion



Reply
 
Thread Tools Search this Thread
Please support our sponsors and let them know you heard about them here!
Old 03-12-2010, 08:45 PM   #1
Registered Member
Regular
 
Join Date: Mar 2010
Posts: 32
How to fix a program with an array of classes [C++]?

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 << "Testing this array: " << 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.

&#039;getModel&#039; : is not a member of &#039;basic_string<char,struct std::char_traits<char>,class std::allocator<char> >&#039;

left of &#039;.getColor&#039; must have class/struct/union type
left of &#039;.getYear&#039; must have class/struct/union type
left of &#039;.getordervalue&#039; must have class/struct/union type
Kilian is offline   Reply With Quote
Sponsored Links
Advertisement
 
Old 03-12-2010, 09:34 PM   #2
Registered Member
Regular
 
Join Date: Aug 2009
Posts: 38
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 << "Testing this array: " << carArray[0].getMake(),
carArray[0].getModel(),
carArray[0].getColor(), carArray[0].getYear(),
carArray[0].getordervalue();

Hope that helps.
jantis8 is offline   Reply With Quote
Old 03-12-2010, 10:28 PM   #3
Registered Member
Regular
 
Join Date: Aug 2009
Posts: 44
How to fix a program with an array of classes [C++]?

you would need to refer to carArray[0].function for all method calls. not only the first one.
ann15 is offline   Reply With Quote
Sponsored Links
Advertisement
 
Old 03-12-2010, 10:58 PM   #4
Registered Member
Regular
 
Join Date: Aug 2009
Posts: 28
How to fix a program with an array of classes [C++]?

I&#039;m not sure if the problem is as trivial as I imagined it, but did you try repeating the "carArray[0]" ?
like this:

cout << "Make: " << carArray[0].getMake();
cout << "Model: " << carArray[0].getModel();
....

if you want to combine them in one statement, do the following

cout << "Make:" << carArray[0].getMake() << " Model: " << carArray[0].getModel() << " Color:" << 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!
cipriano is offline   Reply With Quote
Reply

Lower Navigation
Go Back   Mustang Evolution > Ford Mustang | Wrenching, Care and General Topics > General Mustang Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


» Like Us On Facebook



08:59 PM


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Content Relevant URLs by vBSEO 3.6.0

MustangEvolution.com is in no way associated with or endorsed by Ford Motor Company.