0

I have this class:

class FileRatio{
public:
    double x;
    double y;

    FileRatio(double xx,double yy){
        this->x = xx;
        this->y = yy;
    }
};

When I choose "build", XCode is able to build it. When I choose "run", then I get a "build failed" message:

No matching constructor for initialisation of FileRatio

What is the problem ?

EDIT: I try to use it in the following ways:

std::unordered_map<std::string, FileRatio>fileRatioMap;
std::vector<std::string> tokens = split(line, ',');

FileRatio fileRatio = FileRatio(stod(tokens[1]),stod(tokens[2]));
fileRatioMap[tokens[0]] = fileRatio;

And later I do:

 FileRatio ratio = fileRatioMap[name];

EDIT: The compiler does not highlight a specific line in MY code. Instead, it appears in some class called "memory" which is part of Xcode's toolchain.

EDIT:

The whole error message is:

No matching constructor for initialisation of FileRatio

The line that is highlighted is:

construct(_Up* __p, _Args&&... __args)
{
  ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
}
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • 2
    How are you trying to use it? – BoBTFish Feb 09 '16 at 10:14
  • 1
    @BobTFish I updated the question – Rahul Iyer Feb 09 '16 at 10:18
  • Thanks. At a quick glance, your usage is reasonable. Could you please include the whole error message, showing where it claims there is an error (in `memory`, whatever that is). – BoBTFish Feb 09 '16 at 10:24
  • 2
    See [this question](http://stackoverflow.com/questions/14164223/why-is-a-default-constructor-required-when-storing-in-a-map). In short, you probably need a default constructor... – dandan78 Feb 09 '16 at 10:25
  • @dandan78 +1. Also to add to that comment. It might be of use to use pointers instead, that way you don't need a default constructor and can detect if no such entry exists by checking if it returns null. – Neijwiert Feb 09 '16 at 10:32
  • @Neijwiert can you explain where should I use pointers ? It doesn't seem obvious to me.. – Rahul Iyer Feb 09 '16 at 10:34
  • `std::unordered_map fileRatioMap;`. Use `new` and `delete` operator to insert/get/remove entries. – Neijwiert Feb 09 '16 at 10:36
  • Even better would be to avoid using `[]` operator altogether and use `unordered_map::find` instead. Also I would recommend implementing the move contructor and operator. Would avoid unnecessary copies. Altough no heavy-weight allocating is going on, it is a good practice in c++11 – Neijwiert Feb 09 '16 at 10:40

0 Answers0