C+中的数组出现问题+;标题 所以我想用C++制作一个简单的数据库来取乐。我想用数组来做。但我还是经常出错。我希望它在用户输入学生信息后增加整数'I'。这样,就可以输入无限多的姓名和年龄。我希望最终将用户输入的姓名存储到记事本中。我可以在int main()中执行此操作,但我正在尝试使用头
我的阵列有很多问题 下面是我在头文件和CPP文件中使用的代码片段。我尝试将int'I'声明为private,int'b'声明为public,并将它们放入数组中,然后将private指向CPP文件中的public,但没有成功。 如果你们能帮我,那太好了,谢谢!顺便说一句,我是新来的,所以请原谅我的无知。我也不知道我是否正确使用了指针C+中的数组出现问题+;标题 所以我想用C++制作一个简单的数据库来取乐。我想用数组来做。但我还是经常出错。我希望它在用户输入学生信息后增加整数'I'。这样,就可以输入无限多的姓名和年龄。我希望最终将用户输入的姓名存储到记事本中。我可以在int main()中执行此操作,但我正在尝试使用头,c++,arrays,C++,Arrays,我的阵列有很多问题 下面是我在头文件和CPP文件中使用的代码片段。我尝试将int'I'声明为private,int'b'声明为public,并将它们放入数组中,然后将private指向CPP文件中的public,但没有成功。 如果你们能帮我,那太好了,谢谢!顺便说一句,我是新来的,所以请原谅我的无知。我也不知道我是否正确使用了指针 #include <string> class student3 { protected: int i; std :
#include <string>
class student3
{
protected:
int i;
std :: string *_studentFirstName[];
std :: string *_studentLastName[];
int *_studentAge[];
double *_studentGPA[];
public:
int b;
void setstudentFirstName(std :: string firstName[]);
void setstudentLastName(std :: string lastName[]);
void setstudentAge(int Age[]);
void setstudentGPA(double GPA[]);
std :: string getstudentFirstName();
std :: string getstudentLastName();
int getstudentAge();
double getstudentGPA();
}
让你的
Student
对象代表一个学生,然后使用std::vector
来收集他们
非常简单的示例:-
class Student
{
public:
std :: string firstName;
std :: string lastName;
int age;
double gpa;
}
std::vector<Student> students;
...
Student temp;
temp.firstName = "Arthur"; // or read from user, of course
temp.lasttName = "Dent";
temp.age = 42;
students.push_back(temp); // adds a *copy* of temp to the vector.
班级学生
{
公众:
std::stringfirstname;
std::字符串lastName;
智力年龄;
双gpa;
}
性病:病媒学生;
...
学生临时工;
temp.firstName=“亚瑟”//当然,也可以从用户那里阅读
temp.lasttName=“凹痕”;
温度年龄=42;
学生们。向后推(临时);//将临时的*副本*添加到向量。
k所以我想我得多读点书了。因为我不知道向量是什么。lol@Snewman8771-它只是一个可以动态增长的阵列。担心你的“学生”对象是否正确,那么收集它们就不成问题了。在你理解向量之前,你只需要学生[100]
以获取其中100个的数组。真的没有什么不同。用户可以输入学生的名字吗?我不认为codedyea很难,但我想要一个数组,它会随着用户输入姓名、年龄而动态增长,这实际上是在编译吗?你错过了一次机会;在类声明的末尾,在开始的#include“student3.h”行之前,我认为您实际上并不打算使用空指针数组。要继续,您应该使用。我认为你需要阅读更多关于数组和指针的内容,它们的相似之处是什么,它们的不同之处是什么。这是一个注释,而不是回答如果你使用的是c++
,你真的应该使用std::vector
engh仍然无法编译。
class Student
{
public:
std :: string firstName;
std :: string lastName;
int age;
double gpa;
}
std::vector<Student> students;
...
Student temp;
temp.firstName = "Arthur"; // or read from user, of course
temp.lasttName = "Dent";
temp.age = 42;
students.push_back(temp); // adds a *copy* of temp to the vector.