C++ 函数未被识别为命名空间的一部分
作为一个更大项目的一部分,我正在编写一个类似于单元测试的短程序。我已经在“Utilities.hpp”文件中编写了一些简单的函数,我正在为其编写测试。这是代码。。。出于长度考虑,某些函数定义已被排除C++ 函数未被识别为命名空间的一部分,c++,unit-testing,boost,C++,Unit Testing,Boost,作为一个更大项目的一部分,我正在编写一个类似于单元测试的短程序。我已经在“Utilities.hpp”文件中编写了一些简单的函数,我正在为其编写测试。这是代码。。。出于长度考虑,某些函数定义已被排除 //Main.cpp #ifdef RUN_TESTS #include "Logger.hpp" #include "UnitTester.hpp" #define TEST_ALL int main(int, char*[]) { logger::init(); logg
//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
//Main.cpp
#ifdef运行测试
#包括“Logger.hpp”
#包括“UnitTester.hpp”
#定义所有测试
int main(int,char*[])
{
logger::init();
记录器::设置每个级别(信息);
auto slg=logger::getSLogger();
BOOST_LOG_SEV(slg,INFO)您包含来自函数体内部的文件,我认为它不应该像这样使用
#include
是一个预编译器指令。预编译器甚至在实际编译器启动之前就运行。这使它能够修改代码。就像#define可以替换某些单词一样,#include可以包含整个文件
void testing::UnitTester::logging()
{
#include "Logger.hpp"
}
想象一下,如果您将Logger.hpp的内容替换为include“Logger.hpp”
,编译器将实际收到什么
请不要在函数体中使用
#include
。您会收到一条错误消息,说向量
没有定义在头文件中,您首先在头文件中执行#include
?您确定吗?看起来有些东西您可能没有以某种方式显示给我们。首先,除了#include
之外,您永远不会在任何地方使用>公用事业。CPP < /代码>这没有意义。实际上,C++标准明确地说:“翻译单元应该只在任何外部声明或定义之外包括一个报头”。。虽然标准专门讨论了标准定义的头,但明智的做法是对任何头都这样做,除了一些非常罕见的头,这些头专门设计用于函数定义中。这解决了所有问题。我想我是想太花哨了。
#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)
{
...
}
#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
#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 "ResourceManager\ResourceGroup.hpp"
}
void testing::UnitTester::resourceManager()
{
#include "ResourceManager\ResourceGroup.hpp"
}
void testing::UnitTester::INIParser()
{
#include "INIParser.hpp"
}
#endif
void testing::UnitTester::logging()
{
#include "Logger.hpp"
}