C++ Boost测试因名称空间中的枚举类而失败
如果定义C++ Boost测试因名称空间中的枚举类而失败,c++,c++11,boost,operator-keyword,enum-class,C++,C++11,Boost,Operator Keyword,Enum Class,如果定义运算符,如果要使用,则需要在名称空间内定义该运算符 #包括 #定义BOOST\u TEST\u DYN\u链接 #定义BOOST_TEST_模块枚举示例 #包括 名称空间A{ 枚举类示例{ 一,, 二,, }; std::ostream&operatorAha,这就解释了,谢谢!因为我是在.hpp文件中声明函数的,所以我必须把std::ostream&operator@Malvineous无论如何,在定义东西时使用完全限定名是一种很好的做法,如果你把声明搞砸了,那么在编译过程中会出现早期
运算符,如果要使用,则需要在名称空间内定义该运算符
#包括
#定义BOOST\u TEST\u DYN\u链接
#定义BOOST_TEST_模块枚举示例
#包括
名称空间A{
枚举类示例{
一,,
二,,
};
std::ostream&operatorAha,这就解释了,谢谢!因为我是在.hpp
文件中声明函数的,所以我必须把std::ostream&operator@Malvineous无论如何,在定义东西时使用完全限定名是一种很好的做法,如果你把声明搞砸了,那么在编译过程中会出现早期错误,而不是在链接过程中。
#include <iostream>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE EnumExample
#include <boost/test/unit_test.hpp>
namespace A {
enum class Example {
One,
Two,
};
std::ostream& operator<< (std::ostream& s, Example e)
{
switch (e) {
case A::Example::One: s << "Example::One"; break;
case A::Example::Two: s << "Example::Two"; break;
}
return s;
}
} // namespace A
BOOST_AUTO_TEST_CASE(enum_example)
{
A::Example a = A::Example::One;
A::Example b = A::Example::Two;
// The following line works with or without the namespace
std::cout << a << std::endl;
// The following line does not work with the namespace - why?
BOOST_REQUIRE_EQUAL(a, b);
}