Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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++_Vector_Stl_Operator Overloading - Fatal编程技术网

C++ 与‘不匹配;运算符=’;对向量使用迭代器

C++ 与‘不匹配;运算符=’;对向量使用迭代器,c++,vector,stl,operator-overloading,C++,Vector,Stl,Operator Overloading,我正在开发一个计时器类,它使用一个通用树来存储不同的进程计时。这个结构有字符串和双变量来存储一个标记和这个“标记”花费的时间(每个标记对应于一些实际过程)。定义如下: struct TimerNode { double time_value; std::string name; std::vector<TimerNode*> children; TimerNode(){}; TimerNode(const std::string name):

我正在开发一个计时器类,它使用一个通用树来存储不同的进程计时。这个结构有字符串和双变量来存储一个标记和这个“标记”花费的时间(每个标记对应于一些实际过程)。定义如下:

struct TimerNode
{
    double time_value;
    std::string name;
    std::vector<TimerNode*> children;

    TimerNode(){};
    TimerNode(const std::string name): name(name){}
    TimerNode& operator=(const TimerNode& other){//CopyAssignable
        this->time_value=other.time_value; this->name=other.name;
        return *this;}
    TimerNode(const TimerNode& other){//CopyConstructible 
        this->time_value=other.time_value; this->name=other.name;}
}tree_;
struct TimerNode
{
双倍时间u值;
std::字符串名;
性病媒儿童;
TimerNode(){};
TimerNode(const std::string name):name(name){}
TimerNode和运算符=(常量TimerNode和其他){//CopyAssignable
this->time\u value=other.time\u value;this->name=other.name;
返回*this;}
TimerNode(常量TimerNode和其他){//CopyConstructible
this->time\u value=other.time\u value;this->name=other.name;}
}树木;
此结构位于timer类中,该类将使用它来维护每个进程使用的时间记录。它还有一个名为open_register的函数,该函数将在每次调用计时器时调用,并接收带有用户决定的标记名的字符串(标记不同的进程)。然后,此函数将检查此标记是否已被使用,以便将时间汇总到该进程以前使用的时间。所以,它必须检查结构可能必须检查的每个子项,我使用迭代器,因为我将在std::vector子项中搜索。所以

TimerNode* actual_timer = &open_timers_.back();   //open_timers is a stack
std::vector<TimerNode>::iterator it;

for(*it=actual_timer->children.begin(); it != actual_timer->children.end(); ++it){
    if(it->name == received_name){//we found it.
        /*At this point we need to put the node associated with this
        * into the stack and start counting because it is a child
        * that was used before but stopped. So we will add the time
        * expended this time to the value it already has.*/
        open_timers_.push_back(*it);
        break;
    }
}
TimerNode*actual_timer=&open_timers_u.back()//开放计时器是一个堆栈
std::vector::it迭代器;
对于(*it=actual\u timer->children.begin();it!=actual\u timer->children.end();++it){
如果(it->name==received_name){//我们找到了它。
/*此时,我们需要放置与此关联的节点
*进入堆栈并开始计数,因为它是一个子对象
*之前使用过,但已停止。因此,我们将添加时间
*将这段时间消耗到它已经拥有的价值*/
打开计时器。向后推(*它);
打破
}
}
然而,在编译g++时,for行中抱怨说

../src/include/timer.h:73:46:错误:它中的“operator=”不匹配。\uu gnu\u cxx:::\uu normal\u迭代器::operator*with u iterator=const opice::Global\u timer::TimerNode*,\u Container=std::vector,通用迭代器::reference=const opice::Global\u Timer::TimerNode&=actual\u Timer->opice::Global\u Timer::TimerNode::children.std::vector::以通用定时器::TimerNode*开始,Alloc=std::分配器,std::vector::迭代器=\uu gnu\u cxx::通用迭代器>,typename std::_Vector_base::_Tp_alloc_type::pointer=opice::Global_Timer::TimerNode**

我一直在寻找解决方案,我发现了一些类似的问题,这似乎与我遇到的问题几乎相同,但没有解决,因为我犯了同样的错误

我还看到了一些与匹配运算符相关的其他问题,但它们似乎与我的问题不太相似。你能指出我在哪里犯了错误,或者我在这里遗漏了什么吗?我想这与重载操作符有关,但我不知道如何在我的结构中重载=操作符,以便用向量迭代器解决这个问题。非常感谢。

删除星号:

for(it=actual_timer->children.begin(); it != actual_timer->children.end(); ++it) {
*it=actual\u timer->children.begin()
尝试将
actual\u timer->children.begin()
分配给
it
指向的元素。
it
指向的元素本身不是迭代器,因此编译错误。此外,
它当时未初始化,因此即使编译成功,访问它所指向的元素也会调用未定义的行为。

删除星号:

for(it=actual_timer->children.begin(); it != actual_timer->children.end(); ++it) {

*it=actual\u timer->children.begin()
尝试将
actual\u timer->children.begin()
分配给
it
指向的元素。
it
指向的元素本身不是迭代器,因此编译错误。而且,
it
当时没有初始化,因此即使编译成功,访问它所指向的元素也会调用未定义的行为。

您将取消引用操作符错误地放在了
it
前面;要分配给迭代器,只需使用它的变量,而不是它指向的元素

vector<int> v { 1, 2, 3 };

auto it = v.begin(); 
cout << *it; // prints 1

it = v.begin() + 1;
cout << *it; // prints 2;

*it = 3;
cout << *it; // prints 3; v contains [1,3,3] now
向量v{1,2,3};
自动it=v.begin();

您是否将解引用运算符放错了它前面的位置;要分配给迭代器,只需使用它的变量,而不是它指向的元素

vector<int> v { 1, 2, 3 };

auto it = v.begin(); 
cout << *it; // prints 1

it = v.begin() + 1;
cout << *it; // prints 2;

*it = 3;
cout << *it; // prints 3; v contains [1,3,3] now
向量v{1,2,3};
自动it=v.begin();

cout问题出在您编写的
for
语句中

*it=actual_timer->children.begin()
而不是

it=actual_timer->children.begin()
出现编译错误是因为
*它
意味着“取消对名为
it
的迭代器的引用”,这将为您提供对
TimerNode
的引用。希望没有定义
TimerNode::operator=(const std::vector::iterator&)
,因此您会得到错误


此外,您正在取消对未初始化指针的引用,因此,如果您编写
*it=SomeTimerNode
,则不会出现编译错误,但会被抛出未定义的行为区。

问题在于您编写的
for
语句中

*it=actual_timer->children.begin()
而不是

it=actual_timer->children.begin()
出现编译错误是因为
*它
意味着“取消对名为
it
的迭代器的引用”,这将为您提供对
TimerNode
的引用。希望没有定义
TimerNode::operator=(const std::vector::iterator&)
,因此您会得到错误


此外,您正在取消对未初始化指针的引用,因此如果您编写了
*it=SomeTimerNode
,则不会出现编译错误,但会被抛出未定义的行为区。

顺便说一句,我想知道的不仅仅是一个解决方案,而是对发生此错误的原因的解释。非常感谢。应该是it=actual\u timer->children.begin()(不带尾随符*)。这是因为*它会尝试将…begin()赋值给迭代器指向的变量