使用单元测试 (cppunit) 中另一个文件中定义的结构时出错
Error using struct defined in another file in UnitTesting (cppunit)
我有一个Parser.h
,它定义了一个结构StmtParent
:
...
struct StmtParent;
class Parser {
...
然后在Parser.cpp
:
struct StmtParent {
int stmtNo;
int parent;
};
...
好像没事吧?然后我有一个单元测试(cppunit):
# in ParserUnitTests.h
#include "headerParser.h"
# in ParserUnitTests.cpp
void ParserUnitTests::testParseProcSideEffects() {
...
stack<StmtParent> follows;
...
然后我收到这样的错误:
error C2027: use of undefined type 'StmtParent'
为什么,我可以使用像Parser::parseLine()
这样的函数。为什么我无法访问结构?所以我尝试在ParserUnitTests.cpp
中包含Parser.h
(尽管我已经将其包含在标题中)。然后我得到:
Error 8 error C2146: syntax error : missing ';' before identifier 'm_cCurToken' c:program files (x86)microsoft sdkswindowsv7.0aincludeparser.h 52
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:program files (x86)microsoft sdkswindowsv7.0aincludeparser.h 52
Error 10 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:program files (x86)microsoft sdkswindowsv7.0aincludeparser.h 52
...
Parser.h
不定义结构,而是向前声明它。因此,当您尝试将其用作stack
的模板参数时,它是不完整的,并且不能将不完整的类型用作 STL 容器的参数:
C++11草案3035,17.4.3.6,第2款:
具体而言,在以下情况下未定义效果:
。
如果在实例化模板组件时将不完整的类型 (3.9) 用作模板参数, 除非该组件特别允许。
你可以看看这个推理。
相关文章:
- 在类构造函数中定义结构变量的参数
- C++ 通过函数传递自定义结构
- 在C++中循环访问自定义结构列表的小问题
- 如何在Qt中使用QDataStream将自定义结构保存/加载到二进制文件中?
- 如何在C++中的.h文件中的类中定义结构
- 如何定义C结构:C-linkage和udt
- 结构向量中自定义结构函数的内存使用
- 类型转换自定义结构
- 使用结构绑定更改自定义结构的值
- 信号槽自定义结构问题
- 如何将unordered_set与自定义结构一起使用?
- 定义结构中没有默认构造函数的类
- 为什么C++在自定义结构向量时会出现 ostream 重载问题?
- 通过自定义结构和链接列表类中的C 中的哈希表
- 从自定义结构数组中提取数据
- 无法使用自定义结构的属性
- 通过引用将自定义结构的向量传递给 boost::compute 闭包或函数
- 如何从输入中定义的"external" int 定义结构中的数组大小
- 定义结构数组
- 在Cython中定义C 结构返回类型