我正在开展学生项目,看起来我被困了。我创建了一个名为Bone的类,其中有一个指向另一个Bone对象的指针。 在另一个类中,我有Bone对象的向量。我正在从文件中读取向量的值,它工作正常。我还能够检查子指针是否为!= NULL但是我不能获得名称之类的值等。我做错了什么?
Bone.h
class Bone {
public:
Bone();
char name[30];
Bone *child = NULL;
Bone *sibiling = NULL;
};
其他类
std::vector<Bone> skeletonBones;
for (int i=0; i<skeletonBones.size(); i++){
Bone *bone, **boneTmp;
bone = &skeletonBones[i];
if (&bone->child != NULL){
boneTmp = &bone->child;
cout << "child - " << << endl; //here is the point where I have no idea how to print the child name
}
else
cout << "no child" << endl;
}
我感谢任何帮助。
答案 0 :(得分:1)
#include <iostream>
#include <vector>
class Bone
{
public:
Bone();
std::string name;
Bone * child { nullptr };
Bone * sibiling { nullptr };
};
int main()
{
std::vector < Bone > skeleton_bones;
// add values to skeleton_bones
for (std::size_t i = 0; i < skeleton_bones.size(); ++i)
{
Bone * bone = &skeleton_bones[i];
if (bone->child)
{
Bone * child = bone->child;
std::cout << "child is : " << child->name << std::endl;
}
else
std::cout << "no child" << std::endl;
}
return 0;
}