对象指针向量似乎没有按 C++ 中的预期执行;

发布于 2024-10-21 17:09:38 字数 1231 浏览 2 评论 0原文

我关注了很长一段时间,但这是我第一次提出问题。简而言之,问题是; 矢量<学生*> StudentVector 是一个对象指针向量,似乎将学生信息推回为 student ;但是当我打印以查看向量中的第一个记录是否按预期执行时,我发现它总是用新来的学生信息更新第一条记录,尽管 studentvector.size() 上没有问题它推动记录回向量的次数与我调用 addStudent(...) 的次数一样多,但它会用最后一个学生的信息填充所有向量。在不使用智能指针或高级东西的情况下,可以采取什么措施来成功地在该帧内用正确的信息填充向量?

抱歉,如果我的问题含糊不清。您可能会引导我提供理解该问题所必需的内容。提前致谢。

addStudent(const string alias, const string name) throw(StudentException)
{
Student* student = new Student(alias, name)
studentvector.push_back(student);
cout << studentvector.front() << endl;
}

那就是Student的实现;

#include "Student.h"

string *Alias;
string *Name;


Student::Student(string alias)
{
    Alias = new string(alias);
}

Student::Student(string alias, string name)
{
    Alias = new string(alias);
    Name = new string(name);
}

Student::~Student()
{
    delete Alias;
    delete Name;
}

const string& Student::getAlias() const
{
    return *Alias;
}

void Student::setAlias(const string& alias)
{
    *Alias = alias;
}

const string& Student::getName() const
{
    return *Name;
}

void Student::setName(const string& name)
{
    *Name = name;
}

考虑不保留别名。

I have been a follower for a long time but this is the first time I ask a question. In a nutshell, the issue is; vector<Student*> studentvector that is a vector of object pointers seems to push the student info back as student ; but when I print to see the first one in vector whether it performs as intended, I see it always updates the first record with new coming student info although there is no problem on studentvector.size() It pushes the record back to the vector as many as how many time I call addStudent(...) but it fills all vector with the information of last student. What may be done to succeed in filling vector with correct info within this frame, without using smart pointers or advanced stuff?

Sorry if I am vague on my question. You may lead me to provide what is also necessary to understand the problem. Thanks in advance.

addStudent(const string alias, const string name) throw(StudentException)
{
Student* student = new Student(alias, name)
studentvector.push_back(student);
cout << studentvector.front() << endl;
}

That is the implementation of Student;

#include "Student.h"

string *Alias;
string *Name;


Student::Student(string alias)
{
    Alias = new string(alias);
}

Student::Student(string alias, string name)
{
    Alias = new string(alias);
    Name = new string(name);
}

Student::~Student()
{
    delete Alias;
    delete Name;
}

const string& Student::getAlias() const
{
    return *Alias;
}

void Student::setAlias(const string& alias)
{
    *Alias = alias;
}

const string& Student::getName() const
{
    return *Name;
}

void Student::setName(const string& name)
{
    *Name = name;
}

Consider alias is not reserved.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吾性傲以野 2024-10-28 17:09:38

哎哟!这是您的问题 - Student 类型的所有对象都使用相同的global 指针:

string *Alias;
string *Name;

将这两个对象作为您类中的成员


class Student
{
private:
    string *Alias;
    string *Name;
//..
}; 

EDIT
另外,我不认为使用指向 std::string 的指针是个好主意,我建议您这样使用:

class Student
{
private:
    string Alias;
    string Name;
//..
}; 

Doh! Here's your problem - all objects from type Student use the same global pointers:

string *Alias;
string *Name;

Make these two as members in you class


class Student
{
private:
    string *Alias;
    string *Name;
//..
}; 

EDIT:
Also, I don't think, that it's a good idea to use pointer to std::string, I'd suggest you to use like this:

class Student
{
private:
    string Alias;
    string Name;
//..
}; 
泪是无色的血 2024-10-28 17:09:38

您的 Student.cpp 定义了一个全局指针 Alias 和一个指针 Name。您真正想要的是每个 Student 对象都有一个单独的 AliasName。您可以通过向类中添加成员来实现此目的:

class Student {
public:
  Student(const std::string& a, const std::string& n);
  //...
private:
  std::string Alias;
  std::string Name;
};

Student::Student(const std::string& a, const std::string& n)
  : Alias(a), Name(n)
{}

Your Student.cpp defines a single global pointer Alias and a single pointer Name. What you really want is a separate Alias and Name for each Student object. You do this by adding members to the class:

class Student {
public:
  Student(const std::string& a, const std::string& n);
  //...
private:
  std::string Alias;
  std::string Name;
};

Student::Student(const std::string& a, const std::string& n)
  : Alias(a), Name(n)
{}
鹿港巷口少年归 2024-10-28 17:09:38

我可能在这里神志不清,但你不是专门打印在前面,同时推到后面吗?

studentvector.push_back(student);
cout << studentvector.front() << endl;

你推到背面,而不是正面,然后打印前面的内容。当然,您并没有看到正面的变化。您需要打印背面或推前。如果前推不可用,您可以使用 insert(container.begin(), object)

您还需要将这些全局字符串变量作为成员移动到类中,以便对于 Student 的每个实例,学生都有 NameAlias 的实例代码>.

另一个注意事项...您正在动态分配字符串类。 string 类的目的是为您处理 char* 字符串的动态内存。据我从您的代码中可以看出,在这种情况下您没有理由使用 string*string 将为您在内部处理newdelete

I may be delirious here, but are you not specifically printing out front, while pushing back?

studentvector.push_back(student);
cout << studentvector.front() << endl;

You're pushing onto the back, not the front, then printing what's in front. Of course you're not seeing front changing. You need to print back or push front. If push front isn't available, you can use insert(container.begin(), object).

You also need to move those global string variables into the class as members, so that for each instance of Student, the student has instances of Name and Alias.

Another note... you're dynamically allocating a string class. The purpose of the string class is to handle the dynamic memory of a char* string for you. There's no reason for you to use string*s in this situation, as far as I can tell from your code. string will handle the new and delete internally for you.

坏尐絯 2024-10-28 17:09:38

我同意其他人所说的。但是,我无法重现您所说的问题。特别是,以下(相当讨厌)代码正确输出两行“Student(foo,bar)”。

#include <iostream>
#include <vector>
#include <string>

// NOTE WELL: many things in this code are bad style and should not be imitated.
// One of them is the namespace-polluting using-directive below:
using namespace std;

struct Student {
  string alias, name;
  Student(string a, string n) : alias(a), name(n) {}
};
class StudentException : public exception {};

vector<Student*> studentvector;

ostream& operator<<(ostream& stream, Student* student) {
  stream << "Student(" << student->alias << "," << student->name << ")";
  return stream;
}

void addStudent(const string alias, const string name) throw(StudentException)
{
  Student* student = new Student(alias, name);
  studentvector.push_back(student);
  cout << studentvector.front() << endl;
}

int main(void) {
  addStudent("foo","bar");
  addStudent("baz","quux");
}

了解您的(不工作的)代码与上述代码有何不同可能会有所帮助。

一些可能相关的评论:

  1. 您绝对不会以某种方式混淆向量的前端和后端吗?
  2. 如果(与您的 addStudent 函数不同)您的实际代码有一个 single Student 对象,并且正在修改它,然后将指向它的指针推送到您的向量上,那么您当然会得到某种错误的结果你描述的,因为它每次都是相同的指针。

I agree with what everyone else has said. However, I can't reproduce the problem you say you're having. In particular, the following (rather icky) code correctly outputs two lines saying "Student(foo,bar)".

#include <iostream>
#include <vector>
#include <string>

// NOTE WELL: many things in this code are bad style and should not be imitated.
// One of them is the namespace-polluting using-directive below:
using namespace std;

struct Student {
  string alias, name;
  Student(string a, string n) : alias(a), name(n) {}
};
class StudentException : public exception {};

vector<Student*> studentvector;

ostream& operator<<(ostream& stream, Student* student) {
  stream << "Student(" << student->alias << "," << student->name << ")";
  return stream;
}

void addStudent(const string alias, const string name) throw(StudentException)
{
  Student* student = new Student(alias, name);
  studentvector.push_back(student);
  cout << studentvector.front() << endl;
}

int main(void) {
  addStudent("foo","bar");
  addStudent("baz","quux");
}

It might be helpful to know how your (not-working) code diverges from the above.

A couple of remarks that conceivably might be relevant:

  1. You aren't by any chance confusing the front and back ends of the vector somehow?
  2. If (unlike your addStudent function) your actual code has a single Student object, and is modifying it and then pushing a pointer to it onto your vector, then of course you'll get the kind of wrong results you describe, because it'll be the same pointer every time.
め七分饶幸 2024-10-28 17:09:38

std::vector::front() 将返回对向量中第一个元素的引用。
如果你想删除元素,你需要调用 pop_back() 它将返回元素并从向量中删除。

std::vector::front() will return you the reference to the first element in the vector.
If you want to remove element, you need to call pop_back() which will return the element and remove from the vector.

记忆消瘦 2024-10-28 17:09:38

如果您有 Boost,请查看 Boost 指针容器

If you have Boost then check out Boost Pointer Containers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文