【发布时间】:2023-03-22 13:09:01
【问题描述】:
例如:
graph.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
graph.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
}
main.cpp
#include "graph.h"
#include <iostream>
using namespace std;
int main()
{
return 0; //Note I have not yet created a graph in main
}
如何访问上述函数的结构节点成员?
这是错误:
graph.cpp: In constructor ‘graph::graph(std::string*, double*)’:
graph.cpp:24:8: error: invalid use of ‘struct graph::node’
this->node->label = newName;
【问题讨论】:
-
你要做的是:
this->head->label = newName;;node是一种类型,而不是成员数据。无论如何,我建议你不要使用指针,即:定义node head;而不是node* head;,然后使用this->head.label = newName; -
挑剔有时会有所帮助:您访问的不是结构/类的成员,而是结构/类实例的成员
-
这是什么奇怪的图表,伙计?你确定你说的不是树?
-
this->head->name = *newName;
标签: c++ struct private-members