【问题标题】:C++ How to return array of objects from a functionC ++如何从函数返回对象数组
【发布时间】:2021-01-30 01:20:35
【问题描述】:

最近我开始学习 c++,所以我正在尝试使用我的基础知识制作一个简单的成绩计算器(我已经精通 Javascript,所以我知道编程的基础知识)。

所以在这种情况下,我必须从函数调用中返回一个对象数组,以便稍后在程序中使用它,但我就是找不到正确的方法。

所以基本上我想从getInput 函数返回subArr,但由于我对语言的基础知识,我无法做到这一点。我尝试了谷歌搜索,但没有找到任何简单的解决方案。

这是代码,希望它很简单:

//the Subject class:
class Subject {
    public:
        string name;
        float grade;
        int factor;
        
        Subject(){};
        
        Subject(string x, float y, int z){
            name = x;
            grade = y;
            factor = z;
        }
};

//get Input function declaration
Subject getInput(int num){
    
    //array of objects of type "Subject"
    Subject subArr[num];
    
    //a for loop to assign the array's elements
    for(int i = 0; i < num; i++){
        string name;
        float grade;
        int factor;
        
        cout << "what is the name of subject " << i+1 <<"? "<<endl;
        cin >> name;
        
        cout << "what is the grade of subject " << i+1 << "? "<<endl;
        cin >> grade;
        
        cout << "what is the factor of subject " << i+1 << "? "<<endl;
        cin >> factor;
        
        subArr[i]=Subject(name, grade, factor);
    };
    
    //trying to return the subArr at last
    return subArr;
};

//main function
int main(){
    //get the number of subjects
    int numOfSubjects;
    cout << "how many subjects are there? ";
    cin >> numOfSubjects;
    
    //trying to receive the subArr from getInput call
    Subject subArr = getInput(numOfSubjects);
    
};

【问题讨论】:

  • 使用std::vector&lt;Subject&gt;
  • Subject subArr[num]; 不是有效的 C++,因为 num 是运行时值。只需改用std::vector。
  • @cigien 但我需要num 来初始化数组的大小,你也可以给我一个使用std :: vector的代码示例吗?我是新手,谢谢
  • 成千上万的人上网。 This one 来自一个您应该收藏的可靠文档站点。
  • @SalahEddineMakdour -- 你应该在Subject 默认构造函数中初始化你的成员变量。 Subject s; std::cout &lt;&lt; s.grade; 会产生未定义的行为,因为 s.grade 没有被初始化。

标签: c++ arrays function oop object


【解决方案1】:

使用std::vector:

#include <iostream>
#include <vector>

using namespace std;

// the Subject class
class Subject {
    public:
        string name;
        float grade;
        int factor;
        
        Subject(){};
        
        Subject(string x, float y, int z){
            name = x;
            grade = y;
            factor = z;
        }
};

// get Input function declaration
vector<Subject> getInput(int num){
    
    // array of objects of type "Subject"
    vector<Subject> subArr;
    
    // a for loop to assign the array's elements
    for(int i = 0; i < num; i++){
        string name;
        float grade;
        int factor;
        
        cout << "what is the name of subject " << i+1 <<"? "<<endl;
        cin >> name;
        
        cout << "what is the grade of subject " << i+1 << "? "<<endl;
        cin >> grade;
        
        cout << "what is the factor of subject " << i+1 << "? "<<endl;
        cin >> factor;
        
        subArr.push_back(Subject(name, grade, factor));
    };
    
    // trying to return the subArr at last
    return subArr;
};

// main function
int main(){
    // get the number of subjects
    int numOfSubjects;
    cout << "how many subjects are there? ";
    cin >> numOfSubjects;
    
    // trying to receive the subArr from getInput call
    vector<Subject> subArr = getInput(numOfSubjects);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-09
    • 2018-01-25
    • 2012-06-17
    • 2015-12-15
    • 2020-07-20
    • 2017-08-02
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多