Linked list c++ , expected primary expression before -> - c++

im trying to learn linked lists in c++; my code look like this
#include <iostream>
#include <string>
using namespace std;
struct node{
int number;
node *next;
};
struct zoznam{
node *head=NULL;
node *tail=NULL;
};
void insertAsFirst(node *&head,node *&last, int number){
node *tmp = new node;
tmp->number = number;
tmp->next=NULL;
head=tmp;
last=tmp;
}
void insertValues( node *&head , node *&last, int number){
if(head==NULL){
insertAsFirst(zoznam->head,zoznam->tail,number);
}else{
node *tmp = new node;
tmp->number=number;
tmp->next=NULL;
last->next=tmp;
//last=tmp;
}
}
int main()
{
for( int i = 0; i < 10; i++){
insertValues(zoznam->head,zoznam->tail,i);
}
node *current=zoznam->head;
while(current!=NULL){
cin<<current->number << endl;
current=current->next;
}
return 0;
}
expected primary-expression before '->' token|
Being new to c++ , i have no idea what the error means , how can i fix that , i tried to check it up but found nothing. Thanks

As you're new to C++, I am giving you some details.
When you are using struct I mean you define any struct then it becomes a type name not a variable. To use that you need to declare variable(s) of that type.
Here, in the main, you're using zoznam, which is a type name and the definition of that type is the definition of the struct.
Declaration: zoznam var_name;
Use: var_name.head; or 'var_name.tail;'.
Learn more about struct and pointer. Read the documentations carefully not only the example codes in this two link.

Related

Reading file into linked list C++ with node structure in a class

I'm trying to read a text file containing the letters "farming" into a linked list of nodes. I've made a class named NumberList that has the structure for the nodes. Here's the header.
#ifndef NUMBERLIST
#define NUMBERLIST
#include <iostream>
using namespace std;
class NumberList
{
protected:
//declare a class for the list node
//constructor to initialize nodes of list
struct ListNode
{
char value;
ListNode *next;
// Constructor
ListNode(char value1, ListNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode *head; //pointer to head of the list
public:
NumberList() { head = NULL; } //constructor
~NumberList(); //destructor
void displayList() const; //print out list
void reverse();
};
#endif
Where I'm running into a problem is trying to read the text file into a linked list in main().
Here's what I have in main:
#include "Numberlist.h"
#include "ReliableNumberList.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ListNode *letterList = nullptr; //create a linked list
char letter;
//This is where I read the file into the list
//open the file
ifstream letterFile("linkedText.txt");
if (!letterFile)
{
cout << "Error in opening the file of letters.";
exit(1);
}
//read the file into a linked list
while (letterFile >> letter)
{
//create a node to hold this letter
letterList = new ListNode(letter, letterList);
//missing a move to the next node?
}
return 0;
}
This read file sample came from my text book but the structure its reading into was not located in a separate class. For the life of me I cannot figure out how I reference ListNode struct in the NumberList class. Visual Studio is stating that ListNode and letterList are undefined. I know its because I'm not referencing them properly from the NumberList class.
Any help would be greatly appreciated.
A quick solution to your problem could be this:
//------------------------------NumberList.hpp-----------------------------
#ifndef NUMBERLIST_HPP
#define NUMBERLIST_HPP
#include <iostream>
class NumberList{
protected:
//Protected Members can't be used outside the class
struct ListNode{
char value;
ListNode *next;
ListNode(char value1, ListNode *next1 = NULL){
value = value1;
next = next1;
}
};
ListNode *head, *tail; //class members
//head always points at the 1st letter, tail is used for quick adding at the end
public:
NumberList() { head = NULL; tail = NULL; }
~NumberList(); //don't forget to deallocate space properly at the end
void displayList() const; //print out list
void reverse();
void add(char newchar) {
//allocate a new node using the ListNode constructor, by default, next1 will be null
ListNode *newNode = new ListNode(newchar); //equvalent to ListNode(newchar, NULL);
if (tail == NULL) { //if no elements in the list, both show to newNode
tail = newNode;
head = newNode;
}else{
tail->next = newNode; //make last node -> next pointer, point to newNode (new last node)
tail = tail->next; //make current last node be the actual last node
}
}
};
#endif
//------------------------------Main.cpp-----------------------------
#include "Numberlist.hpp"
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ifstream letterFile("linkedText.txt");
if (!letterFile){
cout << "Error in opening the file of letters.";
exit(-1);
}
NumberList numberList;
char letter;
while (letterFile >> letter) numberList.add(letter);
}
It slightly alters your logic, as you no longer add list nodes to your list,
but I suspect that you don't want to, anyway. Instead, better add the characters
directly to the list, and let the list handle its nodes (makes sense, since the node struct is protected).
Certainly the class needs more refining, but this should solve your initial problem.

Linked List...declaration incompatible with the protoype

Hey guys so I am getting this error when I am trying to create this function for my linked list class. The function I am having problems with is my search function. I haven't even started creating the function yet but the error I am receiving is in the declaration of the search function. On line 38 under NodePtr it says it is undefined and under search it says Error: declaration incompatible with "LinkedList::NodePtr (declared on line 17). The code is below. Any help appreciated.
// LinkedListProject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <list>
using namespace std;
class LinkedList {
public:
struct Node {
int data;
Node* link;
};
typedef Node* NodePtr;
//NodePtr head = new Node;
void head_insert(NodePtr& head, int the_number);
NodePtr search(NodePtr head, int target);
private:
};
int main()
{
LinkedList obj;
//obj.head->data = 3;
//obj.head->link = NULL;
return 0;
}
void LinkedList::head_insert(NodePtr& head, int the_number) {
NodePtr temp_ptr = new Node;
temp_ptr->data = the_number;
temp_ptr->link = head;
head = temp_ptr;
}
NodePtr LinkedList::search(NodePtr head, int target)
{
return NodePtr();
}
You must set the right scope where NodePtr is defined.
LinkedList::NodePtr LinkedList::search(NodePtr head, int target)
{
return LinkedList::NodePtr();
}
NodePtr is a name scoped to you class. In order to use it outside of the class you need LinkedList::NodePtr. So you have to change
NodePtr LinkedList::search(NodePtr head, int target)
to
LinkedList::NodePtr LinkedList::search(NodePtr head, int target)
Now you may ask, "But wait, I didn't need it in search, what gives?", and the answer to that is after you do
LinkedList::search
The class name is injected into the rest of the scope of the function. Because of this we do not need to explicitly qualify any name that is scoped to the class.

Simple Node Program Stops Working

#include <iostream>
using namespace std;
struct ListNode{
string item;
int count;
ListNode *link;
};
typedef ListNode* ListNodePtr;
int main(){
ListNodePtr head;
head->count = 3;
head->item = "dog";
head->link = NULL;
return 0;
}
Hi I'm really new to nodes and I can not figure out why this program stops working. It will compile and run but then it crashes. It's probably super obvious but, like I said, I'm a baby when it comes to nodes.

Pointing to an object of the same type

I feel this question may be a bit trivial, but I simply cannot wrap my head around it. I currently have a class, Node, which is trying to point to what node occurs before the current node using the pointer prevNode. However I seem unable to access any variables within prevNode.
When running Main.cpp from the following code, it prints the result '15340756'. Where am I going wrong? Appologies as Im still a bit new to C++.
Node.h
#include "stdafx.h"
class Node
{
public:
Node();
void setPrevNode(Node n);
Node getPrevNode();
int i;
private:
Node *prevNode;
};
Node.cpp
#include "stdafx.h"
#include "Node.h"
Node::Node(){
i = 0;
}
void Node::setPrevNode(Node n){
prevNode = &n;
}
Node Node::getPrevNode(){
return *prevNode;
}
Main.cpp
#include "stdafx.h"
#include "Node.h"
int _tmain(int argc, _TCHAR* argv[])
{
Node nodes[] = {Node(), Node()};
nodes[0].i = 1;
nodes[1].setPrevNode(nodes[0]);
printf("%i", nodes[1].getPrevNode().i);
while(true){
}
return 0;
}
void setPrevNode(Node n);
Here setPrevNode is declared to take a copy of the node passed as an argument, and point to such node. After the function returns, the pointed to node no longer exist and what you get is undefined behavior.
What you want is to take the Node either as a reference or a pointer instead:
void setPrevNode(Node& n)
{
prevNode = &n;
}
void setPrevNode(Node* n)
{
prevNode = n;
}
On the same line, getPrevNode is defined to return a copy of the previous node. You most certainly want to return a reference here instead, although you can also return a pointer:
Node& getPrevNode()
{
return *prevNode;
}
Node* getPrevNode()
{
return prevNode;
}

error: expected unqualified-id before ‘->’ token

It's giving me this error for lines 21 and 22, which are the ones I've noted. Judging from other cases with similar error messages, I've got a syntax error somewhere. I just can't figure out what.. Here's my .cpp file:
#include <iostream>
#include <cstdlib>
#include "deque.h"
using namespace std;
struct node{
int data;
node *prev;
node *next;
};
Deque::Deque(){
count = 0;
node->head->next = node->head; //error on this line
node->head->prev = node->head; //and this one
}
Here's my header file:
# ifndef DEQUE_H
# define DEQUE_H
class Deque
{
private:
int count;
class node *head;
public:
Deque();
~Deque();
int size();
void addFirst(int);
void addLast(int);
int removeFirst();
int removeLast();
int getFirst();
int getLast();
};
#endif
Correct code for these lines:
head->next = head;
head->prev = head;
Your variable is named head, and node is its type, but there is no member named node in your class Deque
struct node has no member named head, which is a problem.
Where is your node variable coming from in Dequeue()? Looks undefined given the code that you have posted. node is a type, not a variable.
In C++ there is no need to prefix each declaration of a struct type variable with struct. If it needs to be C compatible you can always typedef the struct as well.