提问者:小点点

有人能帮我吗?C++中的面向对象编程


有人能帮我处理代码吗?在第44行,当传递给编译器时,我得到一个错误,因为它告诉我初始值设定项没有声明,并且没有一个参数是有效的。我的程序的功能是面向对象的,我想做的是将信息放入类中,为私有的值添加值,用Cin引入它们,用Cout弹出它们,有人能帮我吗?

#include <iostream>
#include <string>
using namespace std;

class Zoo{
    
    private:
        string esp;
        int id;
        string nom;
    
    public:
        
        Zoo(string, int, string);
        
        void mDatos(){
            
            this->esp = "";
            this->id = 0;
            this->nom = "";
        }
        
        void getZoologico(string _E, int _ID, string _N){
            
            this->esp = _E;
            this->id = _ID;
            this->nom = _N;
            
    }
    
};

Zoo::Zoo(string _esp, int _id, string _nom){
    
    esp= _esp;
    id= _id;
    nom= _nom;
    
}


int main(){
    
    Zoo animales[5]= Zoo( string _esp, int _id, string _nom);
    int contador = 0;
    
    do{
        system("cls");
        
        string E, N;
        int ID;
        
        cout<< "\n Especie: ";
        cin>> E;
        cout<< "\n ID: ";
        cin>> ID;
        cout<< "\n Nombre: ";
        cin>> N;
        
        animales[contador].getZoologico(E, ID, N);
        contador++;
        
        
        
        
        
        
    }while (contador < 5);
    
    system("Pause");
    return (0);
    
}

共1个答案

匿名用户

以下是您的固定程序(请注意comments):

#include <iostream>
#include <string>
#include <vector>  // added - required header

using namespace std;

class Zoo {

  private:
    string esp;
    int id;
    string nom;

  public:
    Zoo() {}  // added - a default constructor
    Zoo(string, int, string);

    void mDatos() {
        this->esp = "";
        this->id = 0;
        this->nom = "";
    }

    void getZoologico(string _E, int _ID, string _N) {
        this->esp = _E;
        this->id = _ID;
        this->nom = _N;
    }
};

Zoo::Zoo(string _esp, int _id, string _nom) {
    esp = _esp;
    id = _id;
    nom = _nom;
}

int main(void) {
    std::vector<Zoo> animales(5);  // added - Creating a vector of 5
    int contador = 0;              // rooms of type Zoo

    do {
        string E, N;
        int ID;

        cout << "\n Especie: ";
        cin >> E;
        cout << "\n ID: ";
        cin >> ID;
        cout << "\n Nombre: ";
        cin >> N;

        // edited - instead of accessing directly, used at() class
        // method defined under std::vector<> to access it safely
        animales.at(contador).getZoologico(E, ID, N);
        contador++;

    } while (contador < 5);

    return (0);
}

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(有人|帮|我吗|c++|中|面向对象|编程)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?