[英]Vector iterator on c++
我有以下內容:
#ifndef APPSYSTEM_H
#define APPSYSTEM_H
#include "Application.h"
#include <iostream>
#include <exception>
#include <vector>
#include <iterator>
using namespace std;
class AppSystem{
private:
vector<Application> &ApplicationVector;
public:
AppSystem(); //AppSystem Constructor
AppSystem(const AppSystem &); //Copy constructor
void setApplicationVector(vector<Application> &); //Set the AppSystem's Application Vector
vector<Application> getApplicationVector(); //Get the AppSystem's Application Vector
void PushAppToApplicationVector(Application &) const; //Push Data to ApplicationVector
Application &PopAppFromApplicationVector(Application &) const; //Pop Data from ApplicationVector
vector<Application>::iterator FindAppToApplicationVector(Application &) const; //Find if Data belongs to ApplicationVector
void DeleteAppFromApplicationVector(Application &); //Delete Data from ApplicationVector
void ClearAllpicationVector(); //Clear all data from ApplicationVector
virtual ~AppSystem(); //Destructor
};
#endif /* APPSYSTEM_H */
// APPSYSTEM.cpp file
//Find if Data belongs to ApplicationVector
vector<Application>::iterator AppSystem::FindAppToApplicationVector(Application &app) const{
vector<Application>::iterator it;
for (it = this->ApplicationVector.begin(); it = this->ApplicationVector.end(); it++){
if (*it == app){
return it;
}
}
我收到此錯誤:
AppSystem.cpp:56:51: error: could not convert '(it = (&((const AppSystem*)this)->AppSystem::ApplicationVector)->std::vector<_Tp, _Alloc>::end<Application, std::allocator<Application> >())' from 'std::vector<Application>::iterator {aka __gnu_cxx::__normal_iterator<Application*, std::vector<Application> >}' to 'bool'
for (it = this->ApplicationVector.begin(); it = this->ApplicationVector.end(); it++)
有什么建議么?
在這條線上
for (it = this->ApplicationVector.begin(); it = this->ApplicationVector.end(); it++)
您正在使用分配等於不測試相等性。 用it.= this->ApplicationVector.end()
在 for 循環的條件下,您正在分配it
,而不是與end()
的結果進行比較。 你需要做:
for (it = this->ApplicationVector.begin();
it != this->ApplicationVector.end(); it++) {
if (*it == app)
break;
}
return it; // return found iterator, or 'end' if not found.
注意!=
而不是=
。
此外,最好在 for 循環之外返回,否則編譯器會抱怨您可能沒有從 function 返回值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.