Stupid Dev-C++

I’m just going to try my luck here as I cannot find a solution for my problem and I don’t want to look for a c++ forum now.

I’m doing C++ at uni and our book is Data Structures Using C++ by D.S. Malik. But our lecturers decided that we should use Dev-C++ with MingW and the code in the book isn’t certified to work on that.
These are the 3 files directly from book’s source files on the CD:

linkedList.h:


#ifndef H_LinkedListType
#define H_LinkedListType

#include <iostream>
#include <cassert>
using namespace std;

template <class Type>
struct nodeType
{
	Type info;
	nodeType<Type> *link;
};

template<class Type>
class linkedListType
{
	friend ostream& operator<<(ostream&, const linkedListType<Type>&);

public:
    const linkedListType<Type>& operator=
          			      (const linkedListType<Type>&); 
    void initializeList(); 
    bool isEmptyList();

	int length();
    void destroyList();
    Type front(); 
    Type back(); 

   bool search(const Type& searchItem);

    void insertFirst(const Type& newItem);

    void insertLast(const Type& newItem);

    void deleteNode(const Type& deleteItem);

    linkedListType(); 

    linkedListType(const linkedListType<Type>& otherList); 

    ~linkedListType();   

protected:
    int count;		//variable to store the number of 
 					//elements in the list
    nodeType<Type> *first; //pointer to the first node of 
                           //the list
    nodeType<Type> *last;  //pointer to the last node of 
                           //the list 
private:
    void copyList(const linkedListType<Type>& otherList); 
};

template<class Type>
bool linkedListType<Type>::isEmptyList()
{
	return(first == NULL);
}

template<class Type>
linkedListType<Type>::linkedListType() // default constructor
{
	first = NULL;
	last = NULL;
	count = 0;
}

template<class Type>
void linkedListType<Type>::destroyList()
{
	nodeType<Type> *temp;   //pointer to deallocate the memory 
							//occupied by the node
	while(first != NULL)    //while there are nodes in the list
	{
	   temp = first;        //set temp to the current node
	   first = first->link; //advance first to the next node
	   delete temp;         //deallocate memory occupied by temp
	}

	last = NULL;	//initialize last to NULL; first has already
                   //been set to NULL by the while loop
 	count = 0;
}

	
template<class Type>
void linkedListType<Type>::initializeList()
{
	destroyList(); //if the list has any nodes, delete them
}

template<class Type>
int linkedListType<Type>::length()
{
 	return count;
}  // end length

template<class Type>
Type linkedListType<Type>::front()
{   
    assert(first != NULL);
   	return first->info; //return the info of the first node	
}//end front


template<class Type>
Type linkedListType<Type>::back()
{   
	 assert(last != NULL);
   	 return last->info; //return the info of the first node	
}//end back

template<class Type>
bool linkedListType<Type>::search(const Type& searchItem)
{
    nodeType<Type> *current; //pointer to traverse the list
    bool found;

    current = first; //set current to point to the 
                     //first node in the list
    found = false;   //set found to false

    while(current != NULL && !found)		//search the list
        if(current->info == searchItem)     //the item is found
           found = true;
        else
           current = current->link; //make current point 
                                    //to the next node
     
     return found;
}//end search

template<class Type>
void linkedListType<Type>::insertFirst(const Type& newItem)
{
	nodeType<Type> *newNode; //pointer to create the new node

	newNode = new nodeType<Type>; //create the new node

	assert(newNode != NULL);	//If unable to allocate memory, 
 								//terminate the program

	newNode->info = newItem; 	   //store the new item in the node
	newNode->link = first;        //insert newNode before first
	first = newNode;              //make first point to the 
                                 //actual first node
	count++; 			   //increment count

	if(last == NULL)   //if the list was empty, newNode is also 
                      //the last node in the list
		last = newNode;
}

template<class Type>
void linkedListType<Type>::insertLast(const Type& newItem)
{
	nodeType<Type> *newNode; //pointer to create the new node

	newNode = new nodeType<Type>; //create the new node

	assert(newNode != NULL);	//If unable to allocate memory, 
 							    //terminate the program

	newNode->info = newItem;      //store the new item in the node
	newNode->link = NULL;         //set the link field of newNode
         						//to NULL

	if(first == NULL)	//if the list is empty, newNode is 
     					//both the first and last node
	{
		first = newNode;
		last = newNode;
		count++;		//increment count
	}
	else			//the list is not empty, insert newNode after last
	{
		last->link = newNode; //insert newNode after last
		last = newNode;   //make last point to the actual last node
		count++;		//increment count
	}
}//end insertLast

template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
	nodeType<Type> *current; //pointer to traverse the list
	nodeType<Type> *trailCurrent; //pointer just before current
	bool found;

	if(first == NULL)    //Case 1; list is empty. 
		cerr<<"Can not delete from an empty list.\n";
	else
	{
		if(first->info == deleteItem) //Case 2 
		{
			current = first;
			first = first->link;
			count--;
			if(first == NULL)    //list has only one node
				last = NULL;
			delete current;
		}
		else  //search the list for the node with the given info
		{
			found = false;
			trailCurrent = first;   //set trailCurrent to point to
                                 //the first node
			current = first->link;  //set current to point to the 
    			   //second node
	
			while(current != NULL && !found)
			{
  				if(current->info != deleteItem) 
				{
					trailCurrent = current;
					current = current->link;
				}
				else
					found = true;
			} // end while

			if(found) //Case 3; if found, delete the node
			{
				trailCurrent->link = current->link;
				count--;

				if(last == current)      //node to be deleted was 
                                     //the last node
					last = trailCurrent;  //update the value of last

				delete current;  //delete the node from the list
			}
			else
				cout<<"Item to be deleted is not in the list."<<endl;
		} //end else
	} //end else
} //end deleteNode


	//Overloading the stream insertion operator
template<class Type>
ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
{
	nodeType<Type> *current; //pointer to traverse the list

	current = list.first;   //set current so that it points to 
					   //the first node
	while(current != NULL) //while more data to print
	{
	   osObject<<current->info<<" ";
	   current = current->link;
	}

	return osObject;
}

template<class Type>
linkedListType<Type>::~linkedListType() // destructor
{
	destroyList(); 
}//end destructor


template<class Type>
void linkedListType<Type>::copyList
            	      (const linkedListType<Type>& otherList) 
{
   nodeType<Type> *newNode; //pointer to create a node
   nodeType<Type> *current; //pointer to traverse the list

   if(first != NULL)	//if the list is nonempty, make it empty
	  destroyList();

   if(otherList.first == NULL) //otherList is empty
   {
		first = NULL;
		last = NULL;
 		count = 0;
   }
   else
   {
		current = otherList.first;  //current points to the 
									//list to be copied
		count = otherList.count;

			//copy the first node
		first = new nodeType<Type>;  //create the node

 		assert(first != NULL);

		first->info = current->info; //copy the info
		first->link = NULL;  	     //set the link field of 
									 //the node to NULL
		last = first;    		     //make last point to the 
            						 //first node
		current = current->link;     //make current point to  
           							 //the next node

			//copy the remaining list
		while(current != NULL)
		{
			newNode = new nodeType<Type>;  //create a node

			assert(newNode!= NULL);

			newNode->info = current->info;	//copy the info
			newNode->link = NULL;   	    //set the link of 
                                        //newNode to NULL
			last->link = newNode; 		//attach newNode after last
			last = newNode;   			//make last point to
										//the actual last node
			current = current->link;	//make current point to
       									//the next node
		}//end while
	}//end else
}//end copyList


	//copy constructor
template<class Type>
linkedListType<Type>::linkedListType
            	      (const linkedListType<Type>& otherList) 
{
	first = NULL;

	copyList(otherList);
	
}//end copy constructor

	//overload the assignment operator
template<class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
   	 	 		(const linkedListType<Type>& otherList)
{ 
	if(this != &otherList) //avoid self-copy
		copyList(otherList);

	return *this; 
}

#endif

orderedLinkedList.h:


#ifndef H_orderedLinkedListType
#define H_orderedLinkedListType

#include <iostream>
#include <cassert>

#include "linkedList.h"

using namespace std;

template<class Type>
class orderedLinkedListType: public linkedListType<Type>
{
public:
   bool search(const Type& searchItem);

   void insertNode(const Type& newItem);

   void deleteNode(const Type& deleteItem);
};



template<class Type>
bool orderedLinkedListType<Type>::search(const Type& searchItem)
{
    bool found;
    nodeType<Type> *current; //pointer to traverse the list
	
    found = false;    //initialize found to false
    current = first;  //start the search at the first node

    while(current != NULL && !found)
       if(current->info >= searchItem)
          found = true;
       else
          current = current->link;

      if(found)       
         found = (current->info == searchItem); //test for equality

    return found;
}//end search


template<class Type>
void orderedLinkedListType<Type>::insertNode(const Type& newItem)
{
	nodeType<Type> *current; //pointer to traverse the list
	nodeType<Type> *trailCurrent; //pointer just before current
	nodeType<Type> *newNode;  //pointer to create a node

	bool  found;

	newNode = new nodeType<Type>; //create the node
 	assert(newNode != NULL);

	newNode->info = newItem;   //store newitem in the node
	newNode->link = NULL;      //set the link field of the node 
	                           //to NULL

	if(first == NULL)  //Case 1	
	{	
	   first = newNode;
	   count++;
 	}
	else
	{
	   current = first;
	   found = false;

	   while(current != NULL && !found) //search the list
			if(current->info >= newItem)
				found = true;
     		else
			{
				trailCurrent = current;
				current = current->link;
			}
		  
	   if(current == first)  	//Case 2
	   {
			newNode->link = first;
			first = newNode;
 			count++;
	   }
	   else				//Case 3
	   {
			trailCurrent->link = newNode;
			newNode->link = current;
 			count++;
	   }
	}//end else
}//end insertNode

template<class Type>
void orderedLinkedListType<Type>::deleteNode
                                    (const Type& deleteItem)
{
	nodeType<Type> *current; //pointer to traverse the list
	nodeType<Type> *trailCurrent; //pointer just before current
	bool found;

	if(first == NULL) //Case 1
		cerr<<"Cannot delete from an empty list."<<endl;
	else
	{
		current = first;
		found = false;

		while(current != NULL && !found)  //search the list
			if(current->info >= deleteItem)
				found = true;
			else
			{
				trailCurrent = current;
				current = current->link;
			}
		
		if(current == NULL)   //Case 4
			cout<<"The item to be deleted is not in the list."
			    <<endl;
		else
 			if(current->info == deleteItem) //item to be deleted  
											//is in the list
			{
  				if(first == current) 		//Case 2
				{
					first = first->link;

					delete current;
				}
				else     				//Case 3
				{
					trailCurrent->link = current->link;
					delete current;
				}
 				count--;
			}
			else  					//Case 4
				cout<<"The item to be deleted is not in the list."
				    <<endl;
	}
} //end deleteNode

#endif

testProgLinkedList.cpp:


//Program to test various operations on an ordered linked list

#include <iostream> 
#include "orderedLinkedList.h"

using namespace std;

int main()
{
     orderedLinkedListType<int> list1, list2;	//Line 1
     int num;									//Line 2

     cout<<"Line 3: Enter integers ending with -999"
         <<endl;								//Line 3
     cin>>num;									//Line 4

     while(num != -999)							//Line 5
     {
		list1.insertNode(num);					//Line 6
		cin>>num;								//Line 7
     }

     cout<<endl;								//Line 8

     cout<<"Line 9: List 1: "<<list1<<endl;		//Line 9

     list2 = list1; //test the assignment operator; Line 10

     cout<<"Line 11: List 2: "<<list2<<endl;	//Line 11

     cout<<"Line 12: Enter the number to be "
         <<"deleted: ";							//Line 12
     cin>>num;									//Line 13
     cout<<endl;	                            //Line 14

     list2.deleteNode(num);						//Line 15

     cout<<"Line 16: After deleting the node, "
         <<"List 2: "<<endl<<list2
         <<endl;								//Line 16

     return 0;					
}


So the first error I get has to do with the operator<< overloading. So we manage to fix this by adding <> after the operator<<. Or I can just disable the warning. But then I get the following and nobody seems to know what the hell to do.


4 C:\unisa\orderedLinkedListType\testProgLinkedList.cpp
In file included from testProgLinkedList.cpp
orderedLinkedList.h C:\unisa\orderedLinkedListType\orderedLinkedList.h
In member function `bool orderedLinkedListType<Type>::search(const Type&)':
 error C:\unisa\orderedLinkedListType\orderedLinkedList.h:43
`first' undeclared (first use this function)
 
(Each undeclared identifier is reported only once for each function it appears in.)
orderedLinkedList.h C:\unisa\orderedLinkedListType\orderedLinkedList.h
In member function `void orderedLinkedListType<Type>::insertNode(const Type&)':
 error C:\unisa\orderedLinkedListType\orderedLinkedList.h:74
`first' undeclared (first use this function)
 error C:\unisa\orderedLinkedListType\orderedLinkedList.h:77
no post-increment operator for type
 error C:\unisa\orderedLinkedListType\orderedLinkedList.h:97
no post-increment operator for type
 error C:\unisa\orderedLinkedListType\orderedLinkedList.h:103
no post-increment operator for type
orderedLinkedList.h C:\unisa\orderedLinkedListType\orderedLinkedList.h
In member function `void orderedLinkedListType<Type>::deleteNode(const Type&)':
 error C:\unisa\orderedLinkedListType\orderedLinkedList.h:116
`first' undeclared (first use this function)
 error C:\unisa\orderedLinkedListType\orderedLinkedList.h:150
no post-decrement operator for type
 C:\unisa\orderedLinkedListType\Makefile.win
[Build Error] exe: *** [testProgLinkedList.o] Error 1

I can see that the extending class cannot find the protected members but I don’t know what to do. All the files are in a project and I’ve done extending in projects before and never got this problem before.

Any help, maybe?

Hmm, my template knowledge is rusty, but that looks fine to me. Have you tried it with a different compiler? Dev-C++ is pretty ancient now, but I don’t know how up to date MingW is these days. You might want to check to see how old it is, most compilers pre-2000 are lousy with anything other than the most simple templated code.

No i haven’t. The dev-cpp we use is version 4.9.8.3 which i think came out after 2000. And the mingw we use is of gcc 3.4.2.

I have a watcom compiler that came with maple and a borland command line thing. But the book says the code works with Microsoft C++.net and MetroWorks COdeWarrior.

The operator<<'s problem has to do with c++'s stupidity in general. The problem I have is that the extending class doesn’t wwant to see the extended class’s protected variables. But if I do extensions without templates then the variables are visible.

And the error messages are near useless.

All of these unneccessery troubles with c++ makes me glad java exists.

After fixing the operator<< friend declaration it compiles without modifications on VS 2005 Express. I’m going to take the easy route out and blame the compiler. :stuck_out_tongue:

Also, “using” in headers is really bad, especially wholesale “using namespace std” like you’ve got, as it means everyone who includes your linked list files gets the using declaration as well. Restrict it’s usage to cpp files or keep it within a function’s scope.

And yes, compiler errors about templated code are usually pretty hard to decipher, but they do make sense if you mentally split them up into the subsections and figure out which each bit is.

This code comes straight from the book. So I should change the code by prefixing std:: to whatever needs it?

I removed all references to Type and template and made everything be an int and now everything works. But it doesn’t solve my problem.

Prefixing by std:: is my preferred way, but if you like you can just put “using namespace std” or “using std::cout” at the top of functions and it’ll be limited to just within the function itself (ie. not people who also include the same file).

IMO, at least if you’re on Windows, your best bet is to bite the bullet, go to http://www.microsoft.com/express/vc/, register, and use Visual C++ 2008 Express. Pretty much every serious C++ developer uses Visual Studio, except for the truly adventurous, and most projects/examples that you see will have been developed and tested in Visual Studio, so you’re going to be much better supported there in general. College is probably the one exception to this rule, since a lot of profs are pretty serious about always choosing unencumbered/crippled tools, but even there you’re probably going to have fewer headaches overall, even with the express edition of Visual C++.

If you run Linux or really don’t want to do the whole Microsoft thing, some people suggest Code::Blocks as an alternative; I’ve never used it, so I can’t comment. On OS X, you’re plain screwed - my advice there is, sadly, to boot Windows when you code C++ (that’s what I do). XCode can get the job done, minimally, but it’s kind of annoying to use if you’re not doing Objective C, it’s slow to compile and test large projects, and nobody ever provides XCode project files for projects anyways, so you’re either stuck figuring out how to set up the projects yourself from source every time or praying that there is a makefile that actually works.

One of the best things Java has going for it is the high bar set for free cross-platform development tools - the early successes of Eclipse and Netbeans ensured that Microsoft couldn’t lock development efforts down in Windows, and that’s a great thing. It’s just too bad that those don’t function very well as C++ development tools - even if you buy (or “liberate,” not that I endorse any such action) the non-crippleware (and expensive!) versions of Visual Studio, you’re going to miss a lot of the ease of development that comes standard with Eclipse/Netbeans/IntelliJ (lack of decent Subversion support really pisses me off, in particular).

It’s too bad, too, because C++ is actually a fairly pleasant language apart from the dismal IDE landscape…

Thanks for the advice. I have CodeBlocks and I don’t have problems with that ide, it even supports multiple c++ compilers but then I still need to choose the compiler. We use MinGW 5.0.0.0 with GCC 3.4.2. But when I tried that compiler with Blocks I got the same errors. So then I tried Borland 5.5 command line tools but it requires changes to the includes and a bunch of other syntax changes that I do not know. And right now I dont have the need nor time to learn that.

I’m not doing C++ to be a serious developer right now. I’m doing it mostly because university wants me to (we use c++ to learn the mechanics of data structures). Unfortunately Java is only part of my uni’s OO course so I won’t be using it much. I’m also doing OpenGL and Qt for C++ next year. I doubt if I will need to use C++ in the next few years for the advantages it has over java. Heck, if I really wanted so much speed increase over java then I could even use assembler (which I have to do too).

Except for the speed increase of c++ (which I never notice because we’ve never had to do anything of such a scale to notice a difference) I see no reason why I would need to use c++ in the near future.

It does appear to be a bug in the gcc compiler(3.4.2) as this minimal code fails (compiles fine under Visual C++ 2005).


template<class T>
class A 
{
protected:
    T val;
};

template<class T>
class B : public A<T>
{
public:
	T getVal() { return val; }
};

int main()
{
	B<int> b;
        return b.getVal();
}

As other said your best best, as you are on windows is to use Visual C++ express edition, it is free and a nice IDE for C++. The mingw team fell behind in the porting of more recent versions of gcc for a while. Although there have been signs of life in the past few months with a beta version of gcc 4.0 available on the web.

Yeah, thats the problem. It wont find protected member variables when templates are used.

Ok, I guess I will have to get VC++ then.

I noticed the beta version, but if the problems I get here have to do with gcc then that means linux programmers get that too? What other options are available on linux then?

I only use linux fairly minimally but I imagine linux users use a more up to date version of gcc something like version 4.2 or 4.3. These version probably have improved template handling abilities, version 3.4.2 of gcc is fairly old. A lot of work the work that has been done on all C++ compilers in the past few years has been about getting templates to work as they should, so programmers can use libraries such as those in boost that use templates extensively.

;D ;D ;D
I found a way to fix those stupid errors.
Adding

linkedListType<Type>::

before every reference to a member variable that has an error seems to fix this.

But I’m not exactly sure why this works. Wherever a template class is used we always specify whatever it is, but in the extending class’s methods there is no mention of what kind of linkedListType<????> we are using so it cannot find the variables because they don’t exist when it doesn’t know what kind of linkedListType it should take them from. I don’t know.


template<class T>
class A 
{
protected:
    T val;
};

template<class T>
class B : public A<T>
{
public:
	T getVal() { return A<T>::val; }
};

int main()
{
	B<int> b;
        return b.getVal();
}

Would then fix your code.

That does fix it but I am not sure why it is needed! I asked a couple of people at work who do much more C++ template code than me and they are not sure why either, which made me feel a little less stupid :slight_smile:

Incidentally I tried it on the 4.3 beta of mingw and it complains if you do not include the A:: part. I guess to get to the bottom of this it needs posting on a C++ forum where a template “Guru” would be able to give his insight.

Here’s the “official” fix I received from someone this morning:

Thanks :slight_smile:

It was bugging me I had been hitting google for the answers and had just read question number 2 on this page http://womble.decadentplace.org.uk/c++/template-faq.html#type-syntax-error

which effectively says the same thing.

I wonder if I will ever upload all of C++ into my brain!

With respect to C++ IDE’s: Netbeans C/C++ pack is REALLY damn nice now. You can easily set it up to use Mingw (google for ‘netbeans mingw’). Netbeans is so awesome it gives me an erection while coding. Not really, but it is very, very nice.