[英]C++: “error C4430: missing type specifier - int assumed” For constructor and invalid declaration for destructor
[英]“missing type specifier” error on constructor declaration
我在2个不同的文件中有2个类:
RegMatrix.h:
#ifndef _RM_H
#define _RM_H
#include "SparseMatrix.h"
...
class RegMatrix{
...
RegMatrix(const SparseMatrix &s){...} //ctor
...
};
#endif
SparseMatrix.h:
#ifndef _SM_H
#define _SM_H
#include "RegMatrix.h"
...
class SparseMatrix{
...
SparseMatrix(const RegMatrix &r){...} //ctor
...
};
#endif
在构造函数行上我得到错误:
错误C4430:缺少类型说明符 - 假定为int。
错误C2143:语法错误:'&'之前缺少','
但是当我添加类声明时
class SparseMatrix;
在RegMatrix.h文件中
class RegMatrix;
在SparseMatrix.h文件中它工作正常。 我的问题是,如果我有包含,为什么需要它? 10X。
你不能有循环#includes(一个文件#includes另一个#includes第一个文件)。 前面声明其中一个类而不是#include会打破链并允许它工作。 声明类名允许您使用该名称,而无需了解该类的内部位。
顺便说一下,对圆形#includes的渴望是一种设计气味。 也许你可以创建一个两个类可以依赖的接口? 然后他们就不必相互依赖。
你的标题包含不起作用,看看如果我在解析后包括SparseMatrix.h会发生什么:
#ifndef _SM_H
#define _SM_H
/// start of #include "RegMatrix.h"
#ifndef _RM_H
#define _RM_H
/// start of #include "SparseMatrix.h" inside "RegMatrix.h"
#ifndef _SM_H
// skipping, because _SM_H is defined and the condition is false
#endif
/// end of #include "SparseMatrix.h" inside "RegMatrix.h"
class RegMatrix{
...
RegMatrix(const SparseMatrix &s){...} //ctor
...
};
#endif
/// end of #include "RegMatrix.h"
...
class SparseMatrix{
...
SparseMatrix(const RegMatrix &r){...} //ctor
...
};
#endif
所以基本上,SparseMatrix是未定义的。 你无能为力。 只需申报你的班级前瞻声明。
如果首先包含RegMatrix.h
,它将包含SparseMatrix.h
。 然后,这将返回包括RegMatrix.h
,并跳过,因为已定义标头防护。 然后, SparseMatrix
继续进行定义,除了RegMatrix
从未甚至宣称。 然后你得到一个错误。
你不能有圆形包含。 你必须转发声明其中一个或两个,就像你做的那样。
声明如
class SparseMatrix;
被称为前瞻性声明。 它告诉编译器“某处”有一个该名称的类。 只要前向声明文件使用指针或对前向引用类的引用,它就会使编译器满意并且工作正常。 这是因为,从编译器的角度来看,指针或引用只是4个字节,与类内容无关。
在OP的代码中, SparseMatrix
和RegMatrix
都只用作(const)引用,因此前向声明足以使其工作。
但是,如果前向声明文件执行某些操作,需要编译器知道其大小,例如
void foo( SparseMatrix ); // note pass by value
然后编译器会抱怨:-)
在OP提出的特殊情况下,我倾向于完全抛弃共同的#include
并仅基于前向声明来设计界面。 实现(即.cpp文件) 可能必须包含两个头文件,但这不是问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.