简体   繁体   中英

how can i declare a Class in C++

I want do declare the class Person before the main method but it gives me an error that the class Person is undefined

The main method is making the Object Person but I don't know how I can declare it like I do with methods.

//main Function
int main()
{
    Person person = Person("Majd", 18, 177); //Error "Person class is undefined"
    person.printPerson();
}

class Person
{
    //Private Attributes
    int age;
    int height;
    string name;
    //Public Atributes
public:

    Person(string name, int age, int height) {
        setName(name);
        setAge(age);
        setHeight(height);
    }
    //Getters
    string getName() {
        return name;
    }
    int getAge() {
        return age;
    }
    int getHeight() {
        return height;
    }
    //Setters
    void setName(string name) {
        this->name = name;
    }
    void setAge(int age) {
        this->age = age;
    }
    void setHeight(int height) {
        this->height = height;
    }

    void printPerson() {
        cout << "Name: " << getName() << " Age: " << getAge() << " height: " << getHeight();
    }
};

I am doing it like that so I can learn how to declare classes.

If you delcare the class above main:

class Person;

then you will be able to do

Person * person = 0; or Person * person = some_other_person;

but without being told that there are constructors to link against it will not be possible to actually create a person here, or use one.

You could just move the entire class definition to the top of the file, above main. That will fix it for now.

Good programming style would be to make a file called Person.h, put your class in that, and then #include "Person.h" in your main cpp file.

The class must always be defined before the main method, or the method where it is being used to instantiate objects.
Reason : C++ compiles from top to bottom (I am not talking about left to right or right to left, that depends on the associativity of used operators in your code lines), and to let the compiler store all things that you want to be existed in your class, you have to define it before using it, but, wait, this condition doesn't apply on the usage of functions or methods of a class, ie only the declaration could exist before its usage, but definition must also exist(maybe after the code block where we are using our function).
As @Andy Newman talked about header files, I wanted to mention an important fact.
An important fact about header files:

If you include your header files after the block where you are using the contents of that header file, then you will get error of non-declaration of things. The reason being, when the pre-processor reads a line where #include "header_file.h" is written, then it will without any delay copy and paste whole of the code of the that header file ( header_file.h ) in your file, and if you are using contents of the header file before the block where they are copy pasted, you will eventually get non-declaration error(for classes it needs definition).

Short Summary:

  1. Always define your classes before instantiating any object from that class.
  2. Include header files, before the block where you are using the contents of that header file(frank advice always include at beginning of your file).
  3. Functions used in some block must be declared before the block, and definition must also exist somewhere in your code.

You should define your class before "main" function.

class Person 
{
    //Private Attributes
    int age;
    int height;
    string name;
    //Public Atributes
    public:

        Person(string name, int age, int height) {
            setName(name);
            setAge(age);
            setHeight(height);
        }
        //Getters
        string getName() {
            return name;
        }
        int getAge() {
            return age;
        }
        int getHeight() {
            return height;
        }
        //Setters
        void setName(string name) {
            this->name = name;
        }
        void setAge(int age) {
            this->age = age;
        }
        void setHeight(int height) {
            this->height = height;
        }

        void printPerson() {
            cout << "Name: " << getName() << " Age: " << getAge() << " height: " << getHeight();
        }
};


//main Function
int main()
{
    Person person = Person("Majd", 18, 177); //Error "Person class is undefined"
    person.printPerson();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM