Class problem

J

John J

I've written the following function in a class called "Race.cpp". My hope is
that when called it will access the getName function in a class called
"Yacht" to display the name of a yacht object(s). The function is called in
a main using "r1.display_entries ();" where r1 is race 1, where it should
display all the names of yachts that have entered race 1; however, when I
compile and run it outputs the value "1". I'd appreciate some advice on what
I'm doing wrong. The classes are correctly declared to each other as evident
by other functions. I'm guessing it's something in the syntax or the way
I've called the other function.

Thanks for any help.

void Race::display_entries (ostream& out) const

{

out << "Yacht Name : " << Yacht::getName << endl;

}
 
J

John Harrison

John J said:
I've written the following function in a class called "Race.cpp". My hope is
that when called it will access the getName function in a class called
"Yacht" to display the name of a yacht object(s). The function is called in
a main using "r1.display_entries ();" where r1 is race 1, where it should
display all the names of yachts that have entered race 1; however, when I
compile and run it outputs the value "1". I'd appreciate some advice on what
I'm doing wrong. The classes are correctly declared to each other as evident
by other functions. I'm guessing it's something in the syntax or the way
I've called the other function.

Your guess is wrong. The problem is that you are struggling with the
conceptual difference between a class and an object.

What you are trying to do is print the list of names of yacht that have
entered a race. That means that the race class has to be declared so that it
has a data member which is a list of yachts (or a vector of yachts or an
array or something like that). Then when you want to print the list of names
you can loop through the list of yachts and print out each name.

Let me be blunt, you are NEVER going to solve this problem unless you get
the class design and the relationships between your classes correct. It is
not a trivial issue of syntax, its a fundamental mistake or miunderstanding
that you have made when you wrote your classes.

I suggest that the best thing to do would be to post the class definitions
for Race and Yacht and someone can fix them.

john
 
J

John J

Thanks John,

I solved the problem with the following code:

void Race::display_entries (ostream& out) const

{

if (nEntries == 0)

out << "No entries" << endl;

else

{

cout << "Details of Yachts entered into the requested race:" <<endl << endl;

for (int i=0; i<nEntries; i++)

out << *(entries);

}

}
 
J

John Carson

John J said:
I've written the following function in a class called "Race.cpp". My
hope is that when called it will access the getName function in a
class called "Yacht" to display the name of a yacht object(s). The
function is called in a main using "r1.display_entries ();" where r1
is race 1, where it should display all the names of yachts that have
entered race 1; however, when I compile and run it outputs the value
"1". I'd appreciate some advice on what I'm doing wrong. The classes
are correctly declared to each other as evident by other functions.
I'm guessing it's something in the syntax or the way I've called the
other function.

Thanks for any help.

void Race::display_entries (ostream& out) const

{

out << "Yacht Name : " << Yacht::getName << endl;

}

One problem is that you have omitted the brackets from

Yacht::getName

i.e., it should be

Yacht::getName()

(assuming the function takes no arguments). I find your description of your
classes impossible to follow, so there may be other problems.
 
J

John Harrison

John J said:
Thanks John,

I solved the problem with the following code:

void Race::display_entries (ostream& out) const

{

if (nEntries == 0)

out << "No entries" << endl;

else

{

cout << "Details of Yachts entered into the requested race:" <<endl << endl;

for (int i=0; i<nEntries; i++)

out << *(entries);

}

}


That looks an awful lot better.

john
 
J

JKop

class Yacht
{

//Blah Blah Blah

};



class Race
{
public:

Yacht* Competitors[20]; //20 Competitors

bool AddYacht(Yacht* NewCompetitor);

void Begin(void);

private:

unsigned short int NumberOfYachtsAlreadyEntered;
};



int main(void)
{

Race TheBiggestRaceOfAllTime;


Yacht MySuperCoolSupersonicYacht;

TheBiggestRaceOfAllTime.AddYacht(&MySuperCoolSupersonicYacht);

Yacht CrappyYacht;

TheBiggestRaceOfAllTime.AddYacht(&CrappyYacht);


TheBiggestRaceOfAllTime.Begin();
}



Do you understand what's going on?


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Associated classes 3
Unexpected Result 8
Search function 3
Class search function 1
Calling a function in another class 2
Read class variables in different files 1
Java matrix problem 3
Class receipt c++ 5

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top