Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Stupid Dev-C++  (Read 3987 times)
0 Members and 1 Guest are viewing this topic.
Offline malberts

Junior Member





« Posted 2008-05-05 11:34:15 »

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:

In space no-one can hear you System.out.println()
Offline malberts

Junior Member





« Reply #1 - Posted 2008-05-05 11:34:55 »

linkedList.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  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
312  
313  
314  
315  
316  
317  
318  
319  
320  
321  
322  
323  
324  
325  
326  
327  
328  
329  
330  
331  
332  
333  
334  
335  
336  
337  
338  
339  
340  
341  
342  
343  
#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

In space no-one can hear you System.out.println()
Offline malberts

Junior Member





« Reply #2 - Posted 2008-05-05 11:35:25 »


orderedLinkedList.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  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
#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

In space no-one can hear you System.out.println()
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline malberts

Junior Member





« Reply #3 - Posted 2008-05-05 11:35:46 »


testProgLinkedList.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  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
//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;              
}

In space no-one can hear you System.out.println()
Offline malberts

Junior Member





« Reply #4 - Posted 2008-05-05 11:36:14 »


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.
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  
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?

In space no-one can hear you System.out.println()
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #5 - Posted 2008-05-05 12:10:58 »

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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline malberts

Junior Member





« Reply #6 - Posted 2008-05-05 12:27:28 »

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.

In space no-one can hear you System.out.println()
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #7 - Posted 2008-05-05 12:44:14 »

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. 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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline malberts

Junior Member





« Reply #8 - Posted 2008-05-05 12:55:05 »

Also, "using" in headers is really bad, especially wholesale "using namespace std"

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.

In space no-one can hear you System.out.println()
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #9 - Posted 2008-05-05 13:11:07 »

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

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).

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ewjordan

Junior Member





« Reply #10 - Posted 2008-05-05 18:47:19 »

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...
Offline malberts

Junior Member





« Reply #11 - Posted 2008-05-05 20:02:11 »

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.

In space no-one can hear you System.out.println()
Offline PaulReeves

Senior Newbie





« Reply #12 - Posted 2008-05-05 20:09:53 »

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).
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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.

Offline malberts

Junior Member





« Reply #13 - Posted 2008-05-05 20:14:51 »

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?

In space no-one can hear you System.out.println()
Offline PaulReeves

Senior Newbie





« Reply #14 - Posted 2008-05-05 21:13:28 »

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.

Offline malberts

Junior Member





« Reply #15 - Posted 2008-05-05 22:20:39 »

 Grin Grin Grin
I found a way to fix those stupid errors.
Adding
1  
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 <Type> it is, but in the extending class's methods there is no mention of what kind of linkedListType<Huh?> 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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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.

In space no-one can hear you System.out.println()
Offline PaulReeves

Senior Newbie





« Reply #16 - Posted 2008-05-06 09:45:45 »

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  Smiley

Incidentally I tried it on the 4.3 beta of mingw and it complains if you do not include the A<T>:: 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.
Offline malberts

Junior Member





« Reply #17 - Posted 2008-05-06 09:52:40 »

Here's the "official" fix I received from someone this morning:

Quote
If are using the newest compiler that was supplied to you this year (mingw_gcc3.4.2.exe), you may have encountered the following problem. Although orderedLinkedListType is derived from linkedListType, the compiler does not associate the inherited data members such as first and count as members of the derived class. The standard says that unqualified names in a template are generally non-dependent and must be looked up when the template is defined. Since the definition of a dependent base class is not known at that time, unqualified names are never resolved to members of the dependent base class. Where names in the template are supposed to refer to base class members or to indirect base classes, they can be made dependent by qualifying them. In this case, this could be achieved by replacing the all references to first, last or count with this->first, or this->count and this->last.

In space no-one can hear you System.out.println()
Offline PaulReeves

Senior Newbie





« Reply #18 - Posted 2008-05-06 10:07:51 »

Thanks Smiley

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!

Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #19 - Posted 2008-05-28 02:26:56 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (35 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (31 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (44 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!