class type undeclared identifier

I'm working on a small program.
I made header file and the .cpp file seem to recognize the header file.

//Header File (class.h)
class Lunar
{
.
.
.
};

//CPP File
#include "class.h"
#include <iostream>
#include <iomanip>
using namespace std;

char menu();
void status();

int main()
{
Lunar l();
char difficulty;
float ffrate;
float t = 0;

difficulty = menu();

system("clr");

l.setdifficulty(difficulty);
.
.
.
}
This is what I have so far.
For some reason, I get this error saying:
"left of '.setdifficulty' must have class/struct/union type"
I declared Lunar l but compiler doesn't seem to know it. What's the problem?
Last edited on
Is that the only compiler error? It doesn't say anything about the line l is declared in?
Can you post the contents of class.h for us to look at?
///////////////////////////////////////////////////////////////////////
//Header File
class Lunar
{
public:
//CONSTRUCTOR
Lunar();
//MUTATORS
void setdifficulty (char);
void changeffrate (float);
void changefuel (float);
void changetime (int, float);
void changevelo (float);
void changealt (float);
//ACCESSORS
float getfuel () const;
float gettime (float) const;
float getvelo () const;
float getalt () const;
float getmass () const;
float getthrust () const;

private:
//ATTRIBUTES
int ffr; //fuel flow rate
float vs; //vertical speed
float alt; //altitiude
int fuel; //amount of fuel
int mass; //mass of the lander itself
int fcr; //fuel consumption rate
int thrust; //maximum thrust of the engine
};

///////////////////////////////////////////////////////////////////////
//Implementation File
#include <iostream>
#include "class.h"
using namespace std;

Lunar::Lunar()
{
ffr = 0;
vs = 0;
alt = 1000;
fuel = 1700;
mass = 900;
fcr = 10;
thrust = 5000;
}

void Lunar::setdifficulty(char difficulty)
{
switch (difficulty)
{
case 'E': case 'e':
fuel = 2000;
break;

case 'M': case 'm':
fuel = 1700;
break;

case 'H': case 'h':
fuel = 1400;
break;
}
}

void Lunar::changeffrate (float ffrate)
{
ffr = ffrate;
}

void Lunar::changefuel(float time)
{
fuel = fuel - (fcr * time);
}

void Lunar::changetime(int i, float time)
{
time = .001 * i;
}

void Lunar::changevelo (float time)
{
vs += time * ((ffr * thrust) / mass - 1.62);
}

void Lunar::changealt (float time)
{
alt -= vs * time;
}

float Lunar::getfuel () const
{
return fuel;
}

float Lunar::gettime (float time) const
{
return time;
}

float Lunar::getvelo () const
{
return vs;
}

float Lunar::getalt () const
{
return alt;
}

float Lunar::getmass () const
{
return mass;
}

float Lunar::getthrust () const
{
return thrust;
}

///////////////////////////////////////////////////////////////////////
//Main File
#include "class.h"
#include <iostream>
#include <iomanip>
using namespace std;

char menu();
void status();

int main()
{
Lunar l();
char difficulty;
float ffrate;
float t = 0;

difficulty = menu();

system("clr");

l.setdifficulty(difficulty);

while ((l.getfuel() != 0) && (l.getalt() != 0))
{
status();

cout << "Enter fuel flow rate: ";
cin >> ffrate;

for (int i = 1; i <= 2000; ++i)
{
l.changetime (i, t);
t = l.gettime(t);
l.changevelo (t);
l.changealt (t);
}
}

if ((l.getalt () == 0) && (l.getvelo () >= (-3) && l.getvelo () <= 0))
{
cout << "\n" << "Great landing. You are in one piece" << endl;
system ("pause");
}

if ((l.getalt () == 0) && (l.getvelo () <= (-3)))
{
cout << "\n" << "You are torn apart." << endl;
system ("pause");
}

if ((l.getalt () > 0) && (l.getvelo () >= 0) && (l.getfuel() == 0))
{
cout << "\n" << "You are orbit around the moon rest of your life" << endl;
system ("pause");
}

return 0;
}

char menu ()
{
char difficulty;
cout << "LUNAR LANDER" << endl;

system("pause");

cout << "\n" << "Enter one of the following options: " << endl;
cout << "\n" << "Easy" << setw(10) << "-" << setw(10) << "E" << endl;
cout << "\n" << "Medium" << setw(10) << "-" << setw(10) << "M" << endl;
cout << "\n" << "Hard" << setw(10) << "-" << setw(10) << "H" << endl;
cin >> difficulty;

return difficulty;
}

void status()
{
cout << "Your current status: " << endl;
cout << "\n" << setw(5) << "Altitude:" << setw(5) << l.getalt() << endl;
cout << setw(5) << "Velocity:" << setw(5) << l.getvelo() << endl;
cout << setw(5) << "Fuel:" << setw(5) << l.getfuel() << endl;
cout << setw(5) << "Mass:" << setw(5) << l.getmass() << endl;
cout << setw(5) << "Thrust:" << setw(5) << l.getthrust() << endl;
}



These are my project.
I haven't done any error handling so there should be many minor errors.
The major problem is that I do get these errors:

error C2228: left of '.setdifficulty' must have class/struct/union type
error C2228: left of '.getfuel' must have class/struct/union type
error C2228: left of '.getalt' must have class/struct/union type
fatal error C1903: unable to recover from previous error(s); stopping compilation

I declared "Lunar l" at the beginning of function main.

Oh, and this is the first time I created a class.
It should look disgusting to many programmers...:)
Last edited on
Hmm...from what I see, it should be working fine :/
Yes, it should but the compiler keeps on complaining.

By the way, I use VC++ 6.0
Not easy to spot but this is the problem line (the first line in the main function):
Lunar l(); //Error in declaring a variable of class Lunar using default constructor
As written, that is actually a prototype for a function that takes no parameters and returns a variable of type Lunar.


Last edited on
Topic archived. No new replies allowed.