无法继承受保护的变量 [英] Unable to inherit protected variables

查看:64
本文介绍了无法继承受保护的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图在一个列表中实现一种排序方法,在该列表中我使用了一些受保护的变量countentry[maxlist]等.
List.h

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



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);
        }
    }
};


该错误在注释中提及.这是List.h的完整代码: http://pastebin.com/3xTADqvN [ http://pastebin.com/k1ARsyCR [ ^ ]


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[^]

推荐答案

optimus_prime1,很高兴再次收到您的来信.

我刚刚编译了您的代码-它确实可以编译.

:请参阅下面的评论.
optimus_prime1, glad to hear from you again.

I just compiled your code -- it does compile.

: see comments below.


只是撇开整个受保护的数据成员是魔鬼的精子",您在此处提交的代码看起来应该可以编译.

我可以根据代码提出的唯一建议是,您没有包含某些内容-我在可排序列表标题中看不到#include "List.h"的任何提法-或者您有两个不同的标题出现.

前进最好的方法(除了不尝试实现自己的列表之外)是从派生类重新开始.采取一些简单的步骤,每次都添加少量代码.然后,当出现问题时,您便知道造成该问题的原因,并且可以隔离并修复该问题.只需简单开始即可:

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;
        }
};



如果该代码可以编译并且可以在基类之外没有其他头文件的情况下工作,那么您就知道问题出在其他方面.如果不是这样,那么基类就有些奇怪了,但是却被巧妙地掩盖了.

干杯,



PS:或者看到错误消息后,它可能是循环变量...



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


您正在使用代码块.
您必须使用gcc.
您是否添加了其他一些库.
我认为其他图书馆正在使用List模板类来代替您的模板.


将这些语句放在ur文件的开始和结束之前的ur List.h中

#ifndef _MYLIST_H
#define _MYLIST_H


...我们的代码


#endif



在ur Sortable_list.h中


#include"List.h"//注意非常重要
#ifndef _SORTLIST_H
#define _SORTLIST_H

您的代码...


#endif

问候
rajesh
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


这篇关于无法继承受保护的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆