简体   繁体   English

C ++帮助学生的指针

[英]C++ Help with pointers for a student

I am trying to create a vector and pass it to a function allowing the vector to be modified. 我正在尝试创建一个向量,并将其传递给允许修改向量的函数。 Here is an abridged version of my code that doesn't work. 这是我的代码的简化版本,不起作用。

void addStudent(vector<Student*>*);
int main()
{
   vector<Student*> students = new vector<Student*>;
   addStudent(students);
   return(0);
}
void addStudent(vector<Student*> *students)
{
   students->push_back(new Student("bob")); 
}

This code is compiling with errors. 这段代码编译有错误。 I think I'm not passing the pointer correctly but I'm not sure. 我认为我没有正确传递指针,但不确定。

You've promised to give addStudent a pointer to a vector: 您已承诺给addStudent一个指向向量的指针:

void addStudent(vector<Student*> *students)

so, use the address-of operator to get a pointer: 因此,请使用地址运算符获取指针:

vector<Student*> students;
addStudent(&students);

There's nothing here that actually needs dynamic allocation, but if you did, note that new also returns a pointer: 这里实际上没有任何东西需要动态分配,但是如果您这样做了,请注意new也将返回一个指针:

vector<Student*>* students_ptr = new vector<Student*>();
addStudent(students_ptr);

Another option is to pass by reference: 另一种选择是通过引用传递:

void addStudent(vector<Student*>& students)
vector<Student*> students;
addStudent(students);

But I prefer the pointer, when the function is going to change its parameter. 但是当函数要更改其参数时,我更喜欢使用指针。

Your problem is here: 您的问题在这里:

vector<Student*> students = new vector<Student*>;

students is declared as a value type but you're assigning it a pointer. students被声明为值类型,但您正在为其分配一个指针。

In the future you should include the compiler error in your question. 将来您应该在问题中包含编译器错误。

You probably come from a Java background... so you'll have to learn to live without new for a while :) 您可能是来自Java背景的人,所以您将不得不学习一会儿没有new生活:)

In C++ there are 2 ways to create objects: 在C ++中,有两种创建对象的方法:

  • objects with automatic storage duration (created on the stack) 具有自动存储持续时间的对象(在堆栈上创建)
  • objects with dynamic storage duration (created on the heap) 具有动态存储持续时间的对象(在堆上创建)

new creates objects of the second kind, for which YOU must ensure proper disposal (by calling delete according to text books, but... we'll stay away from this for now). new创建第二种对象,您必须确保对其进行适当处理(根据教科书调用delete ,但是...我们暂时不要这样做)。

Here, you don't need new : 在这里,您不需要new

std::vector<Student> students; // create an empty `vector` of `Student` objects

students.push_back(Student("bob")); // push a new Student in the vector

As for the function, you have several possibilities: 至于功能,您有几种可能性:

  • make it return the object 使它返回对象
  • pass the object by reference void addStudent(std::vector<Student>& students); 通过引用传递对象void addStudent(std::vector<Student>& students);
  • pass by pointer, but then use the address of operator to get to it addStudent(&students); 通过指针传递,然后使用运算符的地址到达它addStudent(&students);

You don't have to use pointers everywhere in C++... in fact, it's probably better you don't, to begin with. 在C ++中,您不必在所有地方都使用指针。实际上,最好不要这样做。

Oh, and you definitely need a good tutorial, that's basic stuff, and you can't get into C++ without the boring basics... sorry :/ 哦,您肯定需要一个不错的教程,这是基础知识,没有无聊的基础知识,您就无法进入C ++。对不起:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM