如何从C++中的函数返回结构?

tai*_*ion 25 c++ struct structure return function

我试过几个不同的论坛,似乎无法得到一个直接的答案,我怎么能让这个函数返回结构?如果我试着'返回newStudent;' 我收到错误"没有从studentType到studentType的合适的用户定义转换."

// 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);

}
Run Code Online (Sandbox Code Playgroud)

小智 34

这是您的代码的编辑版本,它基于ISO C++,适用于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;
}
Run Code Online (Sandbox Code Playgroud)

  • 使用指针阻止复制构造函数不是更有效吗? (2认同)
  • 返回一个已经在栈上分配的变量不是很糟糕吗? (2认同)

Fre*_*son 13

你有一个范围问题.在函数之前定义结构,而不是在函数内部.


Zac*_*and 7

studentType newStudent() // studentType doesn't exist here
{   
    struct studentType // it only exists within the function
    {
        string studentID;
        string firstName;
        string lastName;
        string subjectName;
        string courseGrade;

        int arrayMarks[4];

        double avgMarks;

    } newStudent;
...
Run Code Online (Sandbox Code Playgroud)

将其移到函数外:

struct studentType
{
    string studentID;
    string firstName;
    string lastName;
    string subjectName;
    string courseGrade;

    int arrayMarks[4];

    double avgMarks;

};

studentType newStudent()
{
    studentType newStudent
    ...
    return newStudent;
}
Run Code Online (Sandbox Code Playgroud)

  • @Ramesh-X 否,因为“new”创建了一个指向在堆上创建的实例的“指针”。如果直接返回结构,它只是将结构的位“复制”(足够智能的编译器可能只使用调用者为返回的结构提供的位置)到函数调用结果分配给的左值中呼叫者,召集者。当你返回一个指向本地结构的“指针”(该指针可能是一个引用,实际上并不需要使用指针来实现)时,就会出现返回本地结构的问题。 (2认同)

Reh*_*aja 7

您现在可以 (C++14) 返回一个本地定义的(即在函数内部定义的),如下所示:

auto f()
{
    struct S
    {
      int a;
      double b;
    } s;
    s.a = 42;
    s.b = 42.0;
    return s;
}

auto x = f();
a = x.a;
b = x.b;
Run Code Online (Sandbox Code Playgroud)