错误:方法未在此范围内声明(但已包含)
Error: method was not declared in this scope (but it is included)
我有两个文件夹,f1和f2,它们在同一级别(有相同的文件夹为父)。在f1中,我有项目的源代码,在f2中,我有单元测试。
当我试图将项目中的文件包含到单元测试类中时,就会出现问题。我就得到这个:
natty:/tmp/test/f2$ qmake-qt4 .
natty:/tmp/test/f2$ make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtTest -I../f1 -I. -o main.o main.cpp
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtTest -I../f1 -I. -o tcommon.o tcommon.cpp
tcommon.cpp: In member function ‘void tcommon::tCalculateMD5_str()’:
tcommon.cpp:21:50: error: ‘CalculateMD5’ was not declared in this scope
tcommon.cpp: In member function ‘void tcommon::tCalculateMD5_uint()’:
tcommon.cpp:43:50: error: ‘CalculateMD5’ was not declared in this scope
make: *** [tcommon.o] Error 1
怎么回事?相关文件中的代码为,test/f2/tcommon.cpp
:
#include "tcommon.h"
#include <common.h>
// ...
void tcommon::tCalculateMD5_str()
{
QFETCH(QString, string);
QFETCH(QString, result);
// THIS IS LINE 21 <-----------------------------------------------
QCOMPARE(CalculateMD5(string), result);
}
// ...
这里是common.h
从test/f1/common.h
(包括发现很好):
#ifndef COMMON_H
#define COMMON_H
#include <QtCore>
QString CalculateMD5(uint number);
QString CalculateMD5(QString str);
#endif // COMMON_H
这是一个无法编译的项目(3 kb):http://www.xx77abs.com/test2.rar
您的问题是您在f2/tcommon.h中复制了f1/common.h的标题保护。
修改为(in tcommon.h):
#ifndef TCOMMON_H
#define TCOMMON_H
//...
#endif // TCOMMON_H
,问题被修复,程序构建,你可以运行它。作为回应:fixed.zip
(参见此回答的来源)
相关文章:
- 包含的类类型显然没有声明,为什么?
- 如果 x.h 仅由函数声明组成,为什么有必要在 x 中包含 x.h.cpp
- C++(和 ROS) - 包含与前向声明引用,设置默认值和类型定义
- 我包含了<winuser.h>,但编译器在MonitorFromWindow上抛出"未声明"
- 即使不包含其标头,如何成功向前声明的类编译?
- 包含文件中的 Typdef "未在此范围内声明"
- 程序只适用于包含(无副作用)cout声明
- 循环包含依赖项/转发声明
- 我收到未声明的标识符错误,但我已包含头文件?
- 如何声明包含整数数组的对象
- 我需要在不使用前向声明的情况下相互包含两个头文件,导致"incomplete type"错误
- 我需要声明一个包含整数向量的 4x4 矩阵
- 错误: 包含 <Kinect.h> 时未声明"_Out_"
- 为什么我们可以创建一个模板类结构,其中包含许多与声明结构时指定的参数不同的参数
- 如何声明包含结构的类的成员函数
- 在声明时,向前斜线或后斜线包含在C 上
- 如何在不包含 Windows.h 的情况下获取 IsDebuggerPresent 的声明
- 缺少定义错误可以通过只包含声明来解决
- 互惠友元成员函数=循环包含声明
- CMake 正则表达式匹配包含声明中的标头名称