奇怪的“候选人期望 1 个参数,提供 0 个”在构造函数中
我正在用 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;
};
从我的 main.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;
}
}
它说:
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)
这很奇怪,因为我显然正在传递一个参数,而且它是一个 char *
!。
好吧,一如既往,任何帮助将不胜感激。
朱利安.
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)
Which is weird because I'm clearly passing an argument, and moreover its a char *
!.
Well, as always, any help will be appreciated.
Julian.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试默认构建您的配置,然后稍后分配给它。但你没有默认的构造函数。
将参数传递给成员变量的构造函数的正确方法是:
您仍然可以通过使用所谓的函数 try 块来捕获异常。请参阅 http://www.gotw.ca/gotw/066.htm
最终代码:
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:
You can still trap the exception, by using what's known as a function try-block. See http://www.gotw.ca/gotw/066.htm
Final code:
首先,不要动态分配互斥体,它没有任何作用。其次,这是因为您有一个无法默认构造的数据成员,并且您没有在 ctor init 列表中初始化它。另外,永远不要将字符串文字分配给
char*
变量(如果您确实想尝试使用 char 指针,则应该是app_config(const char*)
)。您的
app_main::app_main
应如下所示: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 beapp_config(const char*)
if you really want to dabble with char pointers).Your
app_main::app_main
should look like this instead: