C++:试图通过引用向量来理解传递

C++: Trying to understand pass by reference to vectors

本文关键字:向量 引用 C++      更新时间:2023-10-16

我正在学习C++,但我真的不明白如何通过函数传递对象。我读到有三种方法可以做到这一点。按值、引用和指针传递。我想我是在尝试通过引用来做?

我很确定我在通过引用传递时遇到了问题,因为我试图在主要情况下对其进行测试,而无需通过

std::string nameInput = "Penguin";
std::string colorInput = "Black";
Animal temp(nameInput,colorInput);
zoo.addAnimal(temp);
zoo.printDatabase();

而且效果很好。但是当我尝试通过函数调用它时,我什至无法将其添加到向量中。在发布这个之前,我尝试了addAnimal(Animal &toAdd(,但我不确定到底发生了什么。因为我不就是将地址传递给函数吗?而不是对象动物本身。

或者也许我完全看错了方向。这就是为什么我喜欢在这里发帖,因为有另一双眼睛总是好的!

//Zoo.h
#include<vector>
#include<animal.h>
#ifndef ZOO_H_
#define ZOO_H_
class Zoo{
    private:
        std::vector<Animal> database;
    public:
        void addAnimal(animal toAdd);
        void printDatabase();
};
#endif ZOO_H_
//Animal.h
#include<string>
#ifndef ANIMAL_H_
#define ANIMAL_H_
class Animal{
    private:
        std::string name;
        std::string color;
    public:
        Animal(std::string name, std::string color);
        void printInfo();
}
#endif ANIMAL_H_

//Zoo methods
void Zoo::printDatabase(){
    for(std::vector<Animal>::iterator list = database.begin(); list != list.end(); list++){
        (*list).printInfo();
    }
}
void Zoo::addAnimal(Animal toAdd){
    database.push_back(toAdd);
}
//Animal Methods
Animal::Animal(std::string inputName, std::string inputColor){
    name = inputName;
    color = inputColor;
}
void Animal::printInfo(){
    std::cout << "Name: " << name << "n";
    std::cout << "Color: " << color >> "n";
}

//main.cpp
int main(){
    Zoo zoo;
    std::string input;
    do{
        printMenu();
        std::getline(std::cin, input);
        if(!input.empty()){
            decide(input, zoo);
        }
    }while(input != "3";
}
void printMenu(){
    std::cout <<"Zoo databasen";
    std::cout << "1.Add  Animal n";
    std::cout << "2.Print n";
    std::cout << "3.Exit n";
}
void decide(std::string input, Zoo zooInput){
    std::string name;
    std::string color;
    if(input == "1"){
        std::cout << "Please enter the name of the animal to add n";
        std::getline(std::cin,name);
        std::cout << "Please enter the color of the animal n";
        std::getline(std::cin,color);
        Animal temp(name,color);
        zooInput.addAnimal(temp);
    }
    if(input == "2"){
        zooInput.printDatabase();
    }
}

不确定我是否理解您在问什么,但是在您的决定函数中,您不是通过引用或指针传递,而是通过值传递。

您希望通过引用传递 zoo,因为您希望函数修改传递给它的 zoo,而不是复制并修改它。

另外,addAnimal 很好,因为您确实希望在此处按值传递。不是通过引用或指针,因为调用函数中的动物稍后会被销毁。

如注释中所述,您希望将函数的签名更改为:

void decide(std::string input, Zoo& zooInput){

在这些行中:

Animal temp(name,color);
zooInput.addAnimal(temp);

temp的副本被传递给addAnimal,随后被复制/移动到zooInput的向量中。通过引用传递它不会有任何区别,因为它无论如何都会复制到向量中。

但是,由于zooInput是按值传递的,因此对它的更改不会反映在函数外部。

首先,这甚至不应该编译...

  1. 您忘了将;放在类声明Animal末尾。
  2. main()函数中,while 循环中没有闭合),这里:while(input != "3";
  3. 然后你有像void addAnimal(animal toAdd);这样的方法,animal没有在任何地方声明,应该Animal.
  4. 在你的 for 循环中,你有for(std::vector<Animal>::iterator list = database.begin(); list != list.end(); list++){ .显然,list是一个迭代器,它没有end()方法,所以list != list.end()是完全错误的。
  5. std::cout << "Color: " << color >> "n";中,你使用>> "n",这是错误的。
  6. 先用std::getline(std::cin, input);,再用if(!input.empty())不起作用。它可能只是卡在EOF的不定式循环中。

最后,回到参考文献。将Zoo按值(复制(传递到decide()函数中。因此,它将动物添加到自己的 Zoo 私有副本中,该副本在离开函数作用域时被销毁。因此,永远不会修改main()中定义Zoo对象。要修复此问题,请将void decide(std::string input, Zoo zooInput)更改为 void decide(std::string input, Zoo& zooInput)

这是您的程序,有些固定:

#include <string>
#include <vector>
#include <iostream>
class Animal {
    std::string name;
    std::string color;
public:
    Animal(std::string name, std::string color);
    void printInfo();
};
class Zoo {
    std::vector<Animal> database;
public:
    void addAnimal(Animal toAdd);
    void printDatabase();
};
void Zoo::printDatabase(){
    for(std::vector<Animal>::iterator list = database.begin(); list != database.end(); list++){
        (*list).printInfo();
    }
}
void Zoo::addAnimal(Animal toAdd){
    database.push_back(toAdd);
}
Animal::Animal(std::string inputName, std::string inputColor){
    name = inputName;
    color = inputColor;
}
void Animal::printInfo(){
    std::cout << "Name: " << name << "n";
    std::cout << "Color: " << color << "n";
}
void printMenu(){
    std::cout <<"Zoo databasen";
    std::cout << "1.Add  Animal n";
    std::cout << "2.Print n";
    std::cout << "3.Exit n";
}
void decide(std::string input, Zoo& zooInput) {
    std::string name;
    std::string color;
    if(input == "1"){
        std::cout << "Please enter the name of the animal to add n";
        std::getline(std::cin,name);
        std::cout << "Please enter the color of the animal n";
        std::getline(std::cin,color);
        Animal temp(name,color);
        zooInput.addAnimal(temp);
    }
    if(input == "2"){
        zooInput.printDatabase();
    }
}
int main() {
    Zoo zoo;
    std::string input;
    printMenu();
    while (std::getline(std::cin, input)) {
        decide(input, zoo);
        if (input == "3")
            break;
        printMenu();
    }
}

示例运行:

$ g++ -Wall -pedantic -std=c++11 -o test ./test.cc  && ./test 
Zoo database
1.Add  Animal 
2.Print 
3.Exit 
1
Please enter the name of the animal to add 
cow
Please enter the color of the animal 
blue
Zoo database
1.Add  Animal 
2.Print 
3.Exit 
2
Name: cow
Color: blue
Zoo database
1.Add  Animal 
2.Print 
3.Exit 
1
Please enter the name of the animal to add 
lobster
Please enter the color of the animal 
green
Zoo database
1.Add  Animal 
2.Print 
3.Exit 
2
Name: cow
Color: blue
Name: lobster
Color: green
Zoo database
1.Add  Animal 
2.Print 
3.Exit 
3
$