错误:方法未在此范围内声明(但包含在内)

时间:2011-09-11 00:18:19

标签: c++ unit-testing qt compiler-errors subdirectory

我有两个文件夹,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的{​​{1}}(其中包含的内容很好):

test/f1/common.h

这是无法编译的项目(3 kb): http://www.xx77abs.com/test2.rar

1 个答案:

答案 0 :(得分:3)

您的问题是您在f2 / tcommon.h中复制了f1 / common.h中的标题保护。

将这些更改为(在tcommon.h中):

#ifndef TCOMMON_H
#define TCOMMON_H

//...

#endif // TCOMMON_H

并且问题已解决,程序构建并且您可以运行它。回复:fixed.zip(请参阅source of this answer

相关问题