文件名; 打开(文件名); 如果(!file.is_open()){ cout>numOfQuestions>>key>>studentFile; fileForStudents.open(studentFile); 如果(!fileForStudents.is_open()){ cout>id>>学生->名字[i]>>学生->姓氏[i]>>学生->考试[i]; 学生->id[i]=id;//问题在这里 i++; } 计算分数(学生[人数]); 返回0; },c++,structure,C++,Structure" /> 文件名; 打开(文件名); 如果(!file.is_open()){ cout>numOfQuestions>>key>>studentFile; fileForStudents.open(studentFile); 如果(!fileForStudents.is_open()){ cout>id>>学生->名字[i]>>学生->姓氏[i]>>学生->考试[i]; 学生->id[i]=id;//问题在这里 i++; } 计算分数(学生[人数]); 返回0; },c++,structure,C++,Structure" />

C++;指向对象类型的结构指针错误 我对C++结构有一个问题。在我下面的程序中,我试图从文件中读取考试的问题数量、考试答案以及学生考试答案的文件。这一切都是可行的,但当我尝试将学生的信息放入结构的数组中时,变量id由于某种原因不起作用。编译器是Microsoft Visual Studio 2017 RC,它说“students->id[i]”有一个错误,它说:“表达式必须有指向对象类型的指针”,我不知道为什么。我标记了问题所在并删除了其余的代码,我所拥有的只是正在使用的calculateGrade函数。我已经在这方面工作了一段时间了,如果不解决这个问题,我将一事无成。感谢您的帮助 #include<iostream> #include<fstream> #include<string> using namespace std; struct studentInfo { int id; string firstName; string lastName; string exam; }; double calculateGrade(struct studentInfo); int main() { const int SIZE = 12; studentInfo students[SIZE]; string fileName, key, studentFile; ifstream file, fileForStudents; int numOfQuestions, i = 0, id; cout << "Please enter a file name: "; cin >> fileName; file.open(fileName); if (!file.is_open()) { cout << "Could not open file"; } file >> numOfQuestions >> key >> studentFile; fileForStudents.open(studentFile); if (!fileForStudents.is_open()) { cout << "Could not open file"; } while (!fileForStudents.eof()) { fileForStudents >> id >> students->firstName[i] >> students->lastName[i] >> students->exam[i]; students->id[i] = id; //issue is here i++; } calculateGrade(students[SIZE]); return 0; } #包括 #包括 #包括 使用名称空间std; 结构学生信息{ int-id; 字符串名; 字符串lastName; 弦乐考试; }; 双重计算等级(struct studentInfo); int main(){ 常数int SIZE=12; 学生信息学生[人数]; 字符串文件名、键、studentFile; ifstream文件,适用于学生的文件; 整数个问题,i=0,id; cout>文件名; 打开(文件名); 如果(!file.is_open()){ cout>numOfQuestions>>key>>studentFile; fileForStudents.open(studentFile); 如果(!fileForStudents.is_open()){ cout>id>>学生->名字[i]>>学生->姓氏[i]>>学生->考试[i]; 学生->id[i]=id;//问题在这里 i++; } 计算分数(学生[人数]); 返回0; }

C++;指向对象类型的结构指针错误 我对C++结构有一个问题。在我下面的程序中,我试图从文件中读取考试的问题数量、考试答案以及学生考试答案的文件。这一切都是可行的,但当我尝试将学生的信息放入结构的数组中时,变量id由于某种原因不起作用。编译器是Microsoft Visual Studio 2017 RC,它说“students->id[i]”有一个错误,它说:“表达式必须有指向对象类型的指针”,我不知道为什么。我标记了问题所在并删除了其余的代码,我所拥有的只是正在使用的calculateGrade函数。我已经在这方面工作了一段时间了,如果不解决这个问题,我将一事无成。感谢您的帮助 #include<iostream> #include<fstream> #include<string> using namespace std; struct studentInfo { int id; string firstName; string lastName; string exam; }; double calculateGrade(struct studentInfo); int main() { const int SIZE = 12; studentInfo students[SIZE]; string fileName, key, studentFile; ifstream file, fileForStudents; int numOfQuestions, i = 0, id; cout << "Please enter a file name: "; cin >> fileName; file.open(fileName); if (!file.is_open()) { cout << "Could not open file"; } file >> numOfQuestions >> key >> studentFile; fileForStudents.open(studentFile); if (!fileForStudents.is_open()) { cout << "Could not open file"; } while (!fileForStudents.eof()) { fileForStudents >> id >> students->firstName[i] >> students->lastName[i] >> students->exam[i]; students->id[i] = id; //issue is here i++; } calculateGrade(students[SIZE]); return 0; } #包括 #包括 #包括 使用名称空间std; 结构学生信息{ int-id; 字符串名; 字符串lastName; 弦乐考试; }; 双重计算等级(struct studentInfo); int main(){ 常数int SIZE=12; 学生信息学生[人数]; 字符串文件名、键、studentFile; ifstream文件,适用于学生的文件; 整数个问题,i=0,id; cout>文件名; 打开(文件名); 如果(!file.is_open()){ cout>numOfQuestions>>key>>studentFile; fileForStudents.open(studentFile); 如果(!fileForStudents.is_open()){ cout>id>>学生->名字[i]>>学生->姓氏[i]>>学生->考试[i]; 学生->id[i]=id;//问题在这里 i++; } 计算分数(学生[人数]); 返回0; },c++,structure,C++,Structure,您只是把索引放错了位置-它应该是students[i].firstName而不是students->firstName[i]等等,因为students是数组 此外,该行不正确: calculateGrade(students[SIZE]); 它可能会编译,但您可以使用UB进行越界访问。如果需要传递整个数组,请传递指向第一个元素和大小的指针,但最好使用std::vector或std::array并通过引用传递它 因此,对于其他问题,例如为什么要使用此类代码: students->firs

您只是把索引放错了位置-它应该是
students[i].firstName
而不是
students->firstName[i]
等等,因为
students
是数组

此外,该行不正确:

calculateGrade(students[SIZE]);
它可能会编译,但您可以使用UB进行越界访问。如果需要传递整个数组,请传递指向第一个元素和大小的指针,但最好使用
std::vector
std::array
并通过引用传递它

因此,对于其他问题,例如为什么要使用此类代码:

 students->firstName[i]
编译时,首先
students
是一个C样式的数组,它可以隐式地衰减为指向第一个元素的指针,因此
students->firstName
等于
students[0]。firstName
然后
students->firstName[i]
等于
students[0]。firstName[i]
将从字符串中访问第i个符号


因此,使用
std::vector
还有另一个原因-你的表达式
students->firstName[i]
也不会编译,也不会提供错误的表达式来证明这样的代码是正确的。

你只是把索引放错了地方-它应该是
students[i]。firstName
而不是
students->firstName[i]
等等,因为
学生
是数组

此外,该行不正确:

calculateGrade(students[SIZE]);
它可能会编译,但您可以使用UB进行越界访问。如果需要传递整个数组,请传递指向第一个元素和大小的指针,但最好使用
std::vector
std::array
并通过引用传递它

因此,对于其他问题,例如为什么要使用此类代码:

 students->firstName[i]
编译时,首先
students
是一个C样式的数组,它可以隐式地衰减为指向第一个元素的指针,因此
students->firstName
等于
students[0]。firstName
然后
students->firstName[i]
等于
students[0]。firstName[i]
将从字符串中访问第i个符号


因此,使用
std::vector
还有另一个原因-你的表达式
students->firstName[i]
也不会编译,也不会提供错误的表达式来证明这样的代码是正确的。

students->id
只是一个
int
,它不是数组,所以你不能使用
students->id[i]


数组是
students
,所以它应该是
students[i].id
。你不使用
->
,因为
students
是一个结构数组,而不是指针数组。

students->id
只是一个
int
,它不是数组,所以你不能使用
students->id[i]/code>


数组是
students
,因此它应该是
students[i]“<代码> >代码> > > <代码>学生< /C>是一组结构,不是指针数组。

大写中C++中的常量是反模式。C++中大写的常量是反模式。@ Barmar THX,固定的,有太多问题要解决,然后谢谢你的帮助,我必须有FO。R当我重写代码时,我用另一种方法测试了它,将文本放入变量,然后放入数组,但后来我移动了它,使它立即进入数组,只是将索引放在了错误的位置。你知道为什么只有数组的id部分是唯一给我错误的部分吗?@FreckledTerror97因为您可以索引字符串,所以它引用字符串中该位置的字符。@FreckledTerror97因为
std::string
具有
运算符[]
同样,这会给你一个符号,但
int
不是数组或对象。哦,好吧。谢谢!@Barmar thx,修复了,有太多问题需要立即解决。谢谢你的帮助,我一定是在重写代码时忘了把索引放在那里,我用另一种方式测试了它,在那里我将文本放入一个变量中e,然后放入数组,但后来我将它移动到数组中,使它立即进入数组,只是将索引放在了错误的位置。你知道为什么只有数组的id部分给我一个错误吗?@FreckledTerror97因为你可以索引字符串,所以它引用字符串中该位置的字符。@FreckledTerror97因为
std::string
也有
操作符[]
,它给你一个符号,但是
int
不是数组或对象。哦,好的。