#include <iostream> #include <fstream> #include <string> using namespace std; //myString.txt file is in the same folder with the following: //apple orange grape kiwi banana pineapple grapefruit duran class Node{ public: //Constructors Node(); Node(string st); Node(Node *); //get functions string getValue(); Node * getNext(); //set functions void setValue(string v); void setNext(Node * nx); //Utility functions void print(); private: string value; Node * next; }; class LinkedList{ public: //Constructors LinkedList(); LinkedList(Node * nd); //Destructor ~LinkedList(); //public functions void print(); void add(Node * nd); void reverse(); private: Node * first; //Pointer to the first node of the list }; //Global function void buildList(LinkedList * ls) { ifstream inData; inData.open("myStrings.txt"); string st; Node * nd; inData >> st; while(inData) { nd = new Node(st); ls.add(nd); //error C2228: left of '.add' must have class/struct/union inData >> st; } } int main() { //Create an object of LinkedList LinkedList * lst = new LinkedList(); //Build a linked list buildList(lst); //Print the list lst.print(); //error C2228: left of '.print' must have class/struct/union //Revese it lst.reverse(); //error C2228: left of '.reverse' must have class/struct/union //Print it again and check if it is reversed. lst.print(); //error C2228: left of '.print' must have class/struct/union return 0; } |
ls.
or lst.
with ls->
or lst->
respectively. Those errors are showing up because ls/lst is a pointer to a LinkedList and the dot operator is only for non pointer class/struct/union instances. The -> is the same as doing a dereference followed by an access. Let's say you have LinkedList* lst
. Then lst->print()
is the same thing as (*lst).print()
.