Lost again...Just trying to "tie" a header file to a .cpp file, but I keep getting a 'need ;' before '{' ..... in my .cpp file at LIST::LIST()
{
x = 0;
}
Here are both files that I am fooling with:

#ifndef LISTS_H
#define LISTS_H
#include <iostream>
using namespace std;
int x;
typedef int dataItem;
class LIST
{
public:
	LIST();
	
	int getlength() const;

private:

	int size;

};
#endif


#include "LISTS.h"

int main()

{

	LIST::LIST()
	{
		x=0;
	}

Thanks for any help please!!>>>>

Recommended Answers

All 3 Replies

That using namespace std; is a bad idea in a header file. Keep it in cpp files.

The problem on line 28 is that C++ doesn't permit you to define sub-functions. Ideally, you should have a file named LISTS.cpp that looks like this:

#include "LISTS.h"

LIST::LIST()
{
  // my constructor here
}

...

Then in your main program (which I'll call "myprog.cpp"):

#include "LISTS.h"
using namespace std;

int main()
{
  LIST::LIST aList;  // Create a new list object named 'aList'

  ...
}

Compile normally:

D:\foo> gcc myprog.cpp LISTS.cpp

Hope this helps.

That using namespace std; is a bad idea in a header file. Keep it in cpp files.

The problem on line 28 is that C++ doesn't permit you to define sub-functions. Ideally, you should have a file named LISTS.cpp that looks like this:

#include "LISTS.h"

LIST::LIST()
{
  // my constructor here
}

...

Then in your main program (which I'll call "myprog.cpp"):

#include "LISTS.h"
using namespace std;

int main()
{
  LIST::LIST aList;  // Create a new list object named 'aList'

  ...
}

Compile normally:


Hope this helps.

I'm a little confused now...We worked with the following 2 files in class the other day that our professor wrote; and I was trying to mimic only the basics; perhaps it won't work because of the large difference between his code and my code; nevertheless; I would think that his way should work when I apply it to a simple code structure??? Anyway, would you mind just giving it a once over to help me see what's happening here???
Thanks again::

// *********************************************************
// Header file ListA.h for the ADT list
// Array-based implementation
//   2004 0119 sbw added #ifnedef and namespace...
// *********************************************************
// Must define ListItemType and MAX_LIST before compilation

#ifndef LISTA_H
#define LISTA_H

const int MAX_LIST = 10;  //MODIFY!!!
#include <iostream>
using namespace std;

typedef int ListItemType;  //MODIFY!!!

class List
{
public:
   List(); // default constructor
           // destructor is supplied by compiler

// list operations:
   bool isEmpty() const;
   // Determines whether a list is empty.
   // Precondition: None.
   // Postcondition: Returns true if the list is empty;
   // otherwise returns false.

   int getLength() const;
   // Determines the length of a list.
   // Precondition: None.
   // Postcondition: Returns the number of items
   // that are currently in the list.

   void insert(int index, ListItemType newItem,
               bool& success);
   // Inserts an item into the list at position index.
   // Precondition: index indicates the position at which
   // the item should be inserted in the list.
   // Postcondition: If insertion is successful, newItem is
   // at position index in the list, and other items are
   // renumbered accordingly, and success is true;
   // otherwise success is false.
   // Note: Insertion will not be successful if
   // index < 1 or index > getLength()+1.

   void remove(int index, bool& success);
   // Deletes an item from the list at a given position.
   // Precondition: index indicates where the deletion
   // should occur.
   // Postcondition: If 1 <= index <= getLength(),
   // the item at position index in the list is
   // deleted, other items are renumbered accordingly,
   // and success is true; otherwise success is false.

   void retrieve(int index, ListItemType& dataItem,
                 bool& success) const;
   // Retrieves a list item by position.
   // Precondition: index is the number of the item to
   // be retrieved.
   // Postcondition: If 1 <= index <= getLength(),
   // dataItem is the value of the desired item and
   // success is true; otherwise success is false.

void display();

private:
   ListItemType items[MAX_LIST]; // array of list items
   int          size;            // number of items in list

   int translate(int index) const;
   // Converts the position of an item in a list to the
   // correct index within its array representation.
}; // end List class

#endif
// End of header file.


// *********************************************************
// Implementation file ListA.cpp for the ADT list
// Array-based implementation   //Carrano 3rd
// *********************************************************

#include "ListA.h" //header file

List::List() : size(0)
{
} // end default constructor

bool List::isEmpty() const
{
   return bool(size == 0);
} // end isEmpty

int List::getLength() const
{
   return size;
} // end getLength

void List::insert(int index, ListItemType newItem,
                  bool& success)
{
   success = bool( (index >= 1) &&
                   (index <= size+1) &&
                   (size < MAX_LIST) );
   if (success)
   {  // make room for new item by shifting all items at
      // positions >= index toward the end of the
      // list (no shift if index == size+1)
      for (int pos = size; pos >= index; --pos)
         items[translate(pos+1)] = items[translate(pos)];

      // insert new item
      items[translate(index)] = newItem;
      ++size; // increase the size of the list by one
   } // end if
} // end insert

void List::remove(int index, bool& success)
{
   success = bool( (index >= 1) && (index <= size) );

   if (success)
   {  // delete item by shifting all items at positions >
      // index toward the beginning of the list
      // (no shift if index == size)
      for (int fromPosition = index+1;
               fromPosition <= size; ++fromPosition)
         items[translate(fromPosition-1)] =
                             items[translate(fromPosition)];
      --size; // decrease the size of the list by one
   } // end if
} // end remove

void List::retrieve(int index, ListItemType& dataItem,
                    bool& success) const
{
   success = bool( (index >= 1) &&
                   (index <= size) );

   if (success)
      dataItem = items[translate(index)];
} // end retrieve

int List::translate(int index) const
{
   return index-1;
} // end translate


void List::display()
{

	ListItemType data;
	bool s;

	cout <<  " ---List contains " << getLength() << "items ---" << endl;
	for(int i=1; i<=getLength(); i++)
	{
		retrieve(i, data, s);
		cout << data << endl;
	}
		cout << "---------end of list______" << endl;
}	


	




// End of implementation file.

Neither of those files contain main(). They only describe a single object. (The header describes what it looks like and the cpp describes how it works.) That is correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.