Syntax error in templated linked list

// I didn't want to change the original post because it might confuse people. My new question is below.

I am having trouble with what I think is a memory leak with my pointers. When the code is run the list is printed as expected but then the program ends with an error "kernel.exe has encountered a problem and needs to close. We are sorry for the inconvenience." (in Windows XP)
Here is my code:

//kernelmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef _iostream_included_
#define _iostream_included_
#include <iostream>
#endif
#ifndef _kernel_included_
#define _kernel_included_
#include "kernel.h"
#endif
#ifndef _list_included_
#define _list_included_
#include "list.h"
#endif

#define version 0.1

using namespace std;

int main() {
    cList slist(6);
    slist.newNode(7);
    slist.newNode(89);            // Values to test the list with
    slist.newNode(54);
    slist.newNode(52);
    slist.print();  // this member function of cList:: seems to be the problem
    std::cin.get();  
    return 0;
}


---------------------------------------
//kernel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class cKernel {
      public:
             cKernel(int input) { data = input; }
             int print() { return data; }
             void setData(int input) { data = input; }
             void setNext(cKernel * node) { next = node; }
	     bool isset() { return set; }
             cKernel * returnNext();
      private:
       	      bool set;        // keep track if *next is set
              int data;        // data that the node holds
              cKernel *next;   // pointer to next node
              };
              
              cKernel * cKernel::returnNext() {
                      if(next) {
                               return next;
                               }
                      else {
                           cKernel * tmp = new cKernel(-1);   // supposed to be 
// a error object so I can tell *next is not set.  Probably a bad way to do it //though...
                           return tmp;
                           }
                           }

---------------------------------------
//list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef _iostream_included_
#define _iostream_included_
#include <iostream>
#endif
#ifndef _kernel_included_
#define _kernel_included_
#include "kernel.h"
#endif
class cList {
      public:
             cList(int data);
	     ~cList();
             void print();
             void newNode(int data);
      private:
              cKernel *next;
              cKernel *head;
              };
      
      cList::cList(int data) {
      next = new cKernel(data);
      head = new cKernel(-1);
      head->setNext(next);
      }   

	  cList::~cList() {
          delete head;   // this function needs some work to clean up pointers
          delete next;   // Any suggestion for how to delete all of them?
	  }

// This appears to be the problem function
      void cList::print() {
		   cKernel * tmp = head->returnNext();
		   int rep = 0;
		   while(tmp->isset() != 0)
		   {
		   rep++;
		   std::cout << rep << ": " << tmp->print() << std::endl;
		   if((tmp->returnNext())->isset())
		   {
		   tmp = tmp->returnNext();  //<--- I think the problem is here
		   }
		   }
		   }
      
      void cList::newNode(int data) {
          cKernel * tmp = new cKernel(data);
          next->setNext(tmp);
          next = tmp;
          }

----------------------------------------------


I just want a FIFO structure list. I think the problem is in list.h. Maybe I am just missing something obvious. I am also not sure how to accomplish deleting all the pointers that I have created. Thanks for the help.

stringplayer92

Last edited on
How to: Put code into your postings http://www.cplusplus.com/forum/articles/1624/

Fixed the formatting, any ideas on why the program exits abruptly?
I figured it out, I was just stupid and line 39 of list.h I made the if statement and forgot to include the else part of it to break from the while loop.
I solved the problem above and wanted to improve the above code to work with templates. This is my first real use of templates and I apparently made a syntax error. GCC returns these two errors: "26 C:\Dev-Cpp\projects\source\kernel\kernel.h expected constructor, destructor, or type conversion before '*' token" and "26 C:\Dev-Cpp\projects\source\kernel\kernel.h expected `;' before '*' token". Here is my code:

Error is in this header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// kernel.h
template <class myType>
class cKernel {
      public:
             cKernel(myType input) { data = input; set = false;}
             myType print() { return data; }
             void setData(myType input);
             void setNext(cKernel* node);
			 bool isset();
             cKernel * returnNext();
      private:
			  bool set;
              myType data;
              cKernel<myType> *next;
              };

template <class myType>
void cKernel<myType>::setData(myType input) {
	data = input;
}
template <class myType>
void cKernel<myType>::setNext(cKernel * node) {
	next = node;
	set = true;
}
template <class myType>
cKernel * cKernel<myType>::returnNext() {     //<-- Compiler says error in this line
     if(this->isset()) {  
           return next;
                       }
     else {
             return NULL;
          }
                       }
template <class myType>
bool cKernel<myType>::isset() {
	return set;
}


Just in case here is the rest of my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// list.h
#ifndef _iostream_included_
#define _iostream_included_
#include <iostream>
#endif
#ifndef _kernel_included_
#define _kernel_included_
#include "kernel.h"
#endif


	  template <class myType>
	  class cList {
      public:
             cList(myType data);
			 ~cList();
             void print();
             void newNode(myType data);
      private:
              cKernel<myType> *next;
              cKernel<myType> *head;
              };
      template <class myType>
      cList<myType>::cList(myType data) {
      next = new cKernel<myType>(data);
      head = new cKernel<myType>(-1);
      head->setNext(next);
      }   
	  template <class myType>
	  cList<myType>::~cList() {
		  delete head;
          delete next;
	  }
	  template <class myType>
      void cList<myType>::print() {
		   cKernel<myType> * tmp = head->returnNext();
		   int rep = 0;
		   while(1)
		   {
		   rep++;
		   std::cout << rep << ": " << tmp->print() << std::endl;
		   if(tmp->isset())
		   {
		   tmp = tmp->returnNext();
           }
           else 
           {
                break;
           }
           }
		   }
      template <class myType>
      void cList<myType>::newNode(myType data) {
          cKernel<myType> * tmp = new cKernel<myType>(data);
          next->setNext(tmp);
          next = tmp;
          }


This just tests the templated lists
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// kernelmain.cpp
#ifndef _iostream_included_
#define _iostream_included_
#include <iostream>
#endif
#ifndef _kernel_included_
#define _kernel_included_
#include "kernel.h"
#endif
#ifndef _list_included_
#define _list_included_
#include "list.h"
#endif

using namespace std;

int main() {
    int number;
    int rep = 0;
    cList<int> * slist;
    for(;;) {
            std::cout << "Input number (0 to exit)";
            std::cin >> number;
            if(number == 0)
            {
            break;
            }
            else {
                 if( rep == 0) {
                     slist = new cList<int>(number);
                     }
                 else {
                 slist->newNode(number);
                 }
                 }
                 rep++;
                 }
                 
    if(rep == 0) {
           std::cout << "Why didn't you enter any numbers?\n";
                 }
    else {
         slist->print();
         }    
    std::cin.get();  
    std::cin.get();
    return 0;
}

Should it be returning a cKernel<myType>, perhaps?
Last edited on
You were correct, I guess it has to be:
template <class myType>
cKernel<myType> * cKernel<myType>::returnNext()

not sure why, but hey it works!

thanks,
stringplayer92
Topic archived. No new replies allowed.