C++에서 연결된 목록을 구현하려고하지만 컴파일 할 때마다 'Node* Node::nextPtr' is private
이라는 오류가 발생합니다. nextPtr
을 공개 보호로 변경하면 오류가 발생하지 않고 내 목록이 정상입니다. 누군가 이것이 이것이 왜 그리고 어떻게 고칠 지 말해 줄 수 있습니까?C++ 연결 목록의 개인 포인터 오류
//list.h
#include <string>
#include "node.h"
using namespace std;
class List
{
public:
List();
bool isEmpty();
void insertAtFront(string Word);
void displayList();
private:
Node * firstPtr;
Node * lastPtr;
};
//node.h
#ifndef NODE_H
#define NODE_H
#include <string>
using namespace std;
class Node
{
public:
Node(string arg);
string getData();
private:
string data;
Node * nextPtr;
};
//node.cpp
#include <iostream>
#include <string>
#include "node.h"
using namespace std;
Node::Node(string arg)
:nextPtr(0)
{
cout << "Node constructor is called" << endl;
data = arg;
}
string Node::getData()
{
return data;
}
//list.cpp
#include <iostream>
#include "list.h"
#include "node.h"
using namespace std;
List::List()
:firstPtr(0), lastPtr(0)
{
}
bool List::isEmpty()
{
if(firstPtr == lastPtr)
return true;
else
return false;
}
void List::displayList()
{
Node * currPtr = firstPtr;
do
{
if(currPtr->nextPtr == lastPtr) // Error here
cout << endl << currPtr->getData() << endl;
cout << endl << currPtr->getData() << endl;
currPtr = currPtr->nextPtr; //Error here
}
while(currPtr != lastPtr);
}
void List::insertAtFront(string Word)
{
Node * newPtr = new Node(Word);
if(this->isEmpty() == true)
{
firstPtr = newPtr;
cout << "Adding first element...." << endl;
}
else if(this->isEmpty() == false)
{
newPtr->nextPtr = firstPtr; //Error here
firstPtr = newPtr;
cout << "Adding another element...." << endl;
}
}
당신이 우리의 라인을 보여줄 수,
Node
nextPtr
대중Node
에서 친구로List
선언에 액세스 할 수Node
공공 접근 기능을 추가 할 수 있습니다 오류가있는 코드? – luiscubal'class Node {'선언 안에'friend class List; 또는'Node'를'class List'의 private 중첩 클래스로 만드는 것을 고려해보십시오. 즉, 클래스가 속한 곳에 배치하십시오. – WhozCraig
끝에 두 클래스의 구현 파일을 추가했습니다. – rafafan2010