Class implementation? (C++)

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

abdication
Posts: 31
Joined: Fri Sep 24, 2010 10:52 am UTC

Class implementation? (C++)

Postby abdication » Tue Oct 19, 2010 4:53 am UTC

So, lets say I have a basic C++ Class. We'll call it Car. A car has X and Y coordinates, nothing else.

You would normally generate a car with:

Code: Select all

Car MiniVan1(15, 17)


However, lets say I wanted the user to supply a number, and this number would generate X Car's.


Code: Select all

While(count < X)
{
    Car X(15,17)
X++
}


And, in the perfect world, "X" would be replaced by the number, and it would produce Car 1, Car 2, Car 3, etc, it would be no different than:

Code: Select all

Car 1(15, 17)
Car 2(15, 17)
Car 3(15, 17)


However, this doesn't ever seem to work, it says that I am supply the wrong type for the Car class.

Any advice on how to get this to work?

Thanks!

User avatar
Skraxt
Posts: 43
Joined: Mon Nov 30, 2009 5:18 am UTC

Re: Class implementation? (C++)

Postby Skraxt » Tue Oct 19, 2010 5:02 am UTC

The answer to your troubles is an array, I am assuming you are not familiar with them. Start Here.
"Truth emerges more readily from error than from confusion" -Francis Bacon

Karrion
Posts: 92
Joined: Fri Jun 22, 2007 12:14 am UTC
Location: Melbourne, AU

Re: Class implementation? (C++)

Postby Karrion » Tue Oct 19, 2010 5:09 am UTC

Well, you can't use the value of a variable as a variable name itself, because the value won't be known at compile time. (Also, you can't name a variable "1", "2", etc because a variable can't start with a numeral). And in any case, creating a large number of separate variables seems like a bad idea.

It would be better to use a vector:

Code: Select all

#include <vector>

std::vector<Car> cars;
for (int count=0; count < NUM_CARS; ++count)
{
    cars.push_back(Car(15,17));
}


(I also changed your loop, as the way you had it seemed... wrong.)

For this to work, you may need to provide a copy constructor and assignment operator, though if there's just a couple of integer member variables, the default ones should be sufficient.

abdication
Posts: 31
Joined: Fri Sep 24, 2010 10:52 am UTC

Re: Class implementation? (C++)

Postby abdication » Tue Oct 19, 2010 9:56 am UTC

Karrion wrote:Well, you can't use the value of a variable as a variable name itself, because the value won't be known at compile time. (Also, you can't name a variable "1", "2", etc because a variable can't start with a numeral). And in any case, creating a large number of separate variables seems like a bad idea.

It would be better to use a vector:

Code: Select all

#include <vector>

std::vector<Car> cars;
for (int count=0; count < NUM_CARS; ++count)
{
    cars.push_back(Car(15,17));
}


(I also changed your loop, as the way you had it seemed... wrong.)

For this to work, you may need to provide a copy constructor and assignment operator, though if there's just a couple of integer member variables, the default ones should be sufficient.


It worked perfectly. To be honest, I never thought of this solution. For some reason I was stuck on the "Class" "Object_Name"(X,Y) kind of notation, then adding "Object_name" to a vector. I never knew you could just "do away" with the object name as you did, and have it work out. Of course, I realize that it's able to be referenced by cars[0].

Thanks a lot!


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests