Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 访问类私有成员中的结构成员?_C++_Struct_Private Members - Fatal编程技术网

C++ 访问类私有成员中的结构成员?

C++ 访问类私有成员中的结构成员?,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*

例如:

图形.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* 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;
挑剔有时会有帮助:你不访问结构/类的成员,但访问结构/类实例的成员它是什么样的奇怪图形,伙计?你确定你不是指树吗?
这个->头->名称=*新名称;