Hello, I have been working a week on this program with this stupid bug that gives tons of errors. I have narrowed them down to 4 errors and 2 warnings but I have no idea how to fix the 4 errors.

The program is supposed to have two user defined types with classes kilometer and furlong. kilometer constains int kilometers and double meters that are defined in the constructor or defined with a basic type double or defined with a user defined type of Furlong class which holds furlongs yards and feet. The same process is true for the Furlong class with feet as a double and yards and furlongs as ints. It contains 2 .h files - containing the 2 classes - and a .cpp. I get these errors:
1>c:\users\steffen holm\documents\visual studio 2008\projects\program unit 4\program unit 4\furlong.h(39) : error C2027: use of undefined type 'Kilometer'
1> c:\users\steffen holm\documents\visual studio 2008\projects\program unit 4\program unit 4\furlong.h(37) : see declaration of 'Kilometer'
1>c:\users\steffen holm\documents\visual studio 2008\projects\program unit 4\program unit 4\furlong.h(39) : error C2228: left of '.getkilometers' must have class/struct/union
1>c:\users\steffen holm\documents\visual studio 2008\projects\program unit 4\program unit 4\furlong.h(39) : error C2027: use of undefined type 'Kilometer'
1> c:\users\steffen holm\documents\visual studio 2008\projects\program unit 4\program unit 4\furlong.h(37) : see declaration of 'Kilometer'
1>c:\users\steffen holm\documents\visual studio 2008\projects\program unit 4\program unit 4\furlong.h(39) : error C2228: left of '.getmeters' must have class/struct/union
1>c:\users\steffen holm\documents\visual studio 2008\projects\program unit 4\program unit 4\kilometer.h(17) : warning C4005: 'METERS_PER_KILOMETER' : macro redefinition
1> c:\users\steffen holm\documents\visual studio 2008\projects\program unit 4\program unit 4\furlong.h(13) : see previous definition of 'METERS_PER_KILOMETER'

Here is the program:
furlong.h

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

#define MILES_PER_FURLONG	    .125 ;     //constants
#define YARDS_PER_FURLONG		220  ;     //constants   
#define FEET_PER_YARD		    3    ;    //constants
#define FEET_PER_FURLONG        660  ;
#define KILOMETERS_PER_FURLONG  0.201168 ; //constants
#define METERS_PER_KILOMETER	1000;
#define FURLONGS_PER_KILOMETER  4.97096954; 

class Furlong{
private:
	int furlongs;
	int yards;
	double feet;
public:

	Furlong ()
	{
		furlongs=0;
		yards=0;
		feet=0.0;
	};
	Furlong(double f)//Basic type to User Defined type (class furlong type)
	{
		furlongs = (int)floor(f);//f works like d_furlongs since this is a direct conversion not needing to be multiplied by KILOMETERS_PER_FURLONG
		double d_yards = (f - furlongs)*YARDS_PER_FURLONG;// d_yards(decimal yards)  is the difference between f and the floored f
		yards = (int)floor(d_yards);					  // miles the non-decimal value
		feet = (d_yards - yards) * FEET_PER_YARD;		  // can be a decimal value for feet made to take all of the remainding value
	};

	Furlong(class Kilometer& kilo)
	{
		double k = kilo.getkilometers() + kilo.getmeters()/METERS_PER_KILOMETER;//so I dont have to tediously call the long function names over and over wasting code and memory space, combines kilometers with meters/1000 to create a total value for the total kilometers like f for the other constructor
		double d_furlongs = k * KILOMETERS_PER_FURLONG;  //decimal furlongs (d_furlongs) is the double version the kilometers * KILOMETERS_PER_FURLONG
		furlongs = (int)floor(d_furlongs);//non-decimal version of the kilometers to furlong 
		double d_yards = (d_furlongs - furlongs) * YARDS_PER_FURLONG;// d_yards(decimal yards)  is the difference between d_furlongs and the floored furlongs
		yards = (int)floor(d_yards);//miles the non-decimal value
		feet = d_yards - yards * FEET_PER_YARD;		  // can be a decimal value for feet made to take all of the remainding value
	};

	int getfurlongs(){			//no explanation really needed I would hope
		return furlongs;
	}
	
	int getyards(){
		return yards;
	}
	
	double getfeet(){
		return feet;
	}

	void display()
	{cout<<"Furlongs: "<<furlongs<<endl<<"Yards: "<<yards<<endl<<"Feet: "<<feet<<endl;}//could call the get___() functions but would be unnecessary use of space
};

Kilometer.h

#include<iostream>
#include<math.h>
using namespace std;
#define METERS_PER_KILOMETER	1000      ;
#define FURLONGS_PER_KILOMETER  4.97096954; 
#define MILES_PER_FURLONG	    .125      ;  //constants
#define YARDS_PER_FURLONG		220       ; //constants   
#define FEET_PER_YARD		    3         ; //constants
#define FEET_PER_FURLONG        660       ;
#define KILOMETERS_PER_FURLONG  0.201168  ;//constants

class Kilometer{
private:
	int kilometers;
	double meters;
public:

	Kilometer ()
	{
		kilometers=0;
		meters=0.0;
	};
	Kilometer (double k)
	{
		kilometers = (int)floor(k);//floored version of k
		meters = (k-kilometers) * METERS_PER_KILOMETER;//the difference of the 
	};
	
	Kilometer (Furlong& furl)
	{
		double f=furl.getfurlongs() + furl.getyards()/YARDS_PER_FURLONG + furl.getfeet()/FEET_PER_FURLONG;//so I dont have to tediously call the long function names over and over wasting code and memory space, combines furlongs with yards/220 with feet/660 to create a total value for the total furlongs similar to the double k for the other constructor
		double d_kilometers = f*FURLONGS_PER_KILOMETER;  //decimal furlongs (d_furlongs) is the double version the kilometers * fURLONGS_PER_KILOMETER
		kilometers = (int)floor(d_kilometers);//non-decimal version of the kilometers to furlong 
		meters=(d_kilometers-kilometers) * METERS_PER_KILOMETER; 
	};

	int getkilometers(){			//no explanation needed
		return kilometers;
	}
	
	double getmeters(){
		return meters;
	}

	void display()
	{cout<<endl<<"Kilmeters: "<<kilometers<<endl<<"meters: "<<meters<<endl;};
};

main.cpp

#include "Furlong.h"
#include "Kilometer.h"
#include<iostream> //not really needed to be in so I commented out, the linking of the Furlong.h and Kilometer.h includes #include<iostream> 
using namespace std;
int main()
{

	double f=15;//furlong input initialized to zero for safety
	double k=34.3458;//kilometer input initialized to zero for safety
	class Furlong a(f);
	class Kilometer b(k);
	a.display();
	b.display(); 
	class Kilometer c(a);
	c.display();
	system("pause");
	return 0;
}

The errors all point to line 34 of the Furlong.h module and the function following. Thanks.

Recommended Answers

All 3 Replies

Look at line 34 furlong.h (class Kilometer& kilo) You are using Kilometer here. Right? Now just tell me, have you told your compiler before that something called Kilometer exists? No.
So, what to do?
Simple, you need to tell your class furlong that there exists something called Kilometer by declaring it once before the declaration of class furlong. This is called forward declaration.
You just need to put a

class Kilometer;

just before the declaration of furlong class(i.e. line 12 of furlong.h). Something like this:

class Kilometer;
class Furlong{
private:
int furlongs;class Kilometer;
..
..
..
..

For more info about forward declarations:http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.11

commented: AWESOME +1

Yes that worked but I also needed to put the private int Kilometer and double meter and the getmeter() and getkilometer() functions in there too. BUT there are more functions in the program that if I include them they would require the declaration of furlong class. Do you have any ideas? Thanks.

I don't think I am able to understand you.
You can always forward declare furlong class (as you did it to Kilometer), just before the declaration of Kilometer class.

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.