[英]C++ Syntax error in header using class from another header
[英]using an abstract class from another header file in c++
我有 3 個文件
尺寸.h:
namespace mtm {
class Dimensions {
int row, col;
//some code
};
}
IntMatrix.h:
#include "dimentions.h"
namespace mtm
{
class intMatrix
{
private:
int** data;
int col;
int row;
public:
intMatrix(Dimensions dims, int num=0);
//some code
};
//the line bellow is where I get the error
intMatrix(Dimensions dims, int num=0): col(dims.getCol()),row(dims.getRow()) ,data(new int*[dims.getRow()])
{
for(int i=0;i<dims.getCol())
{
data[i](new int[dims.getCol]);
}
for(int i=0;i<dims.getRow();i++)
{
for(int j=0;j<dims.getCol();j++)
{
data[i][j]=num;
}
}
}
}
編譯器在“dims”之前說:預期的“)”,當我將鼠標放在 dims 上時,vs 代碼說:“error-type mtm::dims”但 dims 不是一種類型,它是一個可變的類型。
IntMatrix.cpp:
#include "IntMatrix.h"
using namespace mtm;
//some code
在 IntMatrix.cpp 中,問題在於它無法識別 Dimentions 是什么,但它確實識別出 intMatrix 是什么。
歡迎來到 StackOverflow!
編譯器消息有時會產生誤導,在您的情況下,您會遇到錯誤,因為您在實現中重復了默認值。 這可能是編譯器抱怨的錯誤。
編輯作為 drescherjm 的指針,您還忘記了將 class 名稱添加到構造函數
正確的定義應該是:
intMatrix::intMatrix(Dimensions dims, int num): ...
之后如果您仍然有錯誤,請告訴我。
在您的 C++ 源代碼中,構造函數必須定義為
intMatrix::
intMatrix(維度暗淡,int num)....
所以你有兩個錯誤:你必須添加intMatrix
和 delete =0
但是永遠不要像這樣寫矩陣 class 。 這是可怕的 C++ 代碼。 您永遠不需要直接調用new
。 在這種情況下,您的數據被放置在非常不友好的緩存中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.