<insert name of struct> was not declared in this scope

11,221

Solution 1

There are two solutions: Make Employee a non-local class/struct or make PrintInformation a template. For the first solution, just move Employee before PrintInformation. The second solution would be:

template< typename Employee >
void PrintInformation(const Employee& EmployeeName)
{
    cout << " EmployeeName's ID is: " << EmployeeName.ID << endl;
    cout << " EmployeeName's age is: " << EmployeeName.age << endl;
    cout << " EmployeeName's wage is: " << EmployeeName.wage << endl;
}

Note that in any case you don't want a copy of Employee just to print some information, hence make the parameter of PrintInformation a constant reference as shown above.

Solution 2

First piece of information in the error is the line number.

C:\CBProjects\Practise\main.cpp|11|error: variable or field 'PrintInformation' declared void|

Line 11. Lets look at line 11.

void PrintInformation(Employee EmployeeName)

This all looks valid, but what's an Employee? We don't find that out until line 21. Your function, PrintInformation wants to make use of the internal plumbing of PrintInformation so the function actually needs to know the full definition of the struct/class.

In addition to all this, you have very explicitly made Employee a private type of main by defining it inside the function. What you are actually declaring is main::Employee.

A few solutions:

Declare "Employee" in the same scope as PrintInformation, i.e. at the global scope at the top of the file after the includes etc.

Or make "PrintInformation" a member function of Employee.

struct Employee
{
    void PrintInformation()
    {
        std::cout << " Employee's ID is: " << ID << '\n';
        std::cout << " Employee's age is: " << age << '\n';
        std::cout << " Employee's wage is: " << wage << '\n';
    }
    ...
};

...

Dominic.PrintInformation();

Or implement operator<< (probably advanced for where you are right now).

I'd also like to point out an aspect of style that is lining you up for some serious headaches down stream: You're using the same UpperCamelCase for variables and types, but being inconsistent with types. It's going to benefit you if you teach yourself to prefix member variables with something and maintain consistent case early on:

struct Employee  // UpperCamel for types.
{
    int m_id;
    int m_age;
    float m_wage;
};

It now becomes very easy to separate local variables and types and member variables. This is going to become especially useful as you start to learn about member functions.

struct Employee  // UpperCamel for types.
{
    int m_id;
    std::string m_name;
    int m_age;
    float m_wage;

    Employee(const std::string& name, int age, float wage)
        : m_id(nextID++)
        , m_name(name)
        , m_age(age)
        , m_wage(wage)
    {}

    ...

};
Share:
11,221
Miles Bardan
Author by

Miles Bardan

Autodidact; teaching myself to code so I can work as a freelance web-developer. Learning Python with Django; html/css and JS with JQuery. So far have only used Sqlite database but expect to move on to PostgreSQL. Using Notepad++ but guess I will move to Emacs or something.

Updated on June 04, 2022

Comments

  • Miles Bardan
    Miles Bardan almost 2 years

    http://pastebin.com/4gvcQm7P

    #include <iostream>
    
    using namespace std;
    
    int GenerateID()
    {
        static int nextID = 0;
        return nextID++;
    }
    
    void PrintInformation(Employee EmployeeName)
    {
        cout << EmployeeName << "'s ID is: " << EmployeeName.ID << endl;
        cout << EmployeeName << "'s age is: " << EmployeeName.age << endl;
        cout << EmployeeName << "'s wage is: " << EmployeeName.wage << endl;
    }
    
    int main()
    {
    
        struct Employee
        {
            int ID;
            int age;
            float wage;
        };
    
        Employee Dominic;
        Employee Jeffrey;
    
        Dominic.ID = GenerateID();
        Dominic.age = 22;
        Dominic.wage = 7.10;
    
        Jeffrey.ID = GenerateID();
        Jeffrey.age = 28;
        Dominic.wage = 7.10;
    
        PrintInformation(Dominic);
        PrintInformation(Jeffrey);
    
        return 0;
    }
    
    /*
    C:\CBProjects\Practise\main.cpp|11|error: variable or field 'PrintInformation' declared void|
    C:\CBProjects\Practise\main.cpp|11|error: 'Employee' was not declared in this scope|
    C:\CBProjects\Practise\main.cpp||In function 'int main()':|
    C:\CBProjects\Practise\main.cpp|39|error: 'PrintInformation' was not declared in this scope|
    ||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|
    */
    

    The above pastebin link shows the code I used and the build report. Following this report I attempted to forward declare the struct without including members and then there is an 'incomplete type' error.

    What is the solution?

    Edit: I'm using c++11

    Edit 2: Here is what happens if I try to forward declare the struct, including the members:

    http://pastebin.com/rrt4Yjes#