C++ 在链表中修改的数据不会反映在内存中
我有两门课,分别是Family.cpp和Child.cpp。使用family实例,我只能添加最小的孩子(即,在末尾)。使用Child类,我只能创建一个更小的孩子。这可以通过从族类函数child*getChild(unsigned int i)中获取子级来实现 家庭C++ 在链表中修改的数据不会反映在内存中,c++,class,pointers,c++11,dynamic-memory-allocation,C++,Class,Pointers,C++11,Dynamic Memory Allocation,我有两门课,分别是Family.cpp和Child.cpp。使用family实例,我只能添加最小的孩子(即,在末尾)。使用Child类,我只能创建一个更小的孩子。这可以通过从族类函数child*getChild(unsigned int i)中获取子级来实现 家庭 #ifndef FAMILY_H_ #define FAMILY_H_ #include "Child.h" // This is a more advanced exercise for copy and move // con
#ifndef FAMILY_H_
#define FAMILY_H_
#include "Child.h"
// This is a more advanced exercise for copy and move
// constructors.
// Here there is a system of two classes and the
// copy and move should apply to the Family so as
// to create a full copy of the Family and Child.
// You can think about the main thing that is used
// by other users as the Family and the Child is
// exposed but it is going to be used only as part
// of a Family.
//
// The Family contains several children.
// The main functions that are possible to do
// to a family is to add a (youngest) child
// and to retrieve the i-th child.
// A family also supports copy and move semantics.
class Family {
public:
Family();
Family(const Family&);
~Family();
// Add a new youngest child with this name and this
// dob.
void addChild(const char* name, unsigned int dob);
// Get the i-th child of the family
Child* getChild(unsigned int) const;
// Copy and move through assignment operator
Family& operator=(const Family&);
Family& operator=(Family&&);
private:
void inline addChildren_(const Family&);
void inline reallocate_();
void inline clean_();
Child** children_;
unsigned int childrenCapacity_;
unsigned int numberOfChildren_;
};
#endif /* FAMILY_H_ */
Child.h
#ifndef CHILD_H_
#define CHILD_H_
// This is the child that should appear as part of the family.
// Each child has a name and a date of birth
// The length of the name should be unbounded
// Each child also has pointers to the (closest) older sibling and
// the (closest) younger sibling.
// If there is no such sibling, then, clearly, this pointer is nullptr.
// Each child
class Child {
public:
Child();
Child(const char* name,unsigned int dob);
~Child();
void name(char* buffer) const;
unsigned int dob() const;
Child* olderSibling() const;
Child* youngerSibling() const;
// Create a new immediate younger sibling (using dynamic memory!!!)
// If a younger sibling already exists then the new one is adopted
// and should be between the two.
// If the age order between the siblings turns out to be
// wrong the operation should fail and return null pointer.
// Notice that twins (yes, up to the millisecond) should be
// possible.
Child* addYoungerSibling(const char* name, unsigned int dob);
// Copy and move through assignment operators
Child& operator=(const Child& other);
Child& operator=(Child&&);
private:
Child* siblingOlder_;
Child* siblingYounger_;
char* childName_;
int childDob_;
void inline copyChildName_(const Child&);
void clean_();
};
#endif /* CHILD_H_ */
Family.cpp
输出:
Members: A 5, B 10, D 20,
A is older than B
B is younger than A and is older than D
D is younger than B
Members: A 5, B 10, D 20,
A is older than B
B is younger than A and is older than C
D is younger than C
但是当我将以下代码附加到main.cpp后,输出如下
Child *childB = f.getChild(1);
Child *childC = childB->addYoungerSibling("C", 15);
printFamily(f);
输出:
Members: A 5, B 10, D 20,
A is older than B
B is younger than A and is older than D
D is younger than B
Members: A 5, B 10, D 20,
A is older than B
B is younger than A and is older than C
D is younger than C
我想当我尝试添加一个更小的孩子时,它会被添加,但不会载入家庭记忆。我如何解决这个问题?也许你没有把他加入这个家庭? 未添加到子项\[] 你可能应该叫addChild它叫AddYoungersiling
C++中,使用字符串不是字符(当你只返回字符串时为什么使用字符串)? 在代码中有各种各样的东西可以修复。如果你上传h文件,如果你愿意的话,会更容易得到帮助
祝你好运我添加了.h文件。这也是我的作业,我被限制使用char*,所以我不能使用字符串。然后我可以将child.h包含在family.h中,但不能将child.h包含在family.h中,反之亦然。请你说得更精确一点好吗?不要说太多细节:在你的主语中,你叫Child->addyoungersiling。而不是Family->addChild(称为Child->addyoungersiling)。因此,孩子有了一个新的兄弟,但他的名字没有添加到家庭子女数组中。。。我不明白第二句话,家庭必须知道孩子是什么(即包括它的h文件)。。。
Members: A 5, B 10, D 20,
A is older than B
B is younger than A and is older than C
D is younger than C