-1

I am new to c++ and am trying to make multiple classes with a main class in one of the class and want to access other class data members from main() function.

But when I kept both the classes in single file then the code is working fine but when I keep both the classes in different file then it throws error

class Student
{
    public:   
    int rollno=100;

};

int main()
{
    Student A;
    teacher t;
    cout<< A.rollno<<endl;
    cout<< t.teacherNo;
}

//another class
class teacher
{
    public:    
    int teacherNo=999;

};

[Error] 't' was not declared in this scope

infinity
  • 25
  • 6
  • 2
    Protip: Always read the compiler errors from the top. Very often errors are results of other errors. In this case, the first error would be something along the lines of "Type `Teacher` was not declared in this scope" – Yksisarvinen Jun 04 '19 at 21:53
  • 1
    "*But when I kept both the classes in single file then the code is working fine but when I keep both the classes in different file then it throws error*" You forgot to`#include` the file `Teacher.h`. Or forgot to create one, in which case I recommend one of the [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Yksisarvinen Jun 04 '19 at 21:56
  • Remember that, unlike in Java, C++ doesn't have a "main class" or a "class with a main method". – einpoklum Jun 04 '19 at 21:57

5 Answers5

1

You have to declare Teacher class before the main (rearrange your code).

    class Student
    {
        public:   
        int rollno=100;

    };

    class teacher
    {
        public:    
        int teacherNo=999;

    };

    int main()
    {
        Student A;
        teacher t;
        cout<< A.rollno<<endl;
        cout<< t.teacherNo;
    }

In case the classes are delcated in different files as per your comment just use forward declaration like this:

class Student;
class teacher;

int main(){
    //use the classes here and the IDE should link with the other C++ files in the project.
}
Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
1

You will always need to declare all of the classes before your main function, and before you use the forward declarated class's properties.

To solve this specific case: declare the teacher class before the main function (as you can see in previous answers).

For some different cases, when you have to use class object inside another class object, you can declare the dependency class before, and you can use a c++ technique called forward declaration.

For example, Car's class can be included in teacher's class (and in some countries in both teacher & student- depend on max student age). So you can implement this class before student & teacher (in some cases of double dependency it can be tricky), but you can also do something like this:

#include <memory>
#include <iostream>

using namespace std;

class Car; // forward declaration of Car class.

class Student
{
public:
    Student(); // don't implement yet!
    unique_ptr<Car> car; // not implemented yet
    int rollno=100;

};

class Teacher
{
public:
    Teacher(); // don't implement yet!
    unique_ptr<Car> car; // not implemented yet
    int teacherNo=999;
};

// Implement forward declaration class, before using in inside a function
class Car {
public:
    string name;
};

// Implement Student & Teacher constructors

Student::Student() {
    car = unique_ptr<Car>(new Car()); // this line can be written only after Car's class implementation!
}

Teacher::Teacher() {
    car = unique_ptr<Car>(new Car()); // this line can be written only after Car's class implementation!
}

int main()
{
    Student s;
    Teacher t;
    cout<< s.rollno << endl;
    cout<< t.teacherNo << endl;
    t.car->name = "Car name";
    s.car->name = "Car name2";

    cout<< s.car->name << endl; // Only after Car's class implementation you can access Car's object
    cout<< t.car->name << endl; // Only after Car's class implementation you can access Car's object
}

EDIT: Forward declaration in separated files

Student.h

#ifndef PROJECT_STUDENT_H
#define PROJECT_STUDENT_H

#include <memory>

class Car; // forward declaration of Car class.

class Student
{
public:
    Student(); // don't implement yet!
    std::unique_ptr<Car> car; // not implemented yet
    int rollno=100;

};

#endif //PROJECT_STUDENT_H

Student.cpp

#include "Student.h"
#include "Car.h" // In case of forward declaration, usually it's better to write includes in .h file, but sometimes it's not possible (in cases of double dependency).

// Implement Student constructors

Student::Student() {
    car = std::unique_ptr<Car>(new Car()); // this line can be written only after Car's class implementation!
}

Teacher.h

#ifndef PROJECT_TEACHER_H
#define PROJECT_TEACHER_H

#include <memory>

class Car;

class Teacher
{
public:
    Teacher(); // don't implement yet!
    std::unique_ptr<Car> car; // not implemented yet
    int teacherNo=999;
};

#endif //PROJECT_TEACHER_H

Teacher.cpp

#include "Teacher.h"
#include "Car.h" // In case of forward declaration, usually it's better to write includes in .h file, but sometimes it's not possible (in cases of double dependency).

// Implement Teacher constructors

Teacher::Teacher() {
    car = std::unique_ptr<Car>(new Car()); // this line can be written only after Car's class implementation!
}

Car.h

#ifndef PROJECT_CAR_H
#define PROJECT_CAR_H

#include <string>

// Implement forward declaration class, before using in inside a function
class Car {
public:
    std::string name;
};

#endif //PROJECT_CAR_H

Car.cpp - Empty

main.cpp

#include <memory>
#include <iostream>
#include "Student.h"
#include "Teacher.h"
#include "Car.h"
using namespace std;

int main()
{
    Student s;
    Teacher t;
    cout<< s.rollno << endl;
    cout<< t.teacherNo << endl;
    t.car->name = "Car name";
    s.car->name = "Car name2";

    cout<< s.car->name << endl;
    cout<< t.car->name << endl;
}
Community
  • 1
  • 1
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
1

When defining a variable of a specific type, this type has to be "known" to the compiler at this point.

If the types you want to use are all defined in the same file, just make sure that you define each type before you use it the first time for a variable/parameter definition.

If the types are defined in different files, make header files (e.g. "teacher.h") and include this header wherever needed:

// teacher.h:
class teacher
{
    public:    
    int teacherNo=999;

};

// main.cpp:
#include "teacher.h"

int main()
{
    teacher t;
    cout<< t.teacherNo;
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

In C++, order matters. C++ can't recognize things you haven't told about it yet. Simply reverse the order of main() and class teacher:

class Student
{
    public:   
    int rollno=100;

};

//another class
class teacher
{
    public:    
    int teacherNo=999;

};

int main()
{
    Student A;
    teacher t;
    cout<< A.rollno<<endl;
    cout<< t.teacherNo;
}

P.S. You may want to change teacher to Teacher or Student to student so your capitalization is consistent.

Edit:

Both the classes are in different files i.e. Student.cpp and teacher.cpp

Ah, then in that case, just include the header for it:

#include "teacher.h" // or whatever the file teacher is in is called

class Student
{
    public:   
    int rollno=100;

};

int main()
{
    Student A;
    teacher t;
    cout<< A.rollno<<endl;
    cout<< t.teacherNo;
}

Edit 2:

Both the classes are in different files i.e. Student.cpp and teacher.cpp

In that case, I recommend creating a header file for each of your classes:

// student.h

class Student
{
    public:   
    int rollno=100;

};



// teacher.h

class teacher
{
    public:    
    int teacherNo=999;

};

// main.cpp, or whatever you decide to call it

#include "teacher.h" 
#include "student.h" 

int main()
{
    Student A;
    teacher t;
    cout<< A.rollno<<endl;
    cout<< t.teacherNo;
}

0

You should add the header file of teacher to the other class. If you have teacher.h file, the add the following line of code to your other class:

#include "teacher.h"

That should probably fix your problem.

You can read more about how you divide classes here: https://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/

Hope it helped!