从 'B *' 到 'std::shared_ptr<A>' 的函数式转换没有匹配的转换
No matching conversion for functional-style cast from 'B *' to 'std::shared_ptr<A>'
我有一个尝试初始化共享指针的简单应用程序。
#include <iostream>
#include <memory>
#include <algorithm>
class A {
public:
A(){
std::cout << "default ctor for A" << std::endl;
}
~A(){
std::cout << "default dtor for A" << std::endl;
}
};
class B : A{
B(){
std::cout << "default ctor for B" << std::endl;
}
~B(){
std::cout << "default dtor for B" << std::endl;
}
};
int main()
{
auto ap = std::shared_ptr<A>(new B());
return 0;
}
我遇到了错误
No matching conversion for functional-style cast from 'B *' to 'std::shared_ptr<A>'
在这一行
自动 ap = std::shared_ptr(新 B());
我做错了什么?
Class B 应该声明为
class B : public A{
public:
B(){
std::cout << "default ctor for B" << std::endl;
}
~B(){
std::cout << "default dtor for B" << std::endl;
}
};
我有一个尝试初始化共享指针的简单应用程序。
#include <iostream>
#include <memory>
#include <algorithm>
class A {
public:
A(){
std::cout << "default ctor for A" << std::endl;
}
~A(){
std::cout << "default dtor for A" << std::endl;
}
};
class B : A{
B(){
std::cout << "default ctor for B" << std::endl;
}
~B(){
std::cout << "default dtor for B" << std::endl;
}
};
int main()
{
auto ap = std::shared_ptr<A>(new B());
return 0;
}
我遇到了错误
No matching conversion for functional-style cast from 'B *' to 'std::shared_ptr<A>'
在这一行 自动 ap = std::shared_ptr(新 B());
我做错了什么?
Class B 应该声明为
class B : public A{
public:
B(){
std::cout << "default ctor for B" << std::endl;
}
~B(){
std::cout << "default dtor for B" << std::endl;
}
};