Inheriting List to Sortable List-protected members out of scope
I am developing a list in which I have used some protected variables count
, entry[maxlist]
etc.
List.h
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,开发者_JS百科 position);
}
}
};
Is something wrong with inheriting the List to Sortable List? Why is it showing count out of scope?
#Edit: After seeing your whole code it becomes clearer. You're having ambiguities because of your includes, it will compile with msvc, because it handles such cases silently, but for g++ you should explicitly state that count
is from this class, by doing this->count
. You also had problems because of std::range_error
, which could be avoided by removing using namespace std
or replacing range_error
with ::range_error
which will indicate that you want the global scope. Another problem with your code is that, you were using an undefined variable i
in your Sortable_list
. The fixed code that compiles with g++ and msvc: http://codepad.org/7V70rNqf
I don't want to sound rude, but I strongly suggest you read a book on C++, your current code is very anti-idiomatic, and could be made generic with a smaller amount of code.
Why don't you use sort function template from <algorithm>
header? All you need to write just one small Compare
function.
Look like your List
is not a template class, so List< Typename >
doesn't exist ..
Also, you can use std::set<T>
as a template class for sorted container => http://www.sgi.com/tech/stl/set.html
精彩评论