Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching constructor for initialization of

Tags:

c++

I’ve seen similar questions on StackOverflow, but none of them seems to apply to me.

Here is my code:


Option.cpp

#include "Option.h"

Option::Option(string valueName, string description, OptionType type){
    this->valueName = valueName;
    this->description = description;
    this->type = type;
};


Option.h

#include <iostream>
using namespace std;

enum OptionType { FLAG, REQUIRED, NORMAL };
class Option {
    string valueName, description, value;
    OptionType type;
public:
    Option(string valueName, string description, OptionType type);
    void setValue(string value) {
        this->value = value;
    };
    string getValueName() {
        return this->valueName;
    };
    string getDescription() {
        return this->description;
    };
    OptionType getType() {
        return this->type;
    };
};


Options.cpp

#include "Options.h"
using namespace std;

Options::Options(int _argc, const char * _argv[]) : argv(_argv) {
    this->argc = _argc;
}

Options::~Options() {
    options.~unordered_map();
}

void Options::printHelp() {
    for (auto &i : options) {
        cout << i.first << '\t' << i.second.getDescription() << '\n';
    }
}

void Options::addFlag(string flagName, string description) {

}

void Options::addOption(string optionName, string valueName, string description, OptionType type) {
    Option option(valueName, description, type);
    options[optionName]=option;
}

void Options::addOptionAlias(string aliasName, string optionName) {

}


Options.h

#include <iostream>
#include <unordered_map>
#include "Option.h"

using namespace std;

class Options {
    unordered_map<string, Option> options;
    int argc;
    const char ** argv;

public:
    Options(int argc, const char * argv[]);
    ~Options();
    void parse();
    void addOption(string optionName, string valueName, string description, OptionType type);
    void addFlag(string flagName, string description);
    void addOptionAlias(string aliasName, string optionName);
    void getOption(string optionName);
    void printHelp();


};

It's in options.cpp on the line Option option(valueName, description, type); that the error seems to stem from, but for the life of me, I can’t figure out why. As far as I can see, the constructor in Option takes the right types.

like image 789
SomeNorwegianGuy Avatar asked Dec 18 '13 20:12

SomeNorwegianGuy


2 Answers

The problem is actually in the next line:

options[optionName]=option;

That first calls the operator[] in the map, that searchs for the given key and returns the associated value. If the key is not found, it insert a default initialized value connected to that key. Then this value is copy assigned with your option.

Do you see the problem? Your Option class does not have a default constructor, so it cannot be default initialized! Read carefully your error message, surely it is talking about the default constructor, not the one you are looking at.

You have several solutions. The easiest would be to write a default constructor for your class.

The alternative would be never to use operator[] in the map so that the default constructor is never needed. If that's what you want to do, to insert an item you write:

options.insert(std::make_pair(optionName, option));

Finally, if you are using C++11 (or later) and a compliant enough compiler, you can even build the object directly into the container: zero copy overhead and you don't even need the copy constructor!

options.emplace(std::piecewise_construct,
         std::forward_as_tuple(optionName),
         std::forward_as_tuple(valueName, description, type));
like image 85
rodrigo Avatar answered Nov 04 '22 19:11

rodrigo


There's a mismatch between the declaration of the constructor in the header and the definition in the source file.

In header...

Option(string& valueName, string& description, OptionType& type);

In source file...

Option::Option(string valueName, string description, OptionType type){

Notice the parameters are defined as references (e.g., string&) in the header, but as objects (e.g., string) in the source.

like image 29
Matt Davis Avatar answered Nov 04 '22 20:11

Matt Davis