2013-10-22 168 views
1

我有下一個代碼 ,我不知道如何解決多功能。 編譯器給我一個消息 不能聲明參數「A」爲類型「Matriz」繼承的類問題

我應該使用正是在主

消息代碼不能聲明參數'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; 
} 
+0

如何包含完整的編譯信息並突出顯示有問題的行? – John3136

+0

@BergQuester看起來我們可以從他的屏幕名稱中推斷出:) –

+0

您正在構造函數中分配內存,但絕不會將其釋放到析構函數中,更不用說遵循三法則或零規則。 – chris

回答

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 
+0

你好,謝謝,讓我試試 – KarlaE

+0

是的,我有同樣的東西,但它必須運行的功能multithiithing是評論 – KarlaE

+0

這樣的事情void Matriz :: mult(IMatriz&a,IMatriz&b){ this-> setSize(a.getNumRows(),b.getNumColumns()); for(int rows = 0; rows KarlaE

0

你忘了新的運營商(或malloc的)

Matriz a = new Matriz(3, 2); 
+0

調用構造函數是合法的OP做了 – lolando

+0

你是不是指'Matriz * a =新的Matriz(3,2)'? – Sam

0

的第一個問題是你不能實例化一個抽象對象像IMatriz,你需要在下面的函數聲明中將其更改爲IMatriz* aIMatriz& a

virtual void mult(IMatriz a, IMatriz b) = 0;

其次,上述純虛函數不被Matriz類,這使得Matriz類還是一個抽象類覆蓋。因爲在派生類中,您聲明它爲void mult(Matriz a, Matriz b);,這是與基類中的不同的函數簽名。

解決的方法是將基址類和派生類中的void mult(Matriz a, Matriz b);更改爲void mult(IMatriz* a, IMatriz* b),並用指針調用它。

+0

嗨在那裏我有同樣的問題不能聲明參數'a'是'IMatriz' – KarlaE

+0

@cplusplus編輯類型,你不能使用抽象類作爲參數類型。 –