Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;错误:没有匹配函数_C++_Vector_Polymorphism - Fatal编程技术网

C++ c++;错误:没有匹配函数

C++ c++;错误:没有匹配函数,c++,vector,polymorphism,C++,Vector,Polymorphism,这是我的密码 #include <iostream> #include <vector> #include <memory> #include <tr1/memory> using namespace std; class Animal { public: string name; Animal (const std::string& givenName) : name(givenName) { }

这是我的密码

#include <iostream>
#include <vector>
#include <memory>
#include <tr1/memory> 
using namespace std;

class Animal {
  public:
    string name;
    Animal (const std::string& givenName) : name(givenName) {

    }

  };

class Dog: public Animal {
  public:
    Dog (const std::string& givenName) : Animal (givenName) {

    }
    string speak ()
      { return "Woof, woof!"; }
  };

class Cat: public Animal {
  public:
    Cat (const std::string& givenName) : Animal (givenName) {
    }
    string speak ()
      { return "Meow..."; }
  };

int main() {
    vector<Animal> animals;
    Dog * skip = new Dog("Skip");
    animals.push_back( skip );
    animals.push_back( new Cat("Snowball") );

    for( int i = 0; i< animals.size(); ++i ) {
        cout << animals[i]->name << " says: " << animals[i]->speak() << endl;
    }

}
#包括
#包括
#包括
#包括
使用名称空间std;
类动物{
公众:
字符串名;
动物(const std::string和givenName):名称(givenName){
}
};
犬类:公共动物{
公众:
狗(const std::string和givenName):动物(givenName){
}
字符串说话()
{返回“汪,汪!”}
};
猫类:公共动物{
公众:
猫(const std::string和givenName):动物(givenName){
}
字符串说话()
{返回“喵…”;}
};
int main(){
载体

但是我得到了关于唯一的ptr的错误。我包括了内存。所以我不确定问题是什么


是错误消息。

有两个问题

首先,向量包含
Animal
对象,您试图用指向
Animal
派生类型的指针填充它。
Animal
Animal*
不是同一类型,因此操作通常不会编译


其次,
Animal
没有方法
speak()
。如果你要将衍生类型的
动物
推入向量,你会得到。你可以通过让向量持有指向
动物
的智能指针来避免它,例如
std::vector,如果你想将
动物*
跳过
一样,你应该将你的
向量
声明为
向量
t、 为了能够使用多态性,您确实需要在其中使用指针。此外,您的基类动物还需要一个
speak()
方法-否则,您不能在编译时只知道是
动物的对象上调用该方法。一旦进行了这些更改,它应该会像您预期的那样工作。

我已经添加了您提到的更改。但是我在唯一ptr方面遇到了错误。但是我包含了内存。所以我不确定问题是什么。是错误消息。谢谢你,我在你的帮助下得到了它。我想问一些事情来澄清。我理解有必要将std::string speak()作为虚拟方法。我不理解你为什么将其设置为=0;其次,我不确定virtual~Animal(){}的目的是什么“Kimsii我添加了一些链接,应该回答你的问题。伙计,你真棒!我从其他家伙那里得到了一些粗鲁的废话,因为我不是C++ C++的人,他跳了我的博客。因为你不是完全的C++。你是完全不同的。我的男人!现在我知道,意思是析构函数和= 0帮助超过。骑马。你太酷了,juanchopanza!!顺便说一句,这就是我在你的帮助下所做的。你太棒了!!
index.cpp: In function ‘int main()’:
index.cpp:36: error: no matching function for call to ‘std::vector<Animal, std::allocator<Animal> >::push_back(Dog*&)’
/usr/include/c++/4.2.1/bits/stl_vector.h:600: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Animal, _Alloc = std::allocator<Animal>]
index.cpp:37: error: no matching function for call to ‘std::vector<Animal, std::allocator<Animal> >::push_back(Cat*)’
/usr/include/c++/4.2.1/bits/stl_vector.h:600: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Animal, _Alloc = std::allocator<Animal>]
index.cpp:40: error: base operand of ‘->’ has non-pointer type ‘Animal’
index.cpp:40: error: base operand of ‘->’ has non-pointer type ‘Animal’
class Animal {   
 public:
  std::string name;
  Animal (const std::string& givenName) : name(givenName) {}
  virtual std::string speak () = 0;
  virtual ~Animal() {}
};

int main() {
  std::vector<std::unique_ptr<Animal>> animals;
  animals.push_back( std::unique_ptr<Animal>(new Dog("Skip")) );
  animals.push_back( std::unique_ptr<Animal>(new Cat("Snowball")) );
}