简体   繁体   中英

Vector iterator on c++

I have the following:

#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; 
       }
}

I get this error:

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++)

Any suggestions?

On this line

for (it = this->ApplicationVector.begin(); it = this->ApplicationVector.end(); it++)

You are using the assignment equals not testing equality. Replace the test condition with it.= this->ApplicationVector.end()

In the condition of your for loop, you are assigning to it , instead of comparing against the result of end() . You need to do:

for (it = this->ApplicationVector.begin(); 
     it != this->ApplicationVector.end(); it++) {
       if (*it == app)
          break;
}
return it;  // return found iterator, or 'end' if not found. 

Note the != instead of = .

Also, it's better to return outside of the for loop, otherwise the compiler will complain that you might not be returning a value from the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM