在 C++ 中使用另一个头文件中的抽象类

using an abstract class from another header file in c++

本文关键字:文件 抽象类 C++ 另一个      更新时间:2023-10-16

我有 3 个文件

尺寸:

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 时,vs 代码说:"错误类型 mtm::d ims" 但Dims不是一种类型,而是一种可诉。

IntMatrix.cpp:

#include "IntMatrix.h"
using namespace mtm;
//some code

在IntMatrix中.cpp问题是它不认识Dimentions是什么,但是它确实识别intMatrix是什么。

欢迎来到 StackOverflow!

编译器消息有时会产生误导,在您的情况下,由于在实现中重复默认值而出现错误。这可能是编译器抱怨的错误。

编辑作为 drescherjm 的指针输出,您也忘记了将类名添加到构造函数中

正确的定义应该是:

intMatrix::intMatrix(Dimensions dims, int num): ...

如果之后您仍然遇到错误,请告诉我。

在C++源代码中,构造函数必须定义为

intMatrix::intMatrix(尺寸变暗,整数( ....

所以你有两个错误:你必须添加intMatrix和删除=0

但是永远不要写这样的矩阵类。这C++代码中很糟糕。您永远不需要直接致电new。在这种情况下,您的数据非常不友好