Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Boost测试因名称空间中的枚举类而失败_C++_C++11_Boost_Operator Keyword_Enum Class - Fatal编程技术网

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);
}