Im trying to figure out how to utilise a program that uses Aggregation of Classes, Im having trouble calling the operations of the Classes in my main. Could anyone assist where Im going wrong?!

horse.h

#include <iostream>
#include "distance.h"
using namespace std;

class Distance;

class Horse
{
private :
	     Distance wonby;
public :
	     Horse(Distance);
	     void setWinningDistance(Distance);
                   Distance getWinningDistance();

};

distance.h

#include <iostream>
using namespace std;

class Distance
{
private :
	     int yards;

public :
	     Distance(int = 0);
	     void setDistance(int);
	     int getyards();
};

distance.cpp

#include ".\distance.h"
#include <iostream>
using namespace std;

Distance::Distance(int y)
{
	yards = y;
}

void Distance::setDistance(int newyards)
{
	yards = newyards;
}

int Distance::getyards()
{
	return yards;
}

horse.cpp

#include ".\horse.h"
#include <iostream>
using namespace std;

Horse::Horse(Distance w)
{
	wonby = w;
}

void Horse:: setWinningDistance(Distance w)
{
	wonby = w;
}

Distance Horse::getWinningDistance()
{
	return wonby;
}

main.cpp (This is where Im having trouble, Im not calling the operations correctly or maybe assigning Horse h1 wrongly?! I know the first line is correct as that compiles at least)

#include "horse.h"
#include <stdlib.h>

int main(int argc, char* argv[])
{
	int p,d;
	Horse h1(Distance(p));
	
	cout << "Please enter the value "<< endl;
	cin  >> p;

	h1.setWinningDistance(Distance(p));

	d = h1.getWinningDistance();
	cout << d << endl;
	

	
	
	system("Pause");
}

Thanks for any input, is my structure for an aggregation class correct also?

> Horse h1(Distance(p));
This is an evil bug. C++ isn't seeing that line as an object definition, it's seeing the line as a function declaration. h1 is a function that returns a Horse object and takes a Distance parameter called p. It looks like a parameter because C++ treats the parentheses around p as redundant and sees Distance p , which looks like a variable declaration. That's not what you wanted, I'm sure. ;) There are three easy ways to fix it:

Horse h1((Distance(p)));

Adding parentheses around the argument forces C++ to parse the whole line the right way.

Horse h1 = Distance(p);

Horse's constructor isn't explicit, so you can use an unambiguous assignment syntax.

Distance arg(p);
Horse h1(arg);

If the argument doesn't look like a variable declaration, the ambiguity is removed.

commented: Thanks pal! +1

Cheers Radical Edward, that helps a lot mate!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.