错误:方法未在此范围内声明(但已包含在内)
我有两个文件夹,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);
}
// ...
这是来自test/f1/common.h
的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
I have two folders, f1 and f2, and they are on same level (have same folder for parent). In f1 I have source code of my project, and in f2 I have unit tests.
The problem occurs when I try to include file from my project into unit test class. I just get this:
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
What is going on? The code in relevant files is, 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);
}
// ...
And here is common.h
from test/f1/common.h
(the include is found just fine):
#ifndef COMMON_H
#define COMMON_H
#include <QtCore>
QString CalculateMD5(uint number);
QString CalculateMD5(QString str);
#endif // COMMON_H
Here is project that won't compile (3 kb):
http://www.xx77abs.com/test2.rar
您的问题是您在 f2/tcommon.h 中复制了 f1/common.h 中的标头保护。
将它们更改为(在 tcommon.h 中):
问题已解决,程序已构建并且您可以运行它。回应:
fixed.zip
(请参阅此答案的来源)Your problem is that you have duplicated the header guards from f1/common.h in f2/tcommon.h.
Change these to (in tcommon.h):
and the problem is fixed, the program builds and you can run it. In response:
fixed.zip
(see source of this answer)