簡體   English   中英

迭代器不匹配運算符=錯誤

[英]Iterator no match operator= Error

我有 2 個類,即 A 類和 B 類。 在 A 類中有一個 map 在堆 memory 上動態聲明。 然而,在 B 類中,正在嘗試使用迭代器訪問 A 類 map 值。 不幸的是,我得到了迭代器的錯誤 no match operator= 。 如果我將 map 移動到 B 類,迭代器將正常工作。 有人可以幫助我嗎,這一直困擾着我一段時間。 提前致謝。

class classA{
public:
  classA();
friend classB;
private:
  map <int,int>* _themap;
};

classA::classA(){
  _themap = new map<int,int>;
}

class classB{
private:
 classA* object = new classA();
 void accessthemap();
};

void classB::accessthemap(){

 map<int,int>::iterator it;
 it = object->_themap->begin();
 it = object->_themap->find();
}

它應該是

it = object->_themap.begin(); //not _themap->begin()

因為_themap是一個非指針,所以你必須使用. 運算符,而不是->運算符。

此外,還有一些錯誤。 如果您將classA寫為

//incorrect
classA{
   //...
};

應該是

//correct
class classA{
   //...
};

也就是說,您必須在class-name之前使用關鍵字class 因此,使用關鍵字class classB

您不能在 class 定義中定義成員,所以這是錯誤的:

classB{
private:
 classA* object = new classA();
 void accessthemap();
};

相反,只需使用普通的 object (更不用說修復您的其他語法錯誤):

class classB {
private:
 classA object;
 void accessthemap();
};

這里不需要動態分配。

然后寫object._themap.begin(); .

暫無
暫無

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

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