【问题标题】:Errors in class relationships in c++c++中类关系的错误
【发布时间】:2016-09-03 05:33:46
【问题描述】:

我对类关系的概念很陌生,并编写了一个程序来测试它。但是,它给了我一些错误。

有 5 个用于 University、Dept、Teacher、Student 和 Course 类的头文件和一个 .cpp 文件。我已经实现了大学和部门之间的组合以及部门和学生、教师、课程之间的双向关联

uni.h

#pragma once
//#include"dept.h"
class University
{
public:
    University(string,int);
    void setDepartmentName(string,int);
    Dept* getDeptAddress(int);
    ~University();
private:
    Dept* departments;
    int totalDepts;
    string name;
};

University::University(string name1,int num) :name(name1)
{
departments = new Dept[num];
totalDepts=num;
}

void University::setDepartmentName(string name,int depNo)
{
departments[depNo].setName(name);
}

Dept* University::getDeptAddress(int i)
{
return &(departments[i]);
}

University::~University()
{
delete[] departments;
}

dept.h

#pragma once
//class Student;
//class Teacher;
//class Course;
#include"course.h"
#include"teacher.h"
#include"student.h"
class Dept
{
    public:
    Dept();
    string getName();
    void setName(string);
    ~Dept();

private:
    Student** students;
    Course** courses;
    Teacher** teachers;
    string name;
    int noOfStudents, noOfTeachers, noOfCourses;
};

Dept::Dept()
{

}

string Dept::getName() {
    return name;
}

void Dept::setName(string name1) {
    name = name1;
}
Dept::~Dept()
{
}

course.h

#pragma once
class Dept;
//#include"dept.h"
class Course
{
public:
    Course(string, string);
    void assignDept(Dept*);
    string getDeptName();
    ~Course();

private:
    Dept* department;
    string code;
    string name;

};

Course::Course(string code1, string name1) :code(code1), name(name1)
{} 
void Course::assignDept(Dept * dep)
{
    department = dep;
}
string Course::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
    return department->getName();
}

Course::~Course()
{}

student.h

#pragma once
#include"dept.h"
class Student
{
public:
    Student(string,string);
    void assignDept(Dept*);
    string getDeptName();
    ~Student();

private:
    Dept* department;
    string rollNo, name;
};

Student::Student(string rNo,string name1):name(name1),rollNo(rNo)
{}


void Student::assignDept(Dept *dep)
{
    department = dep;
}

string Student::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
    return department->getName();
}


Student::~Student()
{
}

teacher.h

#pragma once
//#include"dept.h"
class Teacher
{
public:
    Teacher(string);
    void assignDept(Dept*);
    string getDeptName();
    ~Teacher();
private:
    Dept* department;
    string name;
};

Teacher::Teacher(string name1) :name(name1)
{}


void Teacher::assignDept(Dept *dep)
{
    department = dep;
}

string Teacher::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
    return department->getName();



}

Teacher::~Teacher()
{
}

source.cpp

#include<iostream>
using namespace std;
#include<string>
#include"dept.h"
#include"course.h"
#include"teacher.h"
#include"student.h"
#include"uni.h"

int main()
{

University u1("FAST",3);
u1.setDepartmentName("CS", 0);
u1.setDepartmentName("EE", 1);
u1.setDepartmentName("CV", 2);

Student s1("l144049", "Syed Abdul Wahab");
Course c1("cs1", "ITC");
Teacher t1("abc");

c1.assignDept(u1.getDeptAddress(0));
t1.assignDept(u1.getDeptAddress(1));
s1.assignDept(u1.getDeptAddress(2));

cout << c1.getDeptName()<<endl;
cout << t1.getDeptName() << endl;
cout << s1.getDeptName() << endl;

cin.get();
return 0;

}

但是,如果我在student.h、course.h 和teacher.h 中#include 'dept.h',它会给我在'Dept' 上的错误,即'identifier 'Dept' is undefined'。 任何帮助我都将不胜感激!

【问题讨论】:

  • 代码太多了。请尝试创建minimal reproducible example 并发布。
  • 我已经将它最小化了一点,但它需要全部 6 个文件才能完成。
  • 不要在头文件中编写你的实现,它会与你所了解的前向声明一起工作。
  • “最小”示例不需要 6 个文件。最多 3 个。
  • void Dept::setName(string name1) -> void Dept::setName(const string&amp; name1) 会更好

标签: c++ class composition model-associations


【解决方案1】:

问题是你有一个循环依赖:teacher.h 包括dept.h,其中包括teacher.h。这行不通。

要修复它,请在头文件中使用“前向声明”,并将实现移至 .cpp 文件。

【讨论】:

    猜你喜欢
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多