C++纯虚函数问题请指教

#include
#include
using namespace std;

class Toy {
public:
virtual void talk() const=0;
};

class Dog: public Toy {
// Write your code here
public:
void talk()
{
cout<<"Wow"<<endl;
}
};

class Cat: public Toy {
// Write your code here
public:
void talk()
{
cout<<"Meow"<<endl;
}
};

class ToyFactory {
public:
/**
* @param type a string
* @return Get object of the type
/
Toy
getToy(string& type) {
// Write your code here
if(type=="Dog")
{
Dog d;
return d;
}
if(type=="Cat")
{
Cat *c;
return c;
}
}
};
int main()
{
string type;
type="Dog";
ToyFactory
tf = new ToyFactory();
Toy* toy = tf->getToy(type);
toy->talk();
return 0;
}

上代码。

 #include <iostream>
 #include <string>
 #include <memory>



using namespace std;
class Toy {
public:
    virtual void talk() const = 0;
};
class Dog : public Toy {
    // Write your code here
public:
    void talk() const { cout << "Wow" << endl; }
};
class Cat : public Toy {
    // Write your code here
public:
    void talk() const { cout << "Meow" << endl; }
};
class ToyFactory {
public:
    /**
    * @param type a string
    * @return Get object of the type
    */
    unique_ptr<Toy> getToy(string &type) {
        // Write your code here
        if (type == "Dog") {
            return unique_ptr<Dog>(new Dog);
        }
        if (type == "Cat") {
            return unique_ptr<Cat>(new Cat);
        }
    }
};
int main() {
    string type;
    type                = "Dog";
    ToyFactory *    tf  = new ToyFactory();
    unique_ptr<Toy> toy = tf->getToy(type);
    toy->talk();
    return 0;
}

主要是以下问题:
1,Dog/Cat类型talk方法的签名和Toy的talk方法签名不一致,Toy的签名是void talk() const,注意const
2,getToy方法不应该返回Toy类型,因为Toy是一个不能实例化的抽象类。返回Toy类型的指针,应当return new Dog;return new Cat;,窝这里使用了unique_ptr,不用在意。
3,ToyFactory tf = new ToyFactory应该是ToyFactory *tf,注意new 返回的是指针。

运行后出错,不知道什么原因,请各位大大指教

你确定你是运行出错?
这代码根本无法编译好吗?
可以改为:
Toy* getToy(string& type) {
// Write your code here
if(type=="Dog")
{
Dog* d=new Dog;
return d;
}
if(type=="Cat")
{
Cat *c=new Cat;
return c;
}
}
};