考虑这个非常简单的代码:C++函数定义和变量声明不匹配?
#include <memory>
class Foo
{
public:
Foo() {};
};
class Bar
{
public:
Bar(const std::shared_ptr<Foo>& foo) {}
};
int main()
{
Foo* foo = new Foo;
Bar bar(std::shared_ptr<Foo>(foo));
return 0;
}
为什么Visual Studio的报告
warning C4930: 'Bar bar(std::shared_ptr<Foo>)': prototyped function not called (was a variable definition intended?)
并没有创造......这怎么行Bar bar(std::shared_ptr<Foo>(foo));
被解释为一个函数的定义没有bar
对象?
我检查Do the parentheses after the type name make a difference with new?也C++: warning: C4930: prototyped function not called (was a variable definition intended?),但我觉得我的问题是不同的这里,因为我没有使用语法Foo()
也不Bar()
。
编辑:请注意,它成功编译:
Foo* foo = new Foo;
std::shared_ptr<Foo> fooPtr(foo);
Bar bar(fooPtr);
C++最令人烦恼的解析。 –
@眠りネロクno,gcc编译它 –
@RichardHodges所以MSVC,它只是一个警告。眠りネロク是正确的。 – Quentin