Home > OS >  no matching function for call to CLASS:CLASSCPP(C )
no matching function for call to CLASS:CLASSCPP(C )

Time:04-06

ERROR : Description Resource Path Location Type no matching function for call to 'Saat::Saat()' Ucus.cpp /project9/src line 4 C/C Problem

This my class called Saat

#ifndef SAAT_H_
#define SAAT_H_
#include <string>

class Saat {
public:
    Saat(int, int);
    std::string to_string() const;
private:
    int saat, dakika;
};

#endif /* SAAT_H_ */

#include "Saat.h"

Saat::Saat(int s, int d){
    saat = s;
    dakika = d;
}
std::string Saat::to_string() const{
    return std::to_string(saat)   ":"   std::to_string(dakika);
}

And this is my class called Ucus

#ifndef UCUS_H_
#define UCUS_H_
#include <string>
#include "Saat.h"

class Ucus {
public:
    Ucus(std::string, std::string, std::string, Saat);
    static int get_ucus_sayisi();
    std::string to_string();
private:
    std::string cikisSehir;
    std::string varisSehir;
    std::string ucusNo;
    Saat kalkis_saati;
    static int ucus_sayisi;
};

#endif /* UCUS_H_ */

#include "Ucus.h"

/*ERROR IS HERE */Ucus::Ucus(std::string cs, std::string vs, std::string un, Saat s) { 
    cikisSehir = cs;
    varisSehir = vs;
    ucusNo = un;
    kalkis_saati = s;


}
int Ucus::get_ucus_sayisi(){
    return ucus_sayisi;

}
std::string Ucus::to_string(){
    return cikisSehir   varisSehir   ucusNo   kalkis_saati.to_string();
}

I am watching a video about classes and i want to use my Saat class in Ucus class. I am doing the same things in video but it gives me this error: ***( Description Resource Path Location Type no matching function for call to 'Saat::Saat()' Ucus.cpp /project9/src line 4 C/C Problem )


There is nothing different but not works for me.

CodePudding user response:

In a class, members are created before the body of the constructor is entered. Your code is attempting to default construct a Saat, and then overwrite it in the body through assignment.

Because your Saat class does not have a default constructor, your code doesn't compile. WHY doesn't your class have a default constructor? The compiler can generate one for you if you don't provide any constructors, but once you write one, the default constructor is no longer provided without request. But if this compiled you'd have been slightly worse off. It's nice to catch a mistake instead of silently living with it.

The proper way to do this is to NOT default construct your object, but to copy-construct it, and other members, in the constructors member initializer list:

Ucus::Ucus(std::string cs, std::string vs, std::string un, Saat s)
  cikisSehir{cs},
  varisSehir{vs},
  ucusNo{un},
  kalkis_saati{s},
{ 
}

It's more efficient too. No reason to create an object, and then overwrite it via assignment. Better to create it directly by a copy, which is a constructor you'll get generated automatically (unless you have a member that is not copyable, but in your case you don't.)

Of course, you really should consider passing in your strings as std::string const & instead, to avoid needless copies.

  • Related