Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.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++ 这个向量比较有什么问题?_C++ - Fatal编程技术网

C++ 这个向量比较有什么问题?

C++ 这个向量比较有什么问题?,c++,C++,我试图将方法之前的向量与方法之后的相同向量进行比较,以断言它们是相同的向量,但当我运行测试时,我得到以下错误: [build] C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\include\xutility(5044,1): error C2893: Failed to specialize function template 'unknown-type std::

我试图将方法之前的向量与方法之后的相同向量进行比较,以断言它们是相同的向量,但当我运行测试时,我得到以下错误:

[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\include\xutility(5044,1): error C2893: Failed to specialize function template 'unknown-type std::equal_to<void>::operator ()(_Ty1 &&,_Ty2 &&) noexcept(<expr>) const' [C:\Users\james\Documents\BSc Computer Science\4007CEM - Activity Led Learning\FantasyRPG\build\tests\test_containers.vcxproj]
[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\include\xstddef(198): message : see declaration of 'std::equal_to<void>::operator ()' [C:\Users\james\Documents\BSc Computer Science\4007CEM - Activity Led Learning\FantasyRPG\build\tests\test_containers.vcxproj]
[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\include\xutility(5044,1): message : With the following template arguments: [C:\Users\james\Documents\BSc Computer Science\4007CEM - Activity Led Learning\FantasyRPG\build\tests\test_containers.vcxproj]
[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\include\xutility(5044,1): message : '_Ty1=const Item &' [C:\Users\james\Documents\BSc Computer Science\4007CEM - Activity Led Learning\FantasyRPG\build\tests\test_containers.vcxproj]
[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29333\include\xutility(5044,1): message : '_Ty2=const Item &' [C:\Users\james\Documents\BSc Computer Science\4007CEM - Activity Led Learning\FantasyRPG\build\tests\test_containers.vcxproj]
[build] Build finished with exit code 1
方法
get_contents
定义为
std::vector get_contents(){return contents;}
,应该返回类型为
Item
的对象向量。 在更大的上下文中,这是围绕错误行的代码块:

WHEN("I try to add something which is not an Item.") {
    std::vector<Item> contentsBefore = merchant.get_contents();
    merchant.addItem(helmet);
    THEN("The container should not be updated to add the item.") {
        REQUIRE(contentsBefore == merchant.get_contents());
    }
}

但这并没有解决问题,所以我真的不明白问题是什么
contents
是一个私有属性,但我不认为这是问题所在,因为
get\u contents
方法在定义
contents之前的行中工作正常…

错误消息表明您的对象项未实现运算符==

模板
结构等于//(直到C++14)
模板结构等于//(从C++14开始)

用于执行比较的函数对象。除非专门指定,否则在类型T上调用运算符==。

错误消息表明您的对象项未实现运算符==

模板
结构等于//(直到C++14)
模板结构等于//(从C++14开始)

用于执行比较的函数对象。除非专业化,否则在类型T上调用运算符==。

什么是
要求的
?可能您只需要使
get_contents
成为常量成员函数。。。。当
和之后也不熟悉。REQUIRE/THEN/WHEN是Catch2测试中的BDD语法,但这不是我不认为的问题,因为我有六个其他测试使用它,它们工作得很好什么是
REQUIRE
?可能您只需要使
get_contents
成为常量成员函数。。。。当和时,那么也不熟悉。REQUIRE/THEN/WHEN是Catch2测试中的BDD语法,但这不是我不认为的问题,因为我有六个其他测试使用它,它们工作得很好,但我正在比较向量,不是我自己类中的对象如果不比较两个向量中的元素,你就不能比较两个向量。那么这和
vector vec={1}有什么区别;向量vec2={1};如果(vec==vec2){std::cout@askman如果您想以某种方式比较向量,而不比较包含在这些向量中的类的对象,您必须自己重载
=
操作符。@askman
std::vector
是整数的向量。您可以直接比较整数,因为
操作符==
是为它们定义的。您需要o为
Item
定义
operator=
,以便比较两个
Item
s或
Item
向量。但是我在比较向量,而不是我自己类的对象。如果不比较两个向量中的元素,就不能比较它们。那么,这与
vector vec={1};vector vec2={1}之间有什么区别;如果(vec==vec2){std::cout@askman如果您想以某种方式比较向量,而不比较包含在这些向量中的类的对象,您必须自己重载
=
操作符。@askman
std::vector
是整数的向量。您可以直接比较整数,因为
操作符==
是为它们定义的。您需要o为
项目
定义
运算符==
,以便比较两个
项目
项目
向量。
WHEN("I try to add something which is not an Item.") {
    std::vector<Item> contentsBefore = merchant.get_contents();
    merchant.addItem(helmet);
    THEN("The container should not be updated to add the item.") {
        REQUIRE(contentsBefore == merchant.get_contents());
    }
}
WHEN("I try to add something which is not an Item.") {
    std::vector<Item> contentsBefore = merchant.get_contents();
    merchant.addItem(helmet);
    std::vector<Item> contentsAfter = merchant.get_contents();
    THEN("The container should not be updated to add the item.") {
        REQUIRE(contentsBefore == contentsAfter);
    }
}
template< class T >
struct equal_to; //(until C++14)
template< class T = void > struct equal_to; //(since C++14)