简体   繁体   English

c++ 上的向量迭代器

[英]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()it.= this->ApplicationVector.end()

In the condition of your for loop, you are assigning to it , instead of comparing against the result of end() .在 for 循环的条件下,您正在分配it ,而不是与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.此外,最好在 for 循环之外返回,否则编译器会抱怨您可能没有从 function 返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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