C ++ error: expected type-specifier

When I try to compile this simple linked list testing program using the command

g++ -o SLLtest SLLtester.cpp intSLList.o

      

I am getting the error:

SLLtester.cpp: In functionint main()’:
SLLtester.cpp:4:27: error: expected type-specifier
SLLtester.cpp:4:27: error: cannot convertint*’ tointSLList*’ in initialization
SLLtester.cpp:4:27: error: expected ‘,’ or ‘;’

      

I'm missing something simple, but I'm not sure what. Compiling the header and linked list definitions without issue. Three files are included.

//intSLList.hh
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST

class intSLList {
public:
 intSLList(){head=tail=0;}
 void Print();
 void AddToHead(int);
 void AddToTail(int);
 int RemoveFromHead();
 int RemoveFromTail();

protected:
 struct Node {
  int info;
  Node *next;
  Node(int e1, Node *ptr = 0) {info = e1; next = ptr;}
 } *head, *tail, *tmp;
 int e1;
};

#endif

      

And definitions:

//intSLList.cpp
#include "intSLList.hh"
#include <iostream>

void intSLList::AddToHead(int e1){
 head = new Node(e1,head);
 if (!tail)
  tail = head;
}

void intSLList::AddToTail(int e1){
 if (tail) {
  tail->next = new Node(e1);
  tail = tail->next;
 }
 else 
  head = tail = new Node(e1);
}

int intSLList::RemoveFromHead(){
 if (head){
  e1 = head->info;
  tmp = head;
  if (head == tail)
   head = tail = 0;
  else 
   head = head->next;
 delete tmp;
 return e1;
 }
 else
  return 0;    
}

int intSLList::RemoveFromTail(){
 if (tail){
  e1 = tail->info;
  if (head == tail){
   delete head;
   head = tail = 0;
  }
  else {
   for ( tmp = head; tmp->next != tail; tmp = tmp->next);
   delete tail;
   tail = tmp;
   tail->next = 0;
  } 
  return e1;
 }
 else return 0;
}

void intSLList::Print(){
 tmp = head;
 while( tmp != tail ){
  std::cout << tmp->info << std::endl;
  tmp = tmp->next;
 }
}

      

And finally, the main function:

#include "intSLList.hh"

int main(){
 intSLList* mylist = new intSLList::intSLList();
 for ( int i = 0; i < 10; i++ ){   
  mylist->AddToTail(i);    
 }
 mylist->Print();
}

      

Thanks for the help.

+3


source to share


2 answers


intSLList* mylist = new intSLList::intSLList();

      

It is not right. When we write new intSLList()

, we are not "calling the constructor" - just by naming the type - and hence the fully qualified constructor name (as intSLList::intSLList

) is incorrect.

So:



intSLList* mylist = new intSLList();

      

You don't need dynamic placement anyway:

#include "intSLList.hh"

int main()
{
   intSLList mylist;

   for (int i = 0; i < 10; i++) {
      mylist.AddToTail(i);    
   }

   mylist.Print();
}

      

+3


source


You are trying to call the constructor as a function. The constructor will be called automatically when the object is allocated, so change to

intSLList mylist;

      



There is no need for pointers or dynamic allocation at all for this.

+1


source







All Articles