PDA

Click to See Complete Forum and Search --> : simple class error


kovan
Oct 10th, 2000, 01:01 PM
i get error that sayis 'Person' undelcared identifier
and aslo that left of all my functions/routines
that i "must have class/struct/union type"

anyone could help, would greatly appreciated it


#include <iostream>
using namespace std;
int main(void)
{

Person Kovan;
Kovan.SetName("Kovan");
Kovan.SetAge(20);
Kovan.SayAge();
Kovan.SayName();
Kovan.Greeting1();
Kovan.GoodBye();


int retval;
cin >> retval;
return retval;
}

class Person
{
public:
void SetName(char* chrName);
void SetAge(int Age);
char* GetName();

void Greeting1();
void SayName();
void GoodBye();
void SayAge();

private:
char* pName;
unsigned int pAge;
};

void Person::SetAge (int Age){

pAge = Age;

}

void Person::SetName(char* chrName)
{
pName = chrName;
}

char* Person::GetName()
{

return pName;
}

void Person::Greeting1()
{
cout << "hi there how are you doing?" << endl;
}

void Person::SayName()
{
cout << pName << endl;
}

void Person::GoodBye()
{
cout << "See Ya Later" << endl;
}

void Person::SayAge()
{
cout << pAge << endl;
}

Vlatko
Oct 10th, 2000, 01:29 PM
Put the class and the functions before the main function.This works great.

#include <iostream>

using namespace std;


class Person
{
public:
void SetName(char* chrName);
void SetAge(int Age);
char* GetName();

void Greeting1();
void SayName();
void GoodBye();
void SayAge();

private:
char* pName;
unsigned int pAge;
};

void Person::SetAge (int Age){

pAge = Age;

}

void Person::SetName(char* chrName)
{
pName = chrName;
}

char* Person::GetName()
{

return pName;
}

void Person::Greeting1()
{
cout << "hi there how are you doing?" << endl;
}

void Person::SayName()
{
cout << pName << endl;
}

void Person::GoodBye()
{
cout << "See Ya Later" << endl;
}

void Person::SayAge()
{
cout << pAge << endl;
}


int main(int argc, char* argv[])
{
Person Kovan;
Kovan.SetName("Kovan");
Kovan.SetAge(20);
Kovan.SayAge();
Kovan.SayName();
Kovan.Greeting1();
Kovan.GoodBye();


int retval;
cin >> retval;
return retval;

}

kovan
Oct 10th, 2000, 01:39 PM
hehe

that really does make sense doesnt it?

what if i put all those functions and the class into a .H file..
if i use the #include..
will that automatically put them before main() or do i need to do somethign special?
becuase if i remember correctly i tried to put them in a file a while ago and i use to get same error..

thanks anyways for the help
i will try that out

HarryW
Oct 10th, 2000, 01:50 PM
Sticking the class definition in a header file is a pretty standard use for header files I think.

parksie
Oct 10th, 2000, 01:57 PM
Although don't put the code into the header, split it into another .cpp file, as in:

-- Person.h --

#ifndef __PERSON_H__
#define __PERSON_H__

#include <iostream>

using namespace std;

class Person {
public:
void SetName(char* chrName);
void SetAge(int Age);
char* GetName();

void Greeting1();
void SayName();
void GoodBye();
void SayAge();

private:
char* pName;
unsigned int pAge;
};

#endif // __PERSON_H__

...and...
-- Person.cpp --

#include "Person.h"

void Person::SetAge (int Age) {

pAge = Age;

}

void Person::SetName(char* chrName) {
pName = chrName;
}

char* Person::GetName() {

return pName;
}

void Person::Greeting1() {
cout << "hi there how are you doing?" << endl;
}

void Person::SayName() {
cout << pName << endl;
}

void Person::GoodBye() {
cout << "See Ya Later" << endl;
}

void Person::SayAge() {
cout << pAge << endl;
}

kovan
Oct 10th, 2000, 01:57 PM
my bad
i meant another .cpp