C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-16-2003, 02:39 PM   #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.
roktsyntst is offline   Reply With Quote
Old 04-16-2003, 02:44 PM   #2
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,129
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.
Magos is offline   Reply With Quote
Old 04-16-2003, 02:49 PM   #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.
roktsyntst is offline   Reply With Quote
Old 04-16-2003, 06:30 PM   #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
Stoned_Coder is offline   Reply With Quote
Old 04-16-2003, 06:34 PM   #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.
roktsyntst is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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