2013-12-13 32 views
2

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

我敢肯定我有引用传递,因为我试图做

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

来测试它的主要不调用功能和它的作品了罚款的问题。但是当我尝试通过函数调用它时,我甚至无法将它添加到vector.Before发布之前,我尝试addAnimal(动物& 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(); 
    } 
} 

回答

1

不知道如果我明白你在问什么,但在你的决定函数中,你不是通过引用或指针传递,而是通过值。

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

另外,addAnimal很好,因为你确实想在这里传递值。不是通过引用或指针,因为调用函数中的动物将在稍后被销毁。在评论已经指出

1

至于,你想给你的函数的签名改为:

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

在这里这些行:

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

temp副本传给addAnimal,随后复制/搬入zooInput的向量。通过引用传递它将不会有任何区别,因为它被复制到矢量中。

但是,因为zooInput是通过值传递的,所以对它的更改不会反映在该函数之外。

+0

老实说,我永远都不会怀疑动物园是我的问题。谢谢!如果我有足够的声望,我会赞成! – user2677821

1

首先,这不应该连编...

  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()函数中。因此,它会将动物添加到它自己的动物园私人副本中,该副本会在离开功能范围时被破坏。因此,main()中定义的对象不会被修改。要修复它,请将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 
$ 
+0

我不认为这是妥善的解决和移交SO上的作业。 – makhdumi

+0

@ Al-Muhandis:对不起,我从未上过大学,所以我不知道如何区分一个学习C++的人的编程问题。 – 2013-12-13 22:18:43

+0

@ Al-Muhandis他说他正在学习C++。通常当我读到时,我不认为“功课”,而是一个自学成才的初学者。这只是我而已。问题立即引人注目,因此无需修复程序的其余部分。 – 2013-12-13 22:23:14