Results 1 to 6 of 6

Thread: simple class error

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    Unhappy

    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

    Code:
    #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;
    }

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Put the class and the functions before the main function.This works great.
    Code:
    #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;
    
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    *bangs his head on the desk few times*

    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

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Sticking the class definition in a header file is a pretty standard use for header files I think.
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Although don't put the code into the header, split it into another .cpp file, as in:

    -- Person.h --
    Code:
    #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 --
    Code:
    #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;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width