user3117112
user3117112

Reputation: 83

C++ Constructor not found

I've got this class defined on DateTime.h

class DateTime {
public:
    int h;
    int m;

    DateTime() {};
    DateTime(string & sf) {
        int i = sf.find(":", 0);
        string _h = sf.substr(0, sf.length() - i-1);
        string _m = sf.substr(i+1, sf.length() - i);

        h = atoi(_h.c_str());
        m = atoi(_m.c_str());
    };
}

Then in other module I include it like this

#include <DateTime.h>

And call the constructor like this

string str("12:13");
DateTime dt(str);

And gives me this error at compiling

src/problem/Reader.cpp: En la función miembro ‘void Reader::readFile(const char*)’:
src/problem/Reader.cpp:44:12: error: no hay coincidencia para la llamada a ‘(DateTime) (std::string&)’
make: *** [build/Reader.o] Error 1

Upvotes: 1

Views: 2358

Answers (2)

sehe
sehe

Reputation: 392999

Likely your string is const. Change the constructor

DateTime(string const& sf) {

// or even
DateTime(string sf) {

Edit It's either that, or indeed different string types. I truly think my analysis makes more sense, statistically.

Upvotes: 1

Miguel
Miguel

Reputation: 924

Your semi-colons are misplaced. The "};" should be the last one for the class. This is making the compiler not see the constructor that takes a string as a parameter.

Change it and try to compile it again and let us know.

Upvotes: 4

Related Questions