繁体   English   中英

C ++:尝试理解对矢量的引用

[英]C++: Trying to understand pass by reference to vectors

我正在学习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 database\n";
    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();
    }
}

不知道我是否理解您的要求,但是在您的Decision函数中,您不是通过引用或指针传递,而是通过值传递。

您希望通过引用传递动物园,因为您希望该函数修改传递给动物园的动物园,而不是对其进行复制和修改。

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

如评论中所述,您想将函数的签名更改为:

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

在这些行中:

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

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

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

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

  1. 你忘了放; Animal类声明的末尾。
  2. main()函数中,while循环中没有close ) ,在这里: while(input != "3";
  3. 然后,您将拥有像void addAnimal(animal toAdd);这样的方法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) void decide(std::string input, Zoo zooInput)更改为void decide(std::string input, Zoo& zooInput) 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 database\n";
    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
$ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM