C++ 如何为c+中对象指针的常量向量赋值+;?

C++ 如何为c+中对象指针的常量向量赋值+;?,c++,vector,C++,Vector,我目前正在尝试将该值赋值为const std::vector。年龄是从动物类派生出来的类 #include <iostream> #include <vector> class Animal{ public: Animal(std::string name) : name_(name) {} std::string getName() {return name_;} protected: std::string name_; }; c

我目前正在尝试将该值赋值为const std::vector。年龄是从动物类派生出来的类

#include <iostream>
#include <vector>

class Animal{
  public:
    Animal(std::string name) : name_(name) {}
    std::string getName() {return name_;}
  protected:
    std::string name_;
};


class Age : public Animal
{
  public:
    Age(std::string name, int age) : Animal(name), age_(age) {}
  private:
    int age_;
};

class AllAnimals
{
  public:
    AllAnimals(){};
    void setVector(const std::vector<Age*> v) {all_animals_ = v;}
  private:
    const std::vector<Age*> all_animals_;
};



int main()
{
  AllAnimals animals();
  std::vector<Age*> all_animals;
  all_animals.push_back(new Age("Cat", 3));
  all_animals.push_back(new Age("Dog", 4));
  all_animals.push_back(new Age("Mouse", 2));
  all_animals.push_back(new Age("Duck", 1));
  return 0;
}
#包括
#包括
类动物{
公众:
动物(std::string name):名称(name){
std::string getName(){返回名称}
受保护的:
std::字符串名称;
};
班级年龄:公共动物
{
公众:
年龄(std::string name,int-Age):动物(name),年龄(Age){
私人:
国际年龄;
};
动物类
{
公众:
所有动物(){};
void setVector(const std::vector v){all_=v;}
私人:
const std::载体所有动物;
};
int main()
{
所有动物();
所有动物的病媒;
所有动物。推回(新时代(“猫”),3);
所有动物。推回(新时代(“狗”),4);
所有动物。推回(新时代(“老鼠”,2));
所有动物。推回(新时代(“鸭子”),1);
返回0;
}
编译代码后,我收到错误消息:

 error: passing ‘const std::vector<Age*>’ as ‘this’ argument discards qualifiers [-fpermissive] void setVector(const std::vector<Age*> v) {all_animals_ = v;}
错误:将'const std::vector'作为'this'参数传递将丢弃限定符[-fppermissive]void setVector(const std::vector v){all_=v;}

我的实现有什么问题?如何为所有动物赋值?

向量初始化后无法赋值,它是
const
。解决方案是初始化它。由于您的代码处理的是原始指针,需要一些关于正确使用它们或使用智能指针的提示,但这不是问题的主题,因此我允许自己简单介绍一下示例:

#include <vector>

struct foo {
    const std::vector<int> x;
    foo(const std::vector<int>& a) : x(a) {}
};

int main() {
    std::vector<int> v{1,2,3,4};
    foo f(v);
}
#包括
结构foo{
常数std::向量x;
foo(const std::vector&a):x(a){}
};
int main(){
std::向量v{1,2,3,4};
傅f(v),;
}
在执行构造函数主体之前初始化成员,因此初始化它的位置是构造函数成员初始值设定项列表

请注意,创建成员
const
很少是一个好主意。例如,当实例具有
const
成员时,无法复制实例。如果您希望一个成员在构造之后不被修改,那么封装就真的值得了。将成员设置为私有,并且不提供任何来自外部的写访问。然后从外部成员是不可修改的,但您仍然可以复制该类的实例


PS:这里的错误消息可能有点混乱。它指的是
std::vector的
=
,因此消息中的
this
是向量,而不是
AllAnimals
实例。“将XY作为'this'参数传递会丢弃限定符”是当成员函数的常量正确性出现问题时,您会收到的典型错误消息。

在向量初始化后,您无法将其赋值,它是
常量。解决方案是初始化它。由于您的代码处理的是原始指针,需要一些关于正确使用它们或使用智能指针的提示,但这不是问题的主题,因此我允许自己简单介绍一下示例:

#include <vector>

struct foo {
    const std::vector<int> x;
    foo(const std::vector<int>& a) : x(a) {}
};

int main() {
    std::vector<int> v{1,2,3,4};
    foo f(v);
}
#包括
结构foo{
常数std::向量x;
foo(const std::vector&a):x(a){}
};
int main(){
std::向量v{1,2,3,4};
傅f(v),;
}
在执行构造函数主体之前初始化成员,因此初始化它的位置是构造函数成员初始值设定项列表

请注意,创建成员
const
很少是一个好主意。例如,当实例具有
const
成员时,无法复制实例。如果您希望一个成员在构造之后不被修改,那么封装就真的值得了。将成员设置为私有,并且不提供任何来自外部的写访问。然后从外部成员是不可修改的,但您仍然可以复制该类的实例


PS:这里的错误消息可能有点混乱。它指的是
std::vector的
=
,因此消息中的
this
是向量,而不是
AllAnimals
实例。“将XY作为'this'参数传递将丢弃限定符”是当成员函数的const正确性出现问题时所收到的典型错误消息。

谢谢。现在我明白多了,谢谢。现在我明白多了。