函数未被识别为命名空间的一部分
Functions not recognized as being part of namespace
我正在编写一个类似单元测试的简短程序,作为一个更大项目的一部分。我在一个"Utilities.hpp"文件中编写了一些简单的函数,我正在为它编写测试。这是代码。。。为了长度起见,已经排除了一些函数定义。
//Main.cpp
#ifdef RUN_TESTS
#include "Logger.hpp"
#include "UnitTester.hpp"
#define TEST_ALL
int main(int, char*[])
{
logger::init();
logger::setSeverityLevel(INFO);
auto slg = logger::getSLogger();
BOOST_LOG_SEV(slg, INFO) << "Starting Unit Tests...";
testing::UnitTester testObject;
testObject.runTests();
}
#endif
公用设施.hpp
#pragma once
#include <string>
#include <stack>
#include <vector>
namespace util
{
void swapChars(char& a, char& b); //flips two chars
std::string reverseString(const std::string& str); //stack based implementation of string inversion
std::vector<std::string> splitStrAtSubstr(const std::string& str, const std::string& split); //splits string into parts seperated by "split"
}
公用设施.cpp
#include "Utilities.hpp"
void util::swapChars(char& a, char& b)
{
...
}
std::string util::reverseString(const std::string& str)
{
...
}
std::vector<std::string> util::splitStrAtSubstr(const std::string& str, const std::string& split)
{
...
}
最后是UnitTester类。。。UnitTester.hpp
#pragma once
#define RUN_TESTS
#define RUN_ALL
#ifdef RUN_TESTS
#include "Logger.hpp"
#include <string>
#include <vector>
namespace testing
{
class UnitTester
{
public:
UnitTester();
~UnitTester();
void runTests();
private:
src::severity_logger<severity_level> testLogger;
void utilities();
void logging();
void resourceGroup();
void resourceManager();
void INIParser();
};
}
#endif
UnitTester.cpp
#include "UnitTester.hpp"
#ifdef RUN_TESTS
void testing::UnitTester::runTests()
{
#ifdef RUN_ALL
utilities();
logging();
resourceGroup();
resourceManager();
INIParser();
#elif
#ifdef TEST_UTILITIES
utilities();
#endif
#ifdef TEST_LOGGING
logging();
#endif
#ifdef TEST_RESOURCE_GROUP
resourceGroup();
#endif
#ifdef TEST_RESOURCE_MANAGER
resourceMananager();
#endif
#ifdef TEST_INI_PARSER
INIParser();
#endif
#endif
}
//PRIVATE FUNCTIONS
void testing::UnitTester::utilities()
{
#include "Utilities.hpp"
BOOST_LOG_SEV(testLogger, INFO) << "Now testing: void util::swapChars(char& a, char& b);";
char a = 'a', b = 'b';
util::swapChars(a, b);
if (a != 'b' || b != 'a') { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::swapChars failed."; }
BOOST_LOG_SEV(testLogger, INFO) << "Now testing: std::string util::reverseString(const std::string& str);";
if (util::reverseString("myString") != "gnirtSym") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: "myString""; }
if (util::reverseString("MYSTRING ") != " GNIRTSYM") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: "MYSTRING ""; }
if (util::reverseString("aaaaaa") != "aaaaaa") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: "aaaaaa""; }
if (util::reverseString("This Is An Extended TEST") != "TSET dednetxE nA sI sihT") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: "This Is An Extended Test""; }
if (util::reverseString(""\"Escape") != "epacsE"\"") { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::reverseString failed. IN: ""\"Escape""; }
BOOST_LOG_SEV(testLogger, INFO) << "Now testing: std::vector<std::string> splitStrAtSubstr(const std::string& str, const std::string& split)";
std::vector<std::string> expected("One", "Two", "Three");
std::vector<std::string> expected2("One", "Three");
if (util::splitStrAtSubstr("One.Two.Three", ".") != expected) { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::splitStrAtSubstr failed. IN: "One.Two.Three", ".""; }
if (util::splitStrAtSubstr("One"Two"Three", """) != expected) { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::splitStrAtSubstr failed. IN: "One"Two"Three", """"; }
if (util::splitStrAtSubstr("OneTwoThree", ".") != expected) { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::splitStrAtSubstr failed. IN: "One.Two.Three", ".""; }
if (util::splitStrAtSubstr("OneTwoThree", "Two") != expected) { BOOST_LOG_SEV(testLogger, ERROR) << "Input to util::splitStrAtSubstr failed. IN: "OneTwoThree", "Two""; }
}
void testing::UnitTester::logging()
{
#include "Logger.hpp"
}
void testing::UnitTester::resourceGroup()
{
#include "ResourceManagerResourceGroup.hpp"
}
void testing::UnitTester::resourceManager()
{
#include "ResourceManagerResourceGroup.hpp"
}
void testing::UnitTester::INIParser()
{
#include "INIParser.hpp"
}
#endif
以下是我认为的不同错误的代表性样本。我会把我认为最可疑的放在最上面。正如他们经常做的那样,许多错误只是由其他错误引起的。。。
关于Utilities.hp:中的函数声明
错误14错误C2039:"vector":不是"std"的成员
错误13错误C2143:语法错误:在"&"之前缺少","
错误15错误C2143:语法错误:缺少";"在'<'之前
错误10错误C2039:"string":不是"std"的成员
在UnitTester.hpp:中的BOOST_LOG_SEV调用上
错误69错误C2597:非法引用非静态成员"testing::UnitTester::testLogger"
错误49错误C2228:".stream"的左侧必须具有类/结构/联合错误63错误C2228:".open_record"的左侧必须具有类/结构/联合
关于UnitTester.cpp:中的函数调用
错误51错误C2039:"splitStrAtSubstr":不是"util"的成员
错误31错误C2664:"int util::reverseString(const int)":无法将参数1从"const char[10]"转换为"const int"^^发生这种情况是因为编译器找不到std::string作为类型,并且默认为int
这些似乎是最重要的错误。对我来说,这个问题几乎肯定会发生在Utilities.hpp中,编译器似乎无法识别std::string和std::vector,并对函数声明进行了一些更巧妙的解释。
提前谢谢。
您包含函数体内部的文件,我认为它不应该像这样使用。
#include
是预编译器指令。预编译器甚至在实际编译器启动之前就已经运行了。这使它能够修改代码。就像#define可以替换某些单词一样,#include可以包括整个文件。
void testing::UnitTester::logging()
{
#include "Logger.hpp"
}
想象一下编译器实际会收到什么,比如用Logger.hpp.的内容替换#include "Logger.hpp"
请不要在函数体中使用#include
。
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- 嵌套的匿名命名空间
- CMakeLists.txt中的命名空间表示法
- 类是C++中的命名空间吗
- 在命名空间中使用全局命名空间中的函数
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- '使用命名空间{嵌套在另一个命名空间中的某个命名空间}"
- 是否可以将函数导入命名空间,但不能导出它?
- C++ C++类中的命名空间降级
- 如何使用 soong 命名空间来有条件地编译模块
- 使用 Clang++ 有没有办法将文件作为命名空间等包含?
- fabsf 是 C++11 中标准命名空间的一部分吗?
- 函数未被识别为命名空间的一部分
- 如何正确重载作为用户定义命名空间一部分的模板化类的插入运算符?