-1

i want to work with the return value of std::find but it wont compile.

error: no match for 'operator=' in 'it = __gnu_cxx::operator!= >(((const __gnu_cxx::__normal_iterator >)(& std::find<__gnu_cxx::__normal_iterator >, Dummy>(dummylist.std::vector<_Tp, _Alloc>::begin >(), dummylist.std::vector<_Tp, _Alloc>::end >(), ((const Dummy)(& Dummy(tempArray[0])))))), ((const __gnu_cxx::__normal_iterator >)(& dummylist.std::vector<_Tp, _Alloc>::end ())))'

#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#define SIZEDATASET 1

using namespace std;

class Dummy
{
public:
    Dummy(int Name)
    {
        this->Name = Name;
        this->v0 = 0;
        cout << "Hello, im new " << this->Name << endl;
    }
    ~Dummy()
    {
        cout << "Im done " << this->Name << endl;
    }
    int Name;
    int v0;
};

bool operator== (const Dummy &D0, const Dummy &D1)
{
    return D0.Name == D1.Name;
}


void printClass(vector<Dummy>:: iterator D)
{
    cout << "Name: " << (D)->Name << endl;
    cout << "v0: " << (D)->v0 << endl;
    return;
}

int main()
{
    string str0 = "1;3;2;2;2;4;";
    string str1 = ";";
    string str2;
    int ende = 0;
    int start = 0;
    int length = 0;
    int tempArray[SIZEDATASET] = {0};
    int _switch = 0;
    int i;
    vector<Dummy> dummylist;
    vector<Dummy>:: iterator it;
    Dummy *DummyTemp;


    while((unsigned int)(ende = str0.find(str1,ende))!= std::string::npos)
    {
        length = ende - start;
        str2 = str0.substr(start,length);
        ende+= str1.size();
        start=ende;

        tempArray[_switch]= atoi(str2.c_str());
        _switch++;

        if((_switch%=SIZEDATASET) == 0)
        {
            for(i=0; i<SIZEDATASET; i++)
            {
                cout << tempArray[i] << " ";
            }
            cout << endl;



/*this line*/ if((it = find(dummylist.begin(),dummylist.end(),Dummy(tempArray[0])) != dummylist.end())) /* why u not work?*/
            {
                cout << "match " << endl;
                it->v0++;
            }
            else
            {
                cout << "no match" << endl;
                dummylist.push_back(Dummy(tempArray[0]));
                DummyTemp = &(dummylist.back());
                DummyTemp->v0++;
            }
        }

    }
    it = dummylist.begin();
    while(it != dummylist.end())
    {
        printClass(it);
        it++;
    }
    dummylist.clear();


    return 0;
}

Later the program will receive a string of data(str0) cut it and store it in temporary(tempArray). Then I check(using find) if I already got a set data with the same Name (tempArray[0] represent the name of the dataset). If this is not the case I create a new class with the data and store it in a vector(dummylist). If I found a dataset with the same name in my vector I want to change the values of the existing class in the vector by using the return value of find().

The function find() returns an iterator and "it" is one so I don't know why the error appears.

sorry for my english.

Regards, Dominic

edit: notice that im not struggle with find() itself like in this post.

Community
  • 1
  • 1
Dominic
  • 15
  • 4

1 Answers1

1

Your if statement has got its bracketing wrong: you're trying to assign the result of a comparison operation, instead of comparing the result of an assignment. Change:

if((it = find(dummylist.begin(),dummylist.end(),Dummy(tempArray[0])) != dummylist.end()))

To:

if((it = find(dummylist.begin(),dummylist.end(),Dummy(tempArray[0]))) != dummylist.end())
Smeeheey
  • 9,906
  • 23
  • 39