如何将程序拆分为文件C 类

How to split program into files- class c++

本文关键字:文件 拆分 程序      更新时间:2023-10-16

我现在有点困惑,我正在尝试将程序拆分为file.h file.cpp和main.cpp,这是我的代码的示例:

//file.h//
class matrix
{
   private:
      class rcmatrix;
      rcmatrix *data;
   public:
     class Cref;
     class Size_Check{};
     matrix();
    ~matrix();
     matrix(const int rows, const int cols, const double num);
};
class rcmatrix
{
    private:
      rcmatrix(const rcmatrix&);
      rcmatrix &operator =(const rcmatrix&);
    public:
      int rows;
      int cols;
      double **mat;
      int n;
      rcmatrix();
     ~rcmatrix();
};
//file.cpp
matrix::rcmatrix::rcmatrix()
{
    rows=0;
    cols=0;
    mat=NULL;
    n=1;
};
matrix::rcmatrix::rcmatrix(const int r, const int c, const double num)
{
    //instructions.
};

我得到以下错误:

error: invalid use of incomplete type ‘class matrix::rcmatrix’
error: ‘class matrix::rcmatrix’ is private
error: expected unqualified-id before ‘const’
matrix::rcmatrix(const int r, const int c, const double num)

我知道它不会这样起作用,但是我尝试了许多其他解决方案,但没有结果,因此请感谢您对初学者的任何提示/建议。

在包含类的外部声明嵌套类时,您必须使用其全名 class matrix::rcmatrix

否则,编译器认为您想声明另一个完全分开的班级。