C++ 访问类私有成员中的结构成员?
例如: 图形.hC++ 访问类私有成员中的结构成员?,c++,struct,private-members,C++,Struct,Private Members,例如: 图形.h #ifndef GRAPH_H #define GRAPH_H #include <iostream> #include <string> using namespace std; class graph { private: struct node { string name; int currentValue; struct node *next; }; node*
#ifndef GRAPH_H
#define GRAPH_H
#include <iostream>
#include <string>
using namespace std;
class graph
{
private:
struct node
{
string name;
int currentValue;
struct node *next;
};
node* head;
public:
graph();
~graph();
graph(string* newName, int* givenValue);
}
#endif
main.cpp
#include "graph.h"
graph::graph() {}
graph::~graph() {}
graph::graph(string* newName, int* givenValue)
{
//This is what I want to achieve
this->node->name = newName; //Compile error
}
#include "graph.h"
#include <iostream>
using namespace std;
int main()
{
return 0; //Note I have not yet created a graph in main
}
这个问题与您的私有结构无关。构造函数应该能够访问所有私有成员 问题在于您混淆了结构名
节点
和变量名头
:
此->节点->名称=新名称;//不正确
相反,你应该写:
this->head->name = *newName;
这个问题与您的私有结构无关。构造函数应该能够访问所有私有成员 问题在于您混淆了结构名
节点
和变量名头
:
此->节点->名称=新名称;//不正确
相反,你应该写:
this->head->name = *newName;
如果要访问类变量,应调用
this->head->name = *newName;
虽然您可以省略这个->
,但是下面的内容很好
head->name = *newName;
其他几点注意事项:
是一个指针,因此您需要使用解引用运算符“*”(即string*newName
而不是head->name=*newName;
head->name=newName;
是一个指针,当前您正试图访问未初始化的指针。您可能还需要类似于node*head
的内容head=new node();
this->head->name = *newName;
虽然您可以省略这个->
,但是下面的内容很好
head->name = *newName;
其他几点注意事项:
是一个指针,因此您需要使用解引用运算符“*”(即string*newName
而不是head->name=*newName;
head->name=newName;
是一个指针,当前您正试图访问未初始化的指针。您可能还需要类似于node*head
的内容head=new node();
;
以结束您的类声明:
class graph
{
// ...
};
然后,您键入了this->node->name
,而node
是一个类型。将此行更改为this->head->name
。请注意,指针head
在此未初始化
<> >,<代码> NeNeX< /代码>属于“代码>字符串*<代码>,而<代码> >此-> ->名称< /代码>属于“代码>字符串类型。根据您如何使用您的类,您可以考虑修改这样的代码:
graph::graph(const string& newName, int givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = newName;
}
graph::graph(string* newName, int* givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = *newName;
}
或者像这样:
graph::graph(const string& newName, int givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = newName;
}
graph::graph(string* newName, int* givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = *newName;
}
另外,请阅读。您的问题与私人访问无关。首先,添加
;
以结束您的类声明:
class graph
{
// ...
};
然后,您键入了this->node->name
,而node
是一个类型。将此行更改为this->head->name
。请注意,指针head
在此未初始化
<> >,<代码> NeNeX< /代码>属于“代码>字符串*<代码>,而<代码> >此-> ->名称< /代码>属于“代码>字符串类型。根据您如何使用您的类,您可以考虑修改这样的代码:
graph::graph(const string& newName, int givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = newName;
}
graph::graph(string* newName, int* givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = *newName;
}
或者像这样:
graph::graph(const string& newName, int givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = newName;
}
graph::graph(string* newName, int* givenValue):
head(new node)
{
//This is what I want to achieve
this->head->name = *newName;
}
另外,请阅读。您要做的是:
此->头->标签=新名称;
;节点
是一种类型,而不是成员数据。无论如何,我建议您不要使用指针,即:定义节点头;
而不是节点*头;
,然后使用此->头.标签=新名称;
挑剔有时会有帮助:您无法访问ss是一个结构/类的成员,但是是一个结构/类实例的成员它是什么样的奇怪图形,伙计?你确定你不是指树吗?this->head->name=*newName;
你要做的是:this->head->label=newName;
;节点
是一个类型,不是一个成员数据。无论如何,我建议你不要使用po例如:定义节点头;
而不是节点*头;
,然后使用this->head.label=newName;
挑剔有时会有帮助:你不访问结构/类的成员,但访问结构/类实例的成员它是什么样的奇怪图形,伙计?你确定你不是指树吗?这个->头->名称=*新名称;