Search an object with a string or an int

1

I have a list with templates based on an array and what I try to do is to find the position of what I entered, either an integer or a string, but whenever I try, the program tells me that it can not convert an integer or a string to my object, or to the class as such. I have all the operators modified. At the moment I'm interested in comparing them by name, it's about songs

This is how I try to get the position of the string in the array

    List<Song,5000> myLista;//Creación de la lista
    Song s;

    [...]
    }else if (op == "4"){
      string myStr;
      int pos;
      cout << "Ingresa el nombre de la cancion/artista a buscar: ";
      getline(cin, myStr);
      pos = myLista.linearFindData(myStr);
      cout << pos;//Test
    }

This is the method in the class with templates:

    template <class T, int ARRAYSIZE>
    int List<T, ARRAYSIZE>::linearFindData(const T& e) {
      int i(0);
      while( i<=last){
        if(data[i]==e){
          return i;
        }
     i++;
     }
   return -1;
   }

In the Song class I have this:

    [...]
    Song::Song(){}
    Song::Song(const Song& s) : author(s.author), artist(s.artist), name(s.name), rank(s.rank){ }
    [...]
    string Song::toString(){
        char myRank[7];
        sprintf(myRank, "%d", rank);
        return author + "   |   " + artist + "   |   " + name + "   |   " + myRank;
    }

    Song& Song::operator=(const Song& s) {
        author = s.author;
        artist = s.artist;
        name = s.name;
        rank = s.rank;

        return *this;
    }

    ostream &operator << (ostream &o, Song &s){
       o << "Autor: " << s.getAuthor() << endl
         << "Artista: " << s.getArtist() << endl
         << "Nombre: " << s.getName() << endl
         << "Rango: " << s.getRank();

       return o;
    }

    istream& operator >> (istream& is, Song& s){
        getline(is, s.author);
        getline(is, s.artist);
        getline(is, s.name);
        cin>> s.rank;

        return is;
    }

    bool Song::operator==(const Song& s) {
        return name == s.name;
        }

    bool Song::operator!=(const Song& s) {
        return name != s.name;
        }

    bool Song::operator<=(const Song&s) {
        return name <= s.name;
        }

    bool Song::operator>=(const Song&s) {
        return name >= s.name;
        }

    bool Song::operator<(const Song&s) {
        return name < s.name;
        }

    bool Song::operator>(const Song&s) {
        return name > s.name;
        }

This is the error I get:

    
asked by Oscar D 24.02.2018 в 07:36
source

1 answer

0

I managed to solve the problem, when making the call to the search method. Using the created object:

    s.setName(myStr);//Uso el objeto
    pos = myLista.linearFindData(s);//Llamo al método con el objeto
    cout << pos;//Imprimo la posición en la que se encuentra

THANK YOU TO WHOM I HELP TO SOLVE IT !!

    
answered by 24.02.2018 / 18:58
source