C++ Undeclared Identifier (Forward Declaration)

I'm kinda new at coding but I would like to ask why this doesn't work. I prefer putting all the definitions below so I tried coding this but for some reason it didn't work.

#include <iostream>
#include <string>

void ParentClass::Word();
void ParentClass::Word2();

int main() {
    ParentClass Parent;
    Parent.Word();
    Parent.Word2();
}

class ParentClass {
public:
    void Word() {
        std::cout << "Parent \n";
    }

    void Word2() {
        std::cout << "Parent2 \n";
    }
};

I expected it to work the same as when I put the class before the main function but it unfortunately didnt. It just have a red underline on:

void ParentClass::Word();
void ParentClass::Word2();

Upvotes: -3

Views: 124

Answers (2)

Chris
Chris

Reputation: 36536

What you're looking for when forward declaring with a class is to forward declare the class and its member functions, and then define the member functions later.

#include <iostream>
#include <string>

class ParentClass {
public:
    void Word();
    void Word2();
};

int main() {
    ParentClass Parent;
    Parent.Word();
    Parent.Word2();
}

void ParentClass::Word() {
    std::cout << "Parent \n";
}

void ParentClass::Word2() {
    std::cout << "Parent2 \n";
}

Upvotes: 1

Thisal
Thisal

Reputation: 310

#include <iostream>
#include <string>    
    
class ParentClass {
    public:
        // Declaration of the member function 'Word' of ParentClass
        void Word();
        // Declaration of the member function 'Word2' of ParentClass
        void Word2();
};

// The main function where the execution of the program begins
int main() {
    // Create an instance of ParentClass named 'Parent'
    ParentClass Parent;
    // Call the 'Word' method on the 'Parent' object
    Parent.Word();
    // Call the 'Word2' method on the 'Parent' object
    Parent.Word2();

    return 0;
}

// Definition of the 'Word' method for ParentClass
void ParentClass::Word() {
    std::cout << "Parent \n";
}

// Definition of the 'Word2' method for ParentClass
void ParentClass::Word2() {
    std::cout << "Parent2 \n";
}

In practice, it is common to separate the declaration and definition of classes and their member functions into multiple files for better organization and maintainability.

ParentClass.h file

#ifndef PARENTCLASS_H
#define PARENTCLASS_H


class ParentClass {
    public:
        // Declaration of the member function 'Word' of ParentClass
        void Word();
        // Declaration of the member function 'Word2' of ParentClass
        void Word2();
};

#endif // PARENTCLASS_H

ParentClass.cpp file

#include "ParentClass.h" // Include the header that contain declaration
#include <iostream>

// Definition of the 'Word' method for ParentClass
void ParentClass::Word() {
    std::cout << "Parent \n";
}

// Definition of the 'Word2' method for ParentClass
void ParentClass::Word2() {
    std::cout << "Parent2 \n";
}

main.cpp file

#include <iostream>
#include "ParentClass.h"

// The main function where the execution of the program begins
int main() {
    // Create an instance of ParentClass named 'Parent'
    ParentClass Parent;
    // Call the 'Word' method on the 'Parent' object
    Parent.Word();
    // Call the 'Word2' method on the 'Parent' object
    Parent.Word2();

    return 0;
}

Upvotes: 2

Related Questions