对象指针向量似乎没有按 C++ 中的预期执行;
我关注了很长一段时间,但这是我第一次提出问题。简而言之,问题是; 矢量<学生*> 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
哎哟!这是您的问题 -
Student
类型的所有对象都使用相同的global 指针:将这两个对象作为您类中的成员
EDIT:
另外,我不认为使用指向 std::string 的指针是个好主意,我建议您这样使用:
Doh! Here's your problem - all objects from type
Student
use the same global pointers:Make these two as members in you class
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:您的 Student.cpp 定义了一个全局指针
Alias
和一个指针Name
。您真正想要的是每个Student
对象都有一个单独的Alias
和Name
。您可以通过向类中添加成员来实现此目的:Your Student.cpp defines a single global pointer
Alias
and a single pointerName
. What you really want is a separateAlias
andName
for eachStudent
object. You do this by adding members to the class:我可能在这里神志不清,但你不是专门打印在前面,同时推到后面吗?
你推到背面,而不是正面,然后打印前面的内容。当然,您并没有看到正面的变化。您需要打印背面或推前。如果前推不可用,您可以使用
insert(container.begin(), object)
。您还需要将这些全局字符串变量作为成员移动到类中,以便对于
Student
的每个实例,学生都有Name
和Alias
的实例代码>.另一个注意事项...您正在动态分配字符串类。 string 类的目的是为您处理
char*
字符串的动态内存。据我从您的代码中可以看出,在这种情况下您没有理由使用string*
。string
将为您在内部处理new
和delete
。I may be delirious here, but are you not specifically printing out front, while pushing back?
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 ofName
andAlias
.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 usestring*
s in this situation, as far as I can tell from your code.string
will handle thenew
anddelete
internally for you.我同意其他人所说的。但是,我无法重现您所说的问题。特别是,以下(相当讨厌)代码正确输出两行“Student(foo,bar)”。
了解您的(不工作的)代码与上述代码有何不同可能会有所帮助。
一些可能相关的评论:
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)".
It might be helpful to know how your (not-working) code diverges from the above.
A couple of remarks that conceivably might be relevant:
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.
如果您有 Boost,请查看 Boost 指针容器。
If you have Boost then check out Boost Pointer Containers.