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 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
是按值传递的,因此对它的更改不会反映在函数外部。
首先,这甚至不应该编译...
- 您忘了将
;
放在类声明Animal
末尾。 - 在
main()
函数中,while 循环中没有闭合)
,这里:while(input != "3";
- 然后你有像
void addAnimal(animal toAdd);
这样的方法,animal
没有在任何地方声明,应该Animal
. - 在你的 for 循环中,你有
for(std::vector<Animal>::iterator list = database.begin(); list != list.end(); list++){
.显然,list
是一个迭代器,它没有end()
方法,所以list != list.end()
是完全错误的。 - 在
std::cout << "Color: " << color >> "n";
中,你使用>> "n"
,这是错误的。 - 先用
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
$
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 创建引用向量的优雅方式
- C++重新引用向量中的值
- 很好的语法来获取对向量/数组数据的大小引用?
- 如何返回向量的常量引用?
- 取消引用向量时出现问题
- 为什么通过 vector<reference_wrapper> 的元素删除引用的值<T>不会使向量无效?
- 通过引用传递向量是请求 std::分配器
- 从类返回引用向量
- 返回两个向量 – 使用引用还是元组?
- 在C++中通过引用传递向量数组
- 引用向量中的特定索引
- 如何告诉自动推断向量<bool>元素的非引用类型
- 如何在方法主体中返回声明向量的引用?
- 向量引用C++
- 在向量引用上调用 vector::reserve()
- 如何创建一个同时接受数组和向量引用的函数模板
- 如何做列表的向量引用列表中的值
- c++向量引用转换
- 最方便的表示向量引用的向量的方法