如何从C ++中的函数返回结构? [英] How to return a struct from a function in C++?

查看:309
本文介绍了如何从C ++中的函数返回结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过几个不同的论坛,似乎无法得到直接的答案,我该如何让此函数返回结构体?如果我尝试'返回newStudent;'我收到错误'没有合适的用户定义从studentType到studentType的转换存在。'

  //输入函数
studentType newStudent()
{
struct studentType
{
string studentID;
字符串firstName;
字符串lastName;
string subjectName;
字符串courseGrade;

int arrayMarks [4];

double avgMarks;

} newStudent;

cout<< \\\
请输入学生信息:\\\
;

cout<< \\\
First Name:;
cin>> newStudent.firstName;

cout<< \\\
Last Name:;
cin>> newStudent.lastName;

cout<< \学生ID:;
cin>> newStudent.studentID;

cout<< \主题名称:;
cin>> newStudent.subjectName;

for(int i = 0; i {cout<< \\\
Test<< i + 1<< mark:;
cin>> newStudent.arrayMarks [I];
}

newStudent.avgMarks = calculate_avg(newStudent.arrayMarks,NO_OF_TEST);
newStudent.courseGrade = calculate_grade(newStudent.avgMarks);


$ / code $ / pre

解决方案

您的代码编辑版本基于 ISO C ++ ,可与G ++一起使用:

  #include< string.h> 
#include< iostream>
使用namespace std;

#define NO_OF_TEST 1

struct studentType {
string studentID;
字符串firstName;
字符串lastName;
string subjectName;
字符串courseGrade;
int arrayMarks [4];
double avgMarks;
};

studentType input(){
studentType newStudent;
cout<< \\\
请输入学生信息:\\\
;

cout<< \\\
First Name:;
cin>> newStudent.firstName;

cout<< \\\
Last Name:;
cin>> newStudent.lastName;

cout<< \学生ID:;
cin>> newStudent.studentID;

cout<< \主题名称:;
cin>> newStudent.subjectName;

for(int i = 0; i cout<< \\\
Test<< i + 1<< mark:;
cin>> newStudent.arrayMarks [I];
}

返回newStudent;
}

int main(){
studentType s;
s = input();

cout<<\\\
========<< endl<< 收集
<< s.firstName<< ENDL;

返回0;
}


I've tried on a few different forums and can't seem to get a straight answer, how can I make this function return the struct? If I try 'return newStudent;' I get the error 'No suitable user-defined conversion from studentType to studentType exists.'

// Input function
studentType newStudent()
{   
    struct studentType
    {
        string studentID;
        string firstName;
        string lastName;
        string subjectName;
        string courseGrade;

        int arrayMarks[4];

        double avgMarks;

    } newStudent;

    cout << "\nPlease enter student information:\n";

    cout << "\nFirst Name: ";
    cin >> newStudent.firstName;

    cout << "\nLast Name: ";
    cin >> newStudent.lastName;

    cout << "\nStudent ID: ";
    cin >> newStudent.studentID;

    cout << "\nSubject Name: ";
    cin >> newStudent.subjectName;

    for (int i = 0; i < NO_OF_TEST; i++)
    {   cout << "\nTest " << i+1 << " mark: ";
        cin >> newStudent.arrayMarks[i];
    }

    newStudent.avgMarks = calculate_avg(newStudent.arrayMarks,NO_OF_TEST );
    newStudent.courseGrade = calculate_grade (newStudent.avgMarks);

}

解决方案

Here is an edited version of your code which is based on ISO C++ and which works well with G++:

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

#define NO_OF_TEST 1

struct studentType {
    string studentID;
    string firstName;
    string lastName;
    string subjectName;
    string courseGrade;
    int arrayMarks[4];
    double avgMarks;
};

studentType input() {
    studentType newStudent;
    cout << "\nPlease enter student information:\n";

    cout << "\nFirst Name: ";
    cin >> newStudent.firstName;

    cout << "\nLast Name: ";
    cin >> newStudent.lastName;

    cout << "\nStudent ID: ";
    cin >> newStudent.studentID;

    cout << "\nSubject Name: ";
    cin >> newStudent.subjectName;

    for (int i = 0; i < NO_OF_TEST; i++) {
        cout << "\nTest " << i+1 << " mark: ";
        cin >> newStudent.arrayMarks[i];
    }

    return newStudent;
}

int main() {
    studentType s;
    s = input();

    cout <<"\n========"<< endl << "Collected the details of "
        << s.firstName << endl;

    return 0;
}

这篇关于如何从C ++中的函数返回结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆