簡體   English   中英

使用向量迭代器的'operator ='不匹配

[英]no match for ‘operator=’ using iterator for a vector

我正在開發一個Timer類,該類使用通用樹來存儲不同的處理時間。 該結構具有字符串和雙精度變量,用於存儲標簽以及該“標簽”所花費的時間(每個標簽對應於某些實際過程)。 這是定義:

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_;

該結構在計時器類中,它將使用它來維護每個過程所用時間的記錄。 它還具有一個稱為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;
    }
}

但是,在編譯g ++時,在for行中抱怨說

../src/include/timer.h:73:46:錯誤:'it .__ gnu_cxx :: __ normal_iterator <_Iterator,_Container> :: operator *與_Iterator = const opice :: Global_Timer不匹配'operator =': :TimerNode *,_Container = std :: vector,__gnu_cxx :: ____ normal_iterator <_Iterator,_Container> :: reference = const opice :: Global_Timer :: TimerNode&= actual_timer-> opice :: Global_Timer :: TimerNode :: children.std ::: vector <_Tp,_Alloc> ::以_Tp = opice :: Global_Timer :: TimerNode *,_Alloc = std :: allocator,std :: vector <_Tp,_Alloc> :: iterator = __gnu_cxx :: __ normal_iterator>,類型名稱std: :_Vector_base <_Tp,_Alloc> :: _ Tp_alloc_type :: pointer = opice :: Global_Timer :: TimerNode **'

我一直在尋找解決辦法,我發現有些人喜歡這樣 ,這似乎差不多我也有同樣的問題,但它並沒有解決這個問題,因為我有同樣的錯誤。

我還看到了一些其他與匹配運算符有關的問題,但它們似乎與我的問題不太相似。 您能指出我在哪里犯錯或在這里錯過了什么嗎? 我猜想它與重載運算符有關,但是我無法弄清楚如何在結構中重載=運算符以解決矢量迭代器的問題。 非常感謝你。

刪除星號:

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

*it = actual_timer->children.begin()嘗試分配actual_timer->children.begin()到元素指向it it指向的元素本身不是迭代器,因此不是編譯錯誤。 此外, it還沒有初始化,因此即使編譯成功,訪問它所指向的元素也會調用未定義的行為。

問題出在您的for語句中,您在哪里寫

*it=actual_timer->children.begin()

代替

it=actual_timer->children.begin()

您會收到一個編譯錯誤,因為*it意思是“取消引用名為it的迭代器”,從而為您提供對TimerNode的引用。 由於希望沒有TimerNode::operator=(const std::vector<TimerNode>::iterator&) ,因此會出現錯誤。

另外,您正在取消引用未初始化的指針,因此,如果您編寫*it = SomeTimerNode ,則不會有編譯錯誤,但是您將被拋出未定義的行為域。

您將解引用運算符放到了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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM