为什么候选'='运算符重载被标记为常量?
Why would candidate '=' operator overload be marked as const?
我无法使用 std::set_union
,因为我没有正确重载赋值运算符。
我正在使用我自己的结构 std::set
,NodeChange_t
,它本身包含另一个结构,point_t
。以下是这些人的运算符重载:
// ---- defs.h
struct point_t
{
double x;
double y;
...
void operator=(const point_t &p)
{
x = p.x;
y = p.y;
}
...
};
struct NodeChange_t
{
SNode node;
point_t change;
ListDigraph *graph;
...
void operator=(const NodeChange_t &otherChange)
{
this->node = otherChange.node;
this->change = otherChange.change;
this->graph = otherChange.graph;
}
...
};
// ---- _2DSurface.cpp
//Problematic code:
void _2DSurface::updateInnerSurfaceV2(_2DSurface &outerSurf, std::set<NodeChange_t> *nodeChanges)
{
std::set<NodeChange_t> additions;
...
// add stuff to additions
std::set_union(additions.begin(), additions.end(), nodeChanges->begin(), nodeChanges->end(), nodeChanges->begin());
...
}
在这种情况下,我希望 *nodeChanges
被覆盖。但我不断收到的错误是:
src/_2DSurface.cpp:308:7: note: in instantiation of function template specialization
'std::__1::set_union<std::__1::__tree_const_iterator<ct, std::__1::__tree_node<ct, void *> *, long>,
std::__1::__tree_const_iterator<ct, std::__1::__tree_node<ct, void *> *, long>, std::__1::__tree_const_iterator<ct,
std::__1::__tree_node<ct, void *> *, long> >' requested here
std::set_union(nodeChanges->begin(), nodeChanges->end(), additions.begin(), additions.end(), nodeChanges.begin());
include/defs.hpp:258:7: note: candidate function not viable: 'this' argument has type 'const std::__1::__tree_const_iterator<ct,
std::__1::__tree_node<ct, void *> *, long>::value_type' (aka 'const ct'), but method is not marked const
void operator=(struct ct &otherChange)
如果重点是修改左侧的内容,那么将赋值运算符标记为 const
有什么意义呢?我一直在弄乱 const
限定符,但似乎无处可去。感谢任何帮助。
How does it even make sense that an assignment operator would be marked const
, if the whole point is to modify what's on the left hand side?
未标记赋值运算符const
。事实上,错误消息说明了很多;它是错误的触发因素之一。再看一下错误消息的相关部分,重点强调一下:
candidate function not viable: 'this' argument has type [snipped] (aka 'const ct'), but method is not marked const
错误的另一个触发因素是接收分配的对象被标记为 const
。如果仔细查看错误消息中提到的类型,您可能会注意到“const_iterator
”。这是你的线索!在某个地方,一个常量迭代器被取消引用并为结果分配一个值。涉及到的所有迭代器都是set
个迭代器,所以我们来看看documentation for set
。 set
的 iterator
类型是常量迭代器;你不能写信给它。 (对于 set
,iterator
和 const_iterator
类型通常是同一类型的别名。这种冗余允许与其他容器保持一致的接口。)
set_union
algorithm 需要目标的输出迭代器。 set
没有输出迭代器。所以即使"set"这个词出现在"set_union"中,set_union
也不能直接输出一个set
.
另外一个来自set_union
文档的细节:"The resulting range cannot overlap with either of the input ranges."你不能完成你想要的(用并集替换其中一个输入)与 set_union
一步到位。如果这是您要使用的工具,则需要将联合输出到辅助容器,然后在单独的步骤中更新 nodeChanges
。然而,使用 set::insert
(变体 5)可能会更简单:
nodeChanges->insert(additions.begin(), additions.end());
我无法使用 std::set_union
,因为我没有正确重载赋值运算符。
我正在使用我自己的结构 std::set
,NodeChange_t
,它本身包含另一个结构,point_t
。以下是这些人的运算符重载:
// ---- defs.h
struct point_t
{
double x;
double y;
...
void operator=(const point_t &p)
{
x = p.x;
y = p.y;
}
...
};
struct NodeChange_t
{
SNode node;
point_t change;
ListDigraph *graph;
...
void operator=(const NodeChange_t &otherChange)
{
this->node = otherChange.node;
this->change = otherChange.change;
this->graph = otherChange.graph;
}
...
};
// ---- _2DSurface.cpp
//Problematic code:
void _2DSurface::updateInnerSurfaceV2(_2DSurface &outerSurf, std::set<NodeChange_t> *nodeChanges)
{
std::set<NodeChange_t> additions;
...
// add stuff to additions
std::set_union(additions.begin(), additions.end(), nodeChanges->begin(), nodeChanges->end(), nodeChanges->begin());
...
}
在这种情况下,我希望 *nodeChanges
被覆盖。但我不断收到的错误是:
src/_2DSurface.cpp:308:7: note: in instantiation of function template specialization
'std::__1::set_union<std::__1::__tree_const_iterator<ct, std::__1::__tree_node<ct, void *> *, long>,
std::__1::__tree_const_iterator<ct, std::__1::__tree_node<ct, void *> *, long>, std::__1::__tree_const_iterator<ct,
std::__1::__tree_node<ct, void *> *, long> >' requested here
std::set_union(nodeChanges->begin(), nodeChanges->end(), additions.begin(), additions.end(), nodeChanges.begin());
include/defs.hpp:258:7: note: candidate function not viable: 'this' argument has type 'const std::__1::__tree_const_iterator<ct,
std::__1::__tree_node<ct, void *> *, long>::value_type' (aka 'const ct'), but method is not marked const
void operator=(struct ct &otherChange)
如果重点是修改左侧的内容,那么将赋值运算符标记为 const
有什么意义呢?我一直在弄乱 const
限定符,但似乎无处可去。感谢任何帮助。
How does it even make sense that an assignment operator would be marked
const
, if the whole point is to modify what's on the left hand side?
未标记赋值运算符const
。事实上,错误消息说明了很多;它是错误的触发因素之一。再看一下错误消息的相关部分,重点强调一下:
candidate function not viable: 'this' argument has type [snipped] (aka 'const ct'), but method is not marked const
错误的另一个触发因素是接收分配的对象被标记为 const
。如果仔细查看错误消息中提到的类型,您可能会注意到“const_iterator
”。这是你的线索!在某个地方,一个常量迭代器被取消引用并为结果分配一个值。涉及到的所有迭代器都是set
个迭代器,所以我们来看看documentation for set
。 set
的 iterator
类型是常量迭代器;你不能写信给它。 (对于 set
,iterator
和 const_iterator
类型通常是同一类型的别名。这种冗余允许与其他容器保持一致的接口。)
set_union
algorithm 需要目标的输出迭代器。 set
没有输出迭代器。所以即使"set"这个词出现在"set_union"中,set_union
也不能直接输出一个set
.
另外一个来自set_union
文档的细节:"The resulting range cannot overlap with either of the input ranges."你不能完成你想要的(用并集替换其中一个输入)与 set_union
一步到位。如果这是您要使用的工具,则需要将联合输出到辅助容器,然后在单独的步骤中更新 nodeChanges
。然而,使用 set::insert
(变体 5)可能会更简单:
nodeChanges->insert(additions.begin(), additions.end());