p. bosch
p. bosch

Reputation: 139

operator>> error: no matching function for call to

I'm working with a class Allotjament, that has 5 attributes, 4 of them are from a class called Cadena, which I have finished too, here's is Allotjament.cpp:

#include <iostream>
#include <cstring>
#include "Allotjament.h"

    Allotjament::Allotjament() {
        _nom='\0';
        _pais='\0';
        _localitat='\0';
        _categoria='\0';
        _preu=0;
    }
    Allotjament::Allotjament(Cadena nom, Cadena pais, Cadena localitat, Cadena categoria, float preu) {
        _nom=nom;
        _pais=pais;
        _localitat=localitat;
        _categoria=categoria;
        _preu=preu;
    }

    Cadena Allotjament::getNom() const { return _nom; }
    Cadena Allotjament::getPais() const { return _pais; }
    Cadena Allotjament::getLocalitat() const { return _localitat; }
    Cadena Allotjament::getCategoria() const { return _categoria; }
    float Allotjament::getPreu() const { return _preu; }

    ostream& operator<<(ostream &o, Allotjament a) {
        o << "Nom:" << a._nom << endl;
        o << "Pais:" << a._pais << endl;
        o << "Localitat:" << a._localitat << endl;
        o << "Categoria:" << a._categoria << endl;
        o << "Preu:" << a._preu << endl;
        return o;
    }

    istream& operator>>(istream &i, Allotjament &a){
        getline(i,a._nom);
        getline(i,a._pais);
        getline(i,a._localitat);
        getline(i,a._categoria);
        i >> a._preu;
        cin.ignore();

        return i;
    }

Allotjament.h

#include <iostream>
#ifndef ALLOTJAMENT_H
#define ALLOTJAMENT_H
#include "Cadena.h"

class Allotjament{
    public:
        Allotjament();                    // Constructor de la classe
        Allotjament(Cadena nom, Cadena pais, Cadena localitat, Cadena categoria, float preu);   // Constructor amb parametres
        Cadena getNom() const;            // Retorna el nom de l'Allotjament
        Cadena getPais() const;           // Retorna el pais de l'Allotjament
        Cadena getLocalitat() const;      // Retorna la localitat de l'Allotjament
        Cadena getCategoria() const;      // Retorna la categoria de l'Allotjament
        float getPreu() const;            // Retorna el preu de l'Allotjament

        friend ostream& operator<<(ostream&, Allotjament);
        friend istream& operator>>(istream&, Allotjament&);

    private:
        Cadena _nom;
        Cadena _pais;
        Cadena _localitat;
        Cadena _categoria;
        float _preu;
};

And here I have the operator>> for the class Cadena:

istream& operator>>(istream& i, Cadena& s) {
  delete [] s.c;
  const int l = 256;
  char *t = new char[l];
  i.getline(t,l);
  s.c = t;
  return i;
}

My problem is the following: The compiler g++ tells me I do 4 errors, one in each line of the getline in the operator>> in the class Allotjament. Here's the error:

error: no matching function for call to 'getline(std::istream&, Cadena&)

I have tested the class Cadena and its operators and they work as intended, but don't understand why I'm getting this error. Any ideas?

Upvotes: 0

Views: 564

Answers (3)

jrd1
jrd1

Reputation: 10716

You haven't included <string>.

std::getline is defined in there.

UPDATE:

And, as others have pointed out: std::getline() requires that the second parameter be a string.

SEE HERE:

http://en.cppreference.com/w/cpp/string/basic_string/getline

Upvotes: 0

masoud
masoud

Reputation: 56479

You must pass a std::string to std::getline not a Allotjament.

Upvotes: 1

Ashalynd
Ashalynd

Reputation: 12563

You have already defined an operator >> for Cadena class, so just use it:

  istream& operator>>(istream &i, Allotjament &a){
        i >> a._nom;
        i >> a._pais;
        i >> a._localitat;
        i >> a._categoria;
        i >> a._preu;
        cin.ignore();

        return i;
    }

Upvotes: 1

Related Questions