Help with link list errors

linked list 3 errors

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2271 Views - Last Post: 11 November 2008 - 01:58 PM Rate Topic: -----

#1 quatre432  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 08

Help with link list errors

Posted 10 November 2008 - 09:46 PM

Hello i have a promblem i have this program that im writing for a project when i build i get three errors that are drivin me insane like something formal brackets and another a semicolan error but anyways here my code
Networknode.h
#ifndef NETWORKNODE
#define NETWORKNODE

#include <iostream>
using namespace std;

class Networknode{
public:
	void add_node_at_end();
	void display_list();

};


#endif


networknode.cpp
#include<iostream>
#include"Networknode.h"
using namespace std;

struct node
  {  char idnum[20];
	 char ipad[20];		  
	 char os[20];	 
	 node *nxt;
  };

node *start_ptr = NULL;
node *current;		 
int option = 0;

void Networknode::add_node_at_end()
  {  node *temp, *temp2;   // Temporary pointers

	 // Reserve space for new node and fill it with data
	 temp = new node;
	 cout << "Please enter the ID number: ";
	 cin >> temp->idnum;
	 cout << "Please enter the ip address of the person : ";
	 cin >> temp->ipad;
	 cout << "Please enter the os of the person : ";
	 cin >> temp->os;
	 temp->nxt = NULL;

	 // Set up link to this node
	 if (start_ptr == NULL)
	   { start_ptr = temp;
	 current = start_ptr;
	   }
	 else
	   { temp2 = start_ptr;
		 // We know this is not NULL - list not empty!
		 while (temp2->nxt != NULL)
		   {  temp2 = temp2->nxt;
			  // Move to next link in chain
		   }
		 temp2->nxt = temp;
	   }
  }

void Networknode::display_list()
  {  node *temp;
	 temp = start_ptr;
	 cout << endl;
	 if (temp == NULL)
	   cout << "The list is empty!" << endl;
	 else
	   { while (temp != NULL)
	   {  // Display details for what temp points to
			  cout << "ID : " << temp->idnum << " ";
			  cout << "IP : " << temp->ipad << " ";
		  cout << "OS : " << temp->os;
		  if (temp == current)
		cout << " <-- Current node";
			  cout << endl;
		  temp = temp->nxt;

	   }
	 cout << "End of list!" << endl;
	   }
  }


netowork.h
#ifndef NETWORK
#define NETWORK

#include <iostream>
#include "Networknode.h"
using namespace std;

class Network{
public:
void display_list();
void delete_start_node();
void delete_end_node();
void move_current_on ();
void move_current_back ();
}
#endif
[/cpde]
netork.cpp
[code]
#include <iostream>
#include "Network.h"
#include "Networknode.h"
using namespace std;
struct node
  {  char idnum[20];
	 char ipad[20];		  
	 char os[20];	 
	 node *nxt;
  };

node *start_ptr = NULL;
node *current;		 
int option = 0;



void Network::delete_start_node()
   { node *temp;
	 temp = start_ptr;
	 start_ptr = start_ptr->nxt;
	 delete temp;
   }

void Network::delete_end_node()
   { node *temp1, *temp2;
	 if (start_ptr == NULL)
		  cout << "The list is empty!" << endl;
	 else
		{ temp1 = start_ptr;
		  if (temp1->nxt == NULL)
			 { delete temp1;
			   start_ptr = NULL;
			 }
		  else
			 { while (temp1->nxt != NULL)
				{ temp2 = temp1;
				  temp1 = temp1->nxt;
				}
			   delete temp1;
			   temp2->nxt = NULL;
			 }
		}
   }

void Network::move_current_on ()
   { if (current->nxt == NULL)
	  cout << "You are at the end of the list." << endl;
	 else
	  current = current->nxt;
   }

void Network::move_current_back ()
   { if (current == start_ptr)
	  cout << "You are at the start of the list" << endl;
	 else
	  { node *previous;	 // Declare the pointer
		previous = start_ptr;

		while (previous->nxt != current)
		  { previous = previous->nxt;
		  }
		current = previous;
	  }
   }


main program
#include <iostream>
#include "Network.h"
#include "Networknode.h"



void main();

struct node
  {  char idnum[20];
	 char ipad[20];		  
	 char os[20];	 
	 node *nxt;
  };

Networknode *start_ptr = NULL;
Networknode *current;		 
int option = 0;

	{  start_ptr = NULL;
	 do
	{

	display_list();
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout << "0. Exit the program." << endl;
	  cout << "1. Add a node to the end of the list." << endl;
	  cout << "2. Delete the start node from the list." << endl;
	  cout << "3. Delete the end node from the list." << endl;
	  cout << "4. Move the current pointer on one node." << endl;
	  cout << "5. Move the current pointer back one node." << endl;
	  cout << endl << " >> ";
	  cin >> option;

	  switch (option)
		{
		  case 1 : add_node_at_end(); break;
		  case 2 : delete_start_node(); break;
		  case 3 : delete_end_node(); break;
		  case 4 : move_current_on(); break;
		  case 5 : move_current_back();
		}
	}
	 while (option != 0);
  }



any help would be much preciated, please help it do by midnight tommorow

EDIT: Title edited to be more descriptive

PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: Help with link list errors

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Help with link list errors

Posted 10 November 2008 - 09:48 PM

knowing the three errors would be enormously helpful (syntax errors are the easiest to find and fix), should give you line numbers, etc...

This post has been edited by KYA: 10 November 2008 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Help with link list errors

Posted 10 November 2008 - 09:57 PM

The way you have your structs set up you need to put the 'struct' keyword before every declaration of a struct object.
struct node
  {  char idnum[20];
	 char ipad[20];		  
	 char os[20];	
	 node *nxt;
  };

is what you have. If you replace the declaration of the node pointer with
struct node *nxt that will work.

If you add the struct keyword in front of the type when you declare a struct object you should be fine.
Was This Post Helpful? 0
  • +
  • -

#4 quatre432  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 08

Re: Help with link list errors

Posted 10 November 2008 - 10:10 PM

View PostGWatt, on 10 Nov, 2008 - 08:57 PM, said:

The way you have your structs set up you need to put the 'struct' keyword before every declaration of a struct object.
struct node
  {  char idnum[20];
	 char ipad[20];		  
	 char os[20];	
	 node *nxt;
  };

is what you have. If you replace the declaration of the node pointer with
struct node *nxt that will work.

If you add the struct keyword in front of the type when you declare a struct object you should be fine.


tried it i still get the same errors when i run the program
.\main.cpp(7) : error C2628: 'Network' followed by 'void' is illegal (did you forget a ';'?)
.\main.cpp(20) : error C2447: '{' : missing function header (old-style formal list?)
.\Network.cpp(4) : error C2143: syntax error : missing ';' before 'using'
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Help with link list errors

Posted 10 November 2008 - 10:13 PM

Your main source file is missing brackets for the main() method.

You have:

void main()
{ // add this
//code down here
} //and one of these



edit and get rid of the semicolon after main()

This post has been edited by KYA: 10 November 2008 - 10:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 quatre432  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 08

Re: Help with link list errors

Posted 10 November 2008 - 10:20 PM

View PostKYA, on 10 Nov, 2008 - 09:13 PM, said:

Your main source file is missing brackets for the main() method.

You have:

void main()
{ // add this
//code down here
} //and one of these



edit and get rid of the semicolon after main()

after doing this i got 9 errors
.\main.cpp(7) : error C2628: 'Network' followed by 'void' is illegal (did you forget a ';'?)
.\main.cpp(7) : error C3874: return type of 'main' should be 'int' instead of 'Network'
.\main.cpp(24) : error C3861: 'display_list': identifier not found
.\main.cpp(38) : error C3861: 'add_node_at_end': identifier not found
.\main.cpp(39) : error C3861: 'delete_start_node': identifier not found
.\main.cpp(40) : error C3861: 'delete_end_node': identifier not found
.\main.cpp(41) : error C3861: 'move_current_on': identifier not found
.\main.cpp(42) : error C3861: 'move_current_back': identifier not found

i had this program working as a full thing but for this project i have to split the files into 5 diff 2 classes and a main
Was This Post Helpful? 0
  • +
  • -

#7 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Help with link list errors

Posted 10 November 2008 - 10:25 PM

Can you post your updated main.cpp?
Was This Post Helpful? 0
  • +
  • -

#8 quatre432  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 08

Re: Help with link list errors

Posted 10 November 2008 - 10:29 PM

View PostGWatt, on 10 Nov, 2008 - 09:25 PM, said:

Can you post your updated main.cpp?


here my main
#include <iostream>
#include "Network.h"
#include "Networknode.h"



void main(){

struct node
  {  char idnum[20];
	 char ipad[20];		  
	 char os[20];	 
	 struct node *nxt;
  };

Networknode *start_ptr = NULL;
Networknode *current;		 
int option = 0;

	{  start_ptr = NULL;
	 do
	{

	display_list();
	  cout << endl;
	  cout << "Please select an option : " << endl;
	  cout << "0. Exit the program." << endl;
	  cout << "1. Add a node to the end of the list." << endl;
	  cout << "2. Delete the start node from the list." << endl;
	  cout << "3. Delete the end node from the list." << endl;
	  cout << "4. Move the current pointer on one node." << endl;
	  cout << "5. Move the current pointer back one node." << endl;
	  cout << endl << " >> ";
	  cin >> option;

	  switch (option)
		{
		  case 1 : add_node_at_end(); break;
		  case 2 : delete_start_node(); break;
		  case 3 : delete_end_node(); break;
		  case 4 : move_current_on(); break;
		  case 5 : move_current_back();
		}
	}
	 while (option != 0);
  }
}


Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Help with link list errors

Posted 10 November 2008 - 10:32 PM

Your pointer must call those methods since it points to the Network class. you can't call them by themselves, the compiler doesn't know what that is.
Was This Post Helpful? 0
  • +
  • -

#10 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Help with link list errors

Posted 10 November 2008 - 10:37 PM

Change the return type of main to int as opposed to void.
also, because add_node_at_end(), delete_start_node(), delete_end_node(), move_current_on(), and move_current_back() belong to a class they need to called by an object of that class. You're trying to call them like normal functions.
Network net;
net.delete_start_node();


Was This Post Helpful? 0
  • +
  • -

#11 quatre432  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 08

Re: Help with link list errors

Posted 10 November 2008 - 10:39 PM

View PostKYA, on 10 Nov, 2008 - 09:32 PM, said:

Your pointer must call those methods since it points to the Network class. you can't call them by themselves, the compiler doesn't know what that is.



yah i no i been trying that only thing is when i try to get a pointer to declare it only gives me the options for class network node and not class network and some those options in class network so still confusing lol, sorry if i sound like a noob i been working on this program for about a week now
Was This Post Helpful? 0
  • +
  • -

#12 quatre432  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 08

Re: Help with link list errors

Posted 10 November 2008 - 10:49 PM

View PostGWatt, on 10 Nov, 2008 - 09:37 PM, said:

Change the return type of main to int as opposed to void.
also, because add_node_at_end(), delete_start_node(), delete_end_node(), move_current_on(), and move_current_back() belong to a class they need to called by an object of that class. You're trying to call them like normal functions.
Network net;
net.delete_start_node();


ok did as you said got my errors back to the orginal three this is them
.\main.cpp(6) : error C2628: 'Network' followed by 'int' is illegal (did you forget a ';'?)
.\main.cpp(6) : error C3874: return type of 'main' should be 'int' instead of 'Network'
.\Network.cpp(4) : error C2143: syntax error : missing ';' before 'using'

this is my updated main
#include <iostream>
#include "Network.h"
#include "Networknode.h"
using namespace std;
struct node
  {  char idnum[20];
	 char ipad[20];		  
	 char os[20];	 
	 struct node *nxt;
  };

node *start_ptr = NULL;
node *current;		 
int option = 0;



void Network::delete_start_node()
   { node *temp;
	 temp = start_ptr;
	 start_ptr = start_ptr->nxt;
	 delete temp;
   }

void Network::delete_end_node()
   { node *temp1, *temp2;
	 if (start_ptr == NULL)
		  cout << "The list is empty!" << endl;
	 else
		{ temp1 = start_ptr;
		  if (temp1->nxt == NULL)
			 { delete temp1;
			   start_ptr = NULL;
			 }
		  else
			 { while (temp1->nxt != NULL)
				{ temp2 = temp1;
				  temp1 = temp1->nxt;
				}
			   delete temp1;
			   temp2->nxt = NULL;
			 }
		}
   }

void Network::move_current_on ()
   { if (current->nxt == NULL)
	  cout << "You are at the end of the list." << endl;
	 else
	  current = current->nxt;
   }

void Network::move_current_back ()
   { if (current == start_ptr)
	  cout << "You are at the start of the list" << endl;
	 else
	  { node *previous;	 // Declare the pointer
		previous = start_ptr;

		while (previous->nxt != current)
		  { previous = previous->nxt;
		  }
		current = previous;
	  }
   }


Was This Post Helpful? 0
  • +
  • -

#13 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Help with link list errors

Posted 10 November 2008 - 11:09 PM

Can you post the latest version of Network.cpp. It seems the problem is originating there.

as well as the latest Network.h?

This post has been edited by GWatt: 10 November 2008 - 11:11 PM

Was This Post Helpful? 0
  • +
  • -

#14 quatre432  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-November 08

Re: Help with link list errors

Posted 10 November 2008 - 11:20 PM

View PostGWatt, on 10 Nov, 2008 - 10:09 PM, said:

Can you post the latest version of Network.cpp. It seems the problem is originating there.

as well as the latest Network.h?

#include <iostream>
#include "Network.h"
#include "Networknode.h"
using namespace std;
struct node
  {  char idnum[20];
	 char ipad[20];		  
	 char os[20];	 
	 struct node *nxt;
  };

node *start_ptr = NULL;
node *current;		 
int option = 0;



void Network::delete_start_node()
   { node *temp;
	 temp = start_ptr;
	 start_ptr = start_ptr->nxt;
	 delete temp;
   }

void Network::delete_end_node()
   { node *temp1, *temp2;
	 if (start_ptr == NULL)
		  cout << "The list is empty!" << endl;
	 else
		{ temp1 = start_ptr;
		  if (temp1->nxt == NULL)
			 { delete temp1;
			   start_ptr = NULL;
			 }
		  else
			 { while (temp1->nxt != NULL)
				{ temp2 = temp1;
				  temp1 = temp1->nxt;
				}
			   delete temp1;
			   temp2->nxt = NULL;
			 }
		}
   }

void Network::move_current_on ()
   { if (current->nxt == NULL)
	  cout << "You are at the end of the list." << endl;
	 else
	  current = current->nxt;
   }

void Network::move_current_back ()
   { if (current == start_ptr)
	  cout << "You are at the start of the list" << endl;
	 else
	  { node *previous;	 // Declare the pointer
		previous = start_ptr;

		while (previous->nxt != current)
		  { previous = previous->nxt;
		  }
		current = previous;
	  }
   };


that network cpp
and
#ifndef NETWORK
#define NETWORK

#include <iostream>
#include "Networknode.h"
using namespace std;

class Network{
public:
void display_list();
void delete_start_node();
void delete_end_node();
void move_current_on ();
void move_current_back ();
};
#endif


Was This Post Helpful? 0
  • +
  • -

#15 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,079
  • Joined: 01-December 05

Re: Help with link list errors

Posted 10 November 2008 - 11:28 PM

I'm noticing in your Network.cpp that you haven't yet fixed the all of you struct object declarations
struct node
  {  char idnum[20];
     char ipad[20];          
     char os[20];    
     struct node *nxt;
  };

node *start_ptr = NULL;  // need to put 'struct' before 'node'
node *current;  // same here, and anywhere else you declare a node obejct.



I'm not seeing anything else that jumps out at me. Although I would say that any instance variables you want should be declared in your class declaration. Like start_ptr, and current, and option. Also, I would move the struct definition to the Network.h file too.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2