Problem with operator+

This is a discussion on Problem with operator+ within the C++ Programming forums, part of the General Programming Boards category; I'm writing a class to manage memory for me (mostly for the experience though). Borland 5.5.1 for Windows is giving ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Problem with operator+

    I'm writing a class to manage memory for me (mostly for the experience though). Borland 5.5.1 for Windows is giving me a weird error. Here is the class:
    Code:
    namespace MemoryManager {
    
    	enum Option { printall = 1 }; // just one for now
    
    	typedef map< void*, Object > pvomap;
    	
    class Mgr {
    
    public:
    	Mgr( Option opt = 0 );
    	template< class T > T* alloc( int = 1 );
    	template< class T > void dealloc( T* );
    	int allocated( const void* ptr ) const;
    	template< class T > long bytes_allocated ( const T* ) const;
    	long total_allocated() const;
    	long total_bytes_allocated() const;
    
    protected:
    	Option itsOptions;
    	pvomap allPtrs;
    
    };
    
    } // end namespace MemoryManager
    In the definition file, this function is giving me the following error:
    Error E2094: 'operator+' not implemented in type 'pvomap' for arguments of type 'const void *'.

    Code:
    	int Mgr::allocated( const void* ptr ) const {
    
    		Object foo = allPtrs[ ptr ]; // error on this line
    		int bar = foo.created();
    		return bar;
    
    	}
    (This is the result of breaking down the steps. Originally the function was defined with one line: "return allPtrs[ptr].created();" but I broke it down to help me find the problem. I'm still stuck - I have no idea where the operator+ has anything to do with this...any help is appreciated. Thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I believe that the [ ] operator works so it takes the base pointer (allPtrs) then adds the relative adress (ptr) to get the effective adress. And thus, const void* doesn't have a + operator defined.
    Why doing that? Indexes must be integers.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    allPtrs is not an array, but rather a std::map with first type "void *". It's subscript operator should be overloaded to take that type and return the associated Object (in this case). What puzzles me is this error ocurring here, but other functions using a similar construct do not report an error.

    I also forgot to mention that the end of that function reports the following warning:
    W8057: Parameter 'ptr' is never used in function Mgr::allocated(const void* const).

    This is also strange, since ptr is clearing being used...I'm guessing there's an obvious syntax error that I'm just missing.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    const void *

    do you know what that is?

    do you mean

    void * const
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    The difference refers to whether it is the address that is constant or the value there, correct? It probably makes more sense to have void * const, but that doesn't change the issue. A search on comp.lang.c++ (http://groups.google.com/groups?hl=e...5506%40acm.org) revealed that you can't use operator[] on a const map (which is a map that is part of a const object). So I used find instead (at the recommendation there), but that said it could find no match. So I've kept the const-ness of the pointer out completely in order for it to compile...

    edit: I changed const void* to void* and now to void * const, and it seems to be working. Thanks for the tip.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dot operator problem HELP!
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2008, 11:45 AM
  2. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  3. Serializing problem.. (can't use >> operator)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2005, 12:10 PM
  4. Weird Problem with return with operator +
    By KonArtis in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2004, 04:46 PM
  5. problem with new operator
    By codefx in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2002, 06:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21