编译C++代码时出现“不允许不完整的类型”错误

k5ifujac  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(196)

我试着制作GradeBook类,代表一门课程的成绩册。它具有存储课程名称的私有成员变量。类设置成员并获取课程名称,同时还显示欢迎消息。由于我是C++的初学者,我遇到了以下错误:

Incomplete type is not allowed
  • 下面是我正在编写的源代码:
#include<iostream>
#include<string>
using namespace std;
class GradeBook; // GradeBook class definition
{
public:
   // constructor initializes courseName
  GradeBook(string name)
  {
      setCourseName(name);
  }
  void setCourseName(string name)
  {
      courseName = name;
  }
  string getCourseName()
  {
      return courseName;
  }
  void displayMessage()
  {
      cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
  }
private:
  string courseName;
};

int main()
{

  GradeBook gradeBook1("CS101 Introduction to C++ Programming");
  GradeBook gradeBook2("Visual C++ introduction");

  cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
      << "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
      << endl;
  return 0;
}
  • 预期成果:
gradeBook1 created for course: CS101 Introduction to C++ Programming
gradeBook2 created for course: Visual C++ introduction
5cnsuln7

5cnsuln71#

删除class Gradebook之后的;,一切都应该正常。

相关问题