繁体   English   中英

在 c++ 中使用来自另一个 header 文件的抽象 class

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM