c++ - 继承类问题

标签 c++ class inherited

我有下一个代码 而且我不知道如何解决多重功能。 编译器给我发消息 不能将参数“a”声明为“Matriz”类型

我应该完全使用 main 中的代码

消息是无法将参数“a”声明为“Matriz”类型

enter code here
#include <iostream>
#include <iomanip>
using namespace std;
// Base
class IMatriz {
   int **m;
   int numRows;
   int numColumns;
   public:
      IMatriz(){
         numRows = 0;
         numColumns = 0;
         m = NULL;
      }   
      IMatriz(int r, int c) {
         numRows = r;
         numColumns = c;
         m = new int* [numRows];
         for(int i=0; i < numRows; i++)
            m[i] = new int [numColumns];
      }
      virtual void setSize(int r, int c) = 0;
      virtual void setValue(int row, int col, int val) = 0;
      virtual int getValue(int row, int col) = 0;
      virtual int getNumRows() = 0;
      virtual int getNumColumns() = 0;
      virtual void mult(IMatriz a, IMatriz b) = 0;
      virtual void print(void) = 0;
};
// Inherited
class Matriz : public IMatriz {
   protected:
      int **m;
      int numRows;
      int numColumns;   
   public:
      Matriz()
     :  IMatriz()
     {
     }

  Matriz(int r, int c)
     :  IMatriz(r, c)
     {
        numRows = r;
        numColumns = c;
        m = new int* [numRows];

        for(int i=0; i < numRows; i++)
           m[i] = new int [numColumns];
     }

  void setSize(int r, int c);
  void setValue(int row, int col, int val);
  int getValue(int row, int col);
  int getNumRows();
  int getNumColumns();
  void mult(Matriz a, Matriz b);
  void print();
};
// Functions
void Matriz::setSize(int r, int c) {  
   numRows = r;
   numColumns = c;
}
void Matriz::setValue(int row, int col, int val) {    
   m[row][col] = val;
}
int Matriz::getValue(int row, int col) {
   return m[row][col];
}
int Matriz::getNumRows() {
   return numRows;
}
int Matriz::getNumColumns() {
   return numColumns;
}
**void Matriz::mult(Matriz a, Matriz b) {**
//   m.setSize( a.getNumRows(), b.getNumColumns() );
//   for (int rows = 0; rows < numRows; rows ++)
//      for (int cols = 0; cols < numColumns; cols ++)
//         m[rows][cols] = 0;
//   for (int rows = 0; rows < a.getNumRows(); rows ++)
//      for (int cols = 0; cols < b.getNumColumns(); cols ++)
//         for (int auxl = 0; auxl < a.getNumColumns(); auxl ++)
//            m[rows][cols] += (a[rows][auxl] * b[auxl][cols]);
   return;
}
void Matriz::print() {
   for (int rows  = 0; rows < numRows; rows ++)
   {
      for (int cols = 0; cols < numColumns; cols ++)
         cout << m[rows][cols] << " ";

      cout << endl;
   }
}
// Principal
int main() {
   Matriz m;
   Matriz a(3, 2);
   Matriz b(2, 3);
   a.setValue(0,0,7);
   a.setValue(0, 0, 7);
   a.setValue(1, 0, 1);
   a.setValue(2, 0, 8);
   a.setValue(0, 1, 2);
   a.setValue(1, 1, 5);
   a.setValue(2, 1, 6);
   b.setValue(0, 0, 2);
   b.setValue(1, 0, 3);
   b.setValue(0, 1, 5);
   b.setValue(1, 1, 4);
   b.setValue(0, 2, 8);
   b.setValue(1, 2, 9);
   a.print();
   b.print();
//   m.mult(a,b);
//   m.print();
   return 0;
}

最佳答案

对您的代码进行了一些修改。请在下面找到固定代码 -

#include <iostream>
#include <iomanip>
using namespace std;
// Base

class IMatriz;

class IMatriz {
    int **m;
    int numRows;
    int numColumns;
public:
    IMatriz(){
        numRows = 0;
        numColumns = 0;
        m = NULL;
    }
    IMatriz(int r, int c) {
        numRows = r;
        numColumns = c;
        m = new int* [numRows];
        for(int i=0; i < numRows; i++)
            m[i] = new int [numColumns];
    }
    virtual void setSize(int r, int c) = 0;
    virtual void setValue(int row, int col, int val) = 0;
    virtual int getValue(int row, int col) = 0;
    virtual int getNumRows() = 0;
    virtual int getNumColumns() = 0;
    virtual void mult(IMatriz &a, IMatriz &b) = 0;
    virtual void print(void) = 0;
};
// Inherited
class Matriz : public IMatriz {
protected:
    int **m;
    int numRows;
    int numColumns;
public:
    Matriz()
    :  IMatriz()
    {
    }

    Matriz(int r, int c)
    :  IMatriz(r, c)
    {
        numRows = r;
        numColumns = c;
        m = new int* [numRows];

        for(int i=0; i < numRows; i++)
            m[i] = new int [numColumns];
    }

    void setSize(int r, int c);
    void setValue(int row, int col, int val);
    int getValue(int row, int col);
    int getNumRows();
    int getNumColumns();
    void mult(IMatriz &a, IMatriz &b);
    void print();
};
// Functions
void Matriz::setSize(int r, int c) {
    numRows = r;
    numColumns = c;
}
void Matriz::setValue(int row, int col, int val) {
    m[row][col] = val;
}
int Matriz::getValue(int row, int col) {
    return m[row][col];
}
int Matriz::getNumRows() {
    return numRows;
}
int Matriz::getNumColumns() {
    return numColumns;
}
void Matriz::mult(IMatriz &a, IMatriz &b) {
    //   m.setSize( a.getNumRows(), b.getNumColumns() );
    //   for (int rows = 0; rows < numRows; rows ++)
    //      for (int cols = 0; cols < numColumns; cols ++)
    //         m[rows][cols] = 0;
    //   for (int rows = 0; rows < a.getNumRows(); rows ++)
    //      for (int cols = 0; cols < b.getNumColumns(); cols ++)
    //         for (int auxl = 0; auxl < a.getNumColumns(); auxl ++)
    //            m[rows][cols] += (a[rows][auxl] * b[auxl][cols]);
    return;
}
void Matriz::print() {
    for (int rows  = 0; rows < numRows; rows ++)
    {
        for (int cols = 0; cols < numColumns; cols ++)
            cout << m[rows][cols] << " ";

        cout << endl;
    }
}
// Principal
int main(int argc, const char * argv[]) {
    Matriz m;
    Matriz a(3, 2);
    Matriz b(2, 3);
    a.setValue(0,0,7);
    a.setValue(0, 0, 7);
    a.setValue(1, 0, 1);
    a.setValue(2, 0, 8);
    a.setValue(0, 1, 2);
    a.setValue(1, 1, 5);
    a.setValue(2, 1, 6);
    b.setValue(0, 0, 2);
    b.setValue(1, 0, 3);
    b.setValue(0, 1, 5);
    b.setValue(1, 1, 4);
    b.setValue(0, 2, 8);
    b.setValue(1, 2, 9);
    a.print();
    b.print();
    //   m.mult(a,b);
    //   m.print();
    return 0;
}

这是我在 xcode 控制台上得到的输出 -

7 2 
1 5 
8 6 
2 5 8 
3 4 9 

关于c++ - 继承类问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508188/

相关文章:

c++ - 在没有注册表的情况下存储应用程序数据/设置的方法?

c# - 如何重构此 C# 代码

python - 如何在科学的Python脚本中处理全局参数

flutter - 我该如何解决这个弱警告? "The field doesn’ t 覆盖继承的 getter 或 setter。”

c++ - 找不到v141的构建工具(Platform Toolset = 'v141')

c++ - 运算符优先级(bool 之前是否为 void*?)

用于装箱和拆箱的 C++ 类

c++ - 对象表示和运算符sizeof的定义

java - 类要求继承抽象的未实现方法

c++ - C++中继承变量的使用方法