我是c ++的新手,正在尝试用其中一个主类创建多个类,并希望从main()函数访问其他类数据成员。
但是当我将两个类都保存在一个文件中时,代码工作正常,但是当我将两个类都保存在不同文件中时,它将抛出错误
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;
};
在此范围内未声明[错误]'t'
答案 0 :(得分:1)
您必须在主要课程之前声明教师班级(重新安排代码)。
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;
}
如果根据您的注释将类分配到不同的文件中,请使用正向声明,如下所示:
class Student;
class teacher;
int main(){
//use the classes here and the IDE should link with the other C++ files in the project.
}
答案 1 :(得分:1)
在主函数之前和使用前向声明的类的属性之前,您始终需要声明所有类。
要解决此特定情况,请在主函数之前声明教师班级(如您在前面的答案中所见)。
在某些不同的情况下,当您必须在另一个类对象中使用类对象时,可以先声明依赖类,然后可以使用一种称为前向声明的c ++技术。
例如,汽车课可以被包括在老师的课中(在某些国家,老师和学生的年龄都取决于学生的年龄)。因此,您可以在学生和老师之前实施此课程(在某些情况下可能会产生双重依赖,这很棘手),但是您也可以执行以下操作:
#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
}
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;
}
答案 2 :(得分:1)
在定义特定类型的变量时,此类型必须在此时为编译器“已知”。
如果要使用的类型都在同一个文件中定义,只需确保在首次将其用于变量/参数定义之前定义每种类型即可。
如果类型是在不同文件中定义的,则制作头文件(例如“ teacher.h”),并在需要的地方包括此头:
// teacher.h:
class teacher
{
public:
int teacherNo=999;
};
// main.cpp:
#include "teacher.h"
int main()
{
teacher t;
cout<< t.teacherNo;
}
答案 3 :(得分:0)
在C ++中,顺序很重要。 C ++无法识别您尚未得知的事情。只需颠倒main()
和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。您可能需要将teacher
更改为Teacher
或将Student
更改为student
,以便大写字母保持一致。
编辑:
这两个类都位于不同的文件中,即Student.cpp和Teacher.cpp
啊,那么在这种情况下,只需添加标题即可:
#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;
}
编辑2:
这两个类都位于不同的文件中,即Student.cpp和Teacher.cpp
在这种情况下,我建议为您的每个班级创建一个header file:
// 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;
}
答案 4 :(得分:0)
您应该将教师的头文件添加到其他班级。 如果您有Teacher.h文件,则将以下代码行添加到另一个类中:
#include "teacher.h"
这应该可以解决您的问题。
您可以在此处详细了解如何划分班级:https://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/
希望它有所帮助!