如何从 C++ 中的函数返回结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19205024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a struct from a function in C++?
提问by tail_recursion
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.'
我在几个不同的论坛上尝试过,似乎无法得到直接的答案,我怎样才能让这个函数返回结构?如果我尝试 'return 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);
}
采纳答案by Fred Larson
You have a scope problem. Define the struct before the function, not inside it.
你有一个范围问题。在函数之前定义结构体,而不是在函数内部。
回答by Nandakumar Edamana
Here is an edited version of your code which is based on ISO C++and which works well with G++:
这是您的代码的编辑版本,它基于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;
}
回答by Zac Howland
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;
...
Move it outside the function:
将它移到函数外:
struct studentType
{
string studentID;
string firstName;
string lastName;
string subjectName;
string courseGrade;
int arrayMarks[4];
double avgMarks;
};
studentType newStudent()
{
studentType newStudent
...
return newStudent;
}
回答by Rehan Khwaja
You can now (C++14) return a locally-defined (i.e. defined inside the function) as follows:
您现在可以 (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;
回答by mkny
As pointed out by others, define studentType outside the function. One more thing, even if you do that, do not create a local studentType instance inside the function. The instance is on the function stack and will not be available when you try to return it. One thing you can however do is create studentType dynamically and return the pointer to it outside the function.
正如其他人所指出的,在函数之外定义 studentType。还有一件事,即使您这样做,也不要在函数内部创建本地 studentType 实例。该实例位于函数堆栈中,当您尝试返回它时将不可用。但是,您可以做的一件事是动态创建 studentType 并在函数外部返回指向它的指针。