在 C++ 中使用另一个头文件中的抽象类
using an abstract class from another header file in c++
我有 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
。在这种情况下,您的数据非常不友好
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 无法创建抽象类的实例
- 用pybind11包装C++抽象类时出错
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 有没有办法按值将纯抽象类的所有子类传递给 C++ 中的函数?
- 抽象类错误,请参阅声明" "是抽象的
- 将自定义函数传递到基抽象类中以延迟执行
- C++头文件和类实现出现问题
- 将包含抽象类和普通类C++包导出到 Python
- C++:处理抽象类中的错误时出现问题
- C ++如何在原始抽象类中创建一个函数,该函数接受派生类的输入
- 在 C++ 中使用另一个头文件中的抽象类
- ATL::CComContainedObject<contained>: C2259 无法实例化抽象类
- C++:从抽象类重写纯虚拟运算符重载
- .h 或.cpp文件中的抽象类或两者兼而有之?
- 我可以为抽象类提供.cpp文件吗?
- 在C++中使用带有头文件的抽象类
- Visual C++和Allegro5:无法识别头文件中抽象类中的对象
- c++中的头文件和Java中的抽象类/接口实现是相同的想法,这是正确的吗?
- 我需要一个抽象类的.cpp文件吗