English 中文(简体)
Weird“candidate”在构造中预计有1个论点,0个是“提供的”。
原标题:Weird "candidate expects 1 argument, 0 provided" in constructor

I m making a simple threaded server application in C++, thing is, I use libconfig++ to parse my configuration files. Well, libconfig doesn t support multithreading, thus I m using two wrapper classes in order to accomplish "support". Point is, one of them fails:

class app_config {
    friend class app_config_lock;
public:
    app_config(char *file) :
        cfg(new libconfig::Config()),
        mutex(new boost::mutex())
    {
        cfg->readFile(file);
    }

private:
    boost::shared_ptr<libconfig::Config> cfg;
    boost::shared_ptr<boost::mutex> mutex;
};

Fails horribly when called from my main.cpp file:

app_main::app_main(int c, char **v) : argc(c), argv(v) {
    // here need code to parse arguments and pass configuration file!.
    try {
        config = app_config("mscs.cfg");
    } catch (libconfig::ParseException &e) {
        cout << "Parse error at line " << e.getLine() << ": " << e.getError() << endl;
        throw;
    } catch (libconfig::FileIOException &e) {
        cout << "Configuration file not found." << endl;
        throw;
    }
}

And it says:

main.cpp: In constructor ‘app_main::app_main(int, char**)’:
main.cpp:38:54: error: no matching function for call to ‘app_config::app_config()’
main.cpp:38:54: note: candidates are:
../include/app_config.h:15:5: note: app_config::app_config(char*)
../include/app_config.h:15:5: note:   candidate expects 1 argument, 0 provided
../include/app_config.h:12:7: note: app_config::app_config(const app_config&)
../include/app_config.h:12:7: note:   candidate expects 1 argument, 0 provided
main.cpp:41:39: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] (THIS CAN BE IGNORED, I WAS USING STD::STRING, YET CHANGED IT FOR TESTING PURPOSES)

之所以如此,是因为我显然通过了一个论点,而且它又通过了一个<条码>,*!

一如既往,任何帮助都将受到赞赏。

Julian。

最佳回答

你试图不实地构造你们的政客,然后派人到会。 但是,你没有违约建筑商。

向成员变量的构造者提出论据的正确办法是:

app_main::app_main(int c, char **v) : argc(c), argv(v), config("mscs.cfg")

你们仍然可以通过使用所谓的碎机/m>来割裂这一例外情况。 http://www.gotw.ca/gotw/066.htm

最后法典:

app_main::app_main(int c, char **v)
try : argc(c), argv(v), config("mscs.cfg")
{
    // more constructor logic here
} catch (libconfig::ParseException &e) {
    cout << "Parse error at line " << e.getLine() << ": " << e.getError() << endl;
    throw;
} catch (libconfig::FileIOException &e) {
    cout << "Configuration file not found." << endl;
    throw;
}
问题回答

首先,不以动态方式分配货币,它没有用途。 第二,这是因为你有一个数据成员,无法进行违约整理,而且你在博士名册上也做了初步分析。 此外,如果你真的想要与点子公司站住脚,则永远不会将字面直拨到char*变量上(如果你真的想与点子公司站起来的话,应当打上app_config(const char*))。

页: 1 反之:

app_main::app_main(int c, char **v) try
    : argc(c), argv(v), config("mscs.cfg") {
} catch (libconfig::ParseException &e) {
    cout << "Parse error at line " << e.getLine() << ": " << e.getError() << endl;
    throw;
} catch (libconfig::FileIOException &e) {
    cout << "Configuration file not found." << endl;
    throw;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?