Thread: Error with class me,bers

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    722

    Error with class me,bers

    In currently programming a simulation of algorithms to allocate memory. And it's giving me an anoing error with the class' definition. I can't tell why.
    I feel extremly noobed!!!
    Code:
    //memory.h
    struct Block{
    	ADRESS adress;
    	SIZE_T size;
    };
    
    #define DEFAULT_REGION_SIZE ((SIZE_T)1024)
    #define BASE_REGION_ADRESS ((ADRESS)1024)
    
    const int MINBLOCKSIZE = 4;
    enum AllocEnum{ALLOC_FIRST_FIT,ALLOC_BEST_FIT,ALLOC_WORST_FIT};
    
    class Region: private Block{	
    	friend Block alloc_first_fit(Region& region, SIZE_T _size);
    	friend Block alloc_best_fit (Region& region, SIZE_T _size);
    	friend Block alloc_worst_fit(Region& region, SIZE_T _size);
    	friend bool free(Region& region,DWORD adress);
    
    	Block (*falloc)(Region& region, SIZE_T _size);
    
    public:
    	Region(	ADRESS base = BASE_REGION_ADRESS,
    			SIZE_T _size = DEFAULT_REGION_SIZE,
    			AllocEnum allocator = ALLOC_FIRST_FIT);
    	Region(const Region&);
    
    	inline ADRESS getBaseAdr() const;
    	inline SIZE_T getSize() const;
    
    	ADRESS alloc(SIZE_T _size);
    	bool free(ADRESS adr);
    };
    //memory.cpp
    Region::Region(ADRESS base,SIZE_T _size,AllocEnum allocator) :
    	adress(base), size(_size),//error were
    	falloc(allocator==ALLOC_FIRST_FIT?alloc_first_fit:
    	(allocator==ALLOC_BEST_FIT?alloc_best_fit:alloc_worst_fit)){
    }
    
    Region::Region(const Region& rg) : adress(rg.adress),
    	size(rg.size), falloc(rg.falloc){//error here
    }
    //errors
    error C2614: 'Region' : illegal member initialization: 'size' is not a base or member
    error C2614: 'Region' : illegal member initialization: 'adress' is not a base or member
    error C2614: 'Region' : illegal member initialization: 'size' is not a base or member
    error C2614: 'Region' : illegal member initialization: 'adress' is not a base or member
    Isn't Region suposed to inherit Block? The errors are in the constructors.
    NOTE: I don't want to skip the problem declaring the vars elsewhere. Thank you for your attention
    Last edited by xErath; 12-06-2004 at 09:54 AM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you can not initalize a base class' variables directly you have to call the base class' constructor from the init list

    also as a note leading _ variable names are reserved for the compiler

    edit: hmmmmm just noticed that block is a struct. try using something like Block::size = _size (not 100% sure on that)

    edit 2: I just noticed you were using private inhertance on Block. Instead use composition ie:
    Code:
    class Region
    {
      Block block;
      // other stuff
      public:
      Region(/*parameters*/) :
        block.size = _size
      {}
    }
    Last edited by Thantos; 12-06-2004 at 10:05 AM.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Thank you, I stupid of me forgeting to call the Block constructor! Bah... But anyway, Block became class, which has a constructor. And I need it to be, for other reasons.
    Thank you again ...

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>But anyway, Block became class, which has a constructor.
    Well, struct can have anything that class can have. As far as I know, the only difference between the two is that struct is all public by default, while class is all private/protected by default

    Code:
    struct Block
    {
       Block() {...}
       //variables
    };
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM