Cannot call constructor

I have to make a timer which displays an hour and minutes, but I get this compilation error.

In member function ‘std::string Timer::toString() const’: timer.h:43:17: error: cannot call constructor ‘Timer::Timer’ directly [-fpermissive]

i don't understand why this happens or what it means. Please help. I think it has something to do with the toString function but I don't know for sure

#ifndef TIMER_H_
#define TIMER_H_

#include <string>
#include <sstream>



class Timer {
private:
    int horas;
    int minutos;

public:
    Timer();
    Timer(int, int);
    Timer(const Timer&);

    int getHoras() const;
    int getMinutos() const;

    void operator =(const Timer&);
    void operator +=(const Timer&);
    std::string toString() const;



};

 std::string Timer::toString() const {
     std::stringstream aux;

 Timer :: Timer() : horas(0), minutos(0) {}

 Timer :: Timer(int hh, int mm) {
     if(mm > 60){
         horas = hh + (mm/60);
     }else {
         horas = hh;
         minutos = mm;
     }
 }

 Timer :: Timer(const Timer  &source) : horas(source.horas), minutos(source.minutos) {}

 int Timer :: getHoras() const {
     return horas;
 }

 int Timer :: getMinutos() const {
     return minutos;
 }

 void Timer :: operator =(const Timer &source) {
     horas = source.horas;
     minutos = source.minutos;
 }

 void Timer :: operator +=(const Timer &source) {
     if((source.minutos+minutos) > 60){
         horas = horas + 1;
         minutos = (minutos + source.minutos) % 60;
     }else {
         minutos = minutos + source.minutos;
     }
     horas = (horas + source.horas)%24;
 }

 bool operator ==(const Timer &izq, const Timer &der) {
     return ( (izq.getHoras() == der.getHoras())&&(izq.getMinutos() == der.getMinutos() );
 }

 bool operator >(const Timer &izq, const Timer &der) {

     if(izq.getHoras() > der.getHoras()){
         return true;
     }else if(izq.getHoras() == der.getHoras()){
         if(izq.getMinutos() > der.getMinutos()){
                return true;
         }
     }

     return false;
 }
 #endif

Upvotes: 0

Views: 1125

Answers (1)

Mateo Torres-Ruiz
Mateo Torres-Ruiz

Reputation: 111

You're missing a } in a function definition. Also, if the type of the function is declared as string. Then you must return a string.

std::string Timer::toString() const {
    std::stringstream aux;
    std::string youMustReturnAString;
    return youMustReturnAString;
}

You're also missing a ) at the end in line 77:

bool operator ==(const Timer &izq, const Timer &der) {
        return ((izq.getHoras() == der.getHoras()) && (izq.getMinutos() == der.getMinutos()));
}

Upvotes: 2

Related Questions