【发布时间】:2015-01-28 15:19:00
【问题描述】:
我正在研究一个具有以下签名的方法的类:
operator const std::string & () const
它被记录为“转换为字符串运算符”。 我想知道如何有效地调用它。不幸的是以下表达式:
std::string(foo)
产生这个错误:
some_test.cpp:13:41: error: no matching function for call to'
std::basic_string<char>::basic_string(Foo (&)(std::string))'
考虑到foo 是Foo 类型,声明和实例化为Foo foo(std::string(filename))
作为 C++ 的初学者,这让我有点困惑。关于这意味着什么的任何提示?
【问题讨论】:
-
显示
foo的声明,我很确定你与最令人烦恼的 Parse 发生了冲突。 -
我更新了帖子,声明
foo -
声明一个函数调用
foo返回一个Foo并且不带任何参数 (())。如果您只想要一个Foo对象,请将其声明为Foo foo;。 -
抱歉,为简单起见,我省略了构造函数的参数。现在我已经解决了。
标签: c++ c++11 casting operator-overloading overloading