奇怪的“候选期望1参数,0提供”在构造函数中。

时间:2021-06-24 18:48:49

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:

我正在用c++创建一个简单的线程服务器应用程序,问题是,我使用libconfig++ +来解析我的配置文件。libconfig不支持多线程,因此我使用了两个包装类来实现“支持”。关键是,其中一个失败了:

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:

当从我的主召唤时,失败得很惨。cpp文件:

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)

Which is weird because I'm clearly passing an argument, and moreover its a char *!.

这很奇怪,因为我正在传递一个参数,而且它是一个char *!

Well, as always, any help will be appreciated.

嗯,一如既往,任何帮助都会受到感激。

Julian.

朱利安。

2 个解决方案

#1


10  

You're trying to default-construct your config, and then assign to it later. But you don't have a default constructor.

您正在尝试默认构造配置,然后稍后再分配给它。但是你没有默认的构造函数。

The correct way to pass an argument to the constructor of a member variable is:

将参数传递给成员变量的构造函数的正确方法是:

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

You can still trap the exception, by using what's known as a function try-block. See http://www.gotw.ca/gotw/066.htm

您仍然可以通过使用函数try-block来捕获异常。参见http://www.gotw.ca/gotw/066.htm

Final code:

最后的代码:

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;
}

#2


4  

First of all, don't allocate mutexes dynamically, it serves no purpose. Second of all, it's because you have a data member that cannot be default-constructed, and you didn't initialise it in the ctor init list. Plus, never assign string literals to char* variables (it should be app_config(const char*) if you really want to dabble with char pointers).

首先,不要动态地分配互斥体,它没有任何作用。其次,这是因为您有一个不能默认构造的数据成员,并且您没有在ctor init列表中初始化它。另外,永远不要给char*变量分配字符串文字(如果您真的想要使用char指针的话,应该是app_config(const char*))。

Your app_main::app_main should look like this instead:

app_main::app_main应该是这样的:

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;
}

#1


10  

You're trying to default-construct your config, and then assign to it later. But you don't have a default constructor.

您正在尝试默认构造配置,然后稍后再分配给它。但是你没有默认的构造函数。

The correct way to pass an argument to the constructor of a member variable is:

将参数传递给成员变量的构造函数的正确方法是:

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

You can still trap the exception, by using what's known as a function try-block. See http://www.gotw.ca/gotw/066.htm

您仍然可以通过使用函数try-block来捕获异常。参见http://www.gotw.ca/gotw/066.htm

Final code:

最后的代码:

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;
}

#2


4  

First of all, don't allocate mutexes dynamically, it serves no purpose. Second of all, it's because you have a data member that cannot be default-constructed, and you didn't initialise it in the ctor init list. Plus, never assign string literals to char* variables (it should be app_config(const char*) if you really want to dabble with char pointers).

首先,不要动态地分配互斥体,它没有任何作用。其次,这是因为您有一个不能默认构造的数据成员,并且您没有在ctor init列表中初始化它。另外,永远不要给char*变量分配字符串文字(如果您真的想要使用char指针的话,应该是app_config(const char*))。

Your app_main::app_main should look like this instead:

app_main::app_main应该是这样的:

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;
}