Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to implement a sorting method in a list in which I have used some protected variables count, entry[maxlist] etc.
List.h
template < class List_entry >
class List
    {
    public:
    //etc etc
    protected:
    int count;
    int entry[maxlist];
    };


Sortable_list.h
typedef Key Record;
class Sortable_list:public List<Record>
{
void selection_sort()
    {
        for(int position=count-1;position>0;i--) // Count is not declared in the scope
        {
        int max=max_key(0, position);
        swap(max, position);
        }
    }
};

The error is mentioned within the comments. Here's the full code of List.h : http://pastebin.com/3xTADqvN[^]
Sortable_list.h: http://pastebin.com/k1ARsyCR[^]
Posted
Updated 11-Jan-11 6:32am
v3
Comments
#realJSOP 11-Jan-11 12:03pm    
What does your constructor look like?
optimus_prime1 11-Jan-11 12:07pm    
List():count(0){}
Link to full code is in the post above

Just leaving aside the whole "protected data members are the sperm of the devil" thing, the code you've presented there looks like it should compile.

The only suggestion I can make based on the code is that you haven't included something - I couldn't see any mention of #include "List.h" in your sortable list header - or you've got two different headers kicking around.

Going forward the best thing to do (apart from not trying to implement your own list) is to start again on the derived class. Take it baby steps, adding small bits of code each time. Then when something goes wrong you know what you did to cause it and can hopefully isolate and fix it. Just start with something simple:

#include "List.h"

class A : public List<int>
{
    public:
        A()
        {
            count = 87;
        }
};


If that compiles and works with no additional headers aside from the base class then you know your problem is with something else. If it doesn't then there's something weird with the base class, but cunningly disguised.

Cheers,

Ash

PS: Or after seeing the error messages it could be the loop variable...
 
Share this answer
 
v2
Comments
optimus_prime1 11-Jan-11 13:28pm    
@Aescleal: I already tried to include List.h in Sortbale_list.h but then it starts giving huge number of errors related to redefinition of what I have defined in List.h.
P.S I am on Console application in Code::Blocks.
Sergey Alexandrovich Kryukov 11-Jan-11 14:06pm    
My 5.
optimus_prime1, glad to hear from you again.

I just compiled your code -- it does compile.

[EDIT]: see comments below.
 
Share this answer
 
v2
Comments
optimus_prime1 11-Jan-11 13:23pm    
If it would have been compiling for me, I wouldn't have been asking this. Screenshot: http://i.imgur.com/bc2mx.jpg
Aescleal 11-Jan-11 13:28pm    
Try fixing some of the other errors - the one that says i isn't defined could be confusing the compiler.
optimus_prime1 11-Jan-11 13:30pm    
@Aescleal: Just did that but to no avail. Screenshot: http://i.imgur.com/A9KAs.jpg
Aescleal 11-Jan-11 13:33pm    
I completely missed the fact you're not guarding your include files with include guards to prevent redefinition.

http://en.wikipedia.org/wiki/Include_guard describes how to use them fairly well.
Sergey Alexandrovich Kryukov 11-Jan-11 14:06pm    
@Aescleal: this is a good point, I've missed it, too as I compiled all together -- in a write way. "Include" Must die.
you are using code blocks.
you must be using gcc.
have you added some other libraries.
i think the other library is having the List template class which is used in place of yours.


put these statements in ur List.h before the start and end of ur file

#ifndef _MYLIST_H
#define _MYLIST_H


...ur code


#endif



in ur Sortable_list.h


#include "List.h" //note very important
#ifndef _SORTLIST_H
#define _SORTLIST_H

ur code......


#endif

regards
rajesh
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900