C++ templated class with const paramters for functions

Started by
4 comments, last by fov 21 years, 9 months ago
Well, again my C++ compiler makes me bang my head onto my keyboard I have a class called Map which is my implementation of an associative array (it stores key=>value pairs):
      
template<class Tkey, class Tvalue> class Map
{
public:
    inline void Add(const TKey &key, const Tvalue &value)
    {
        // etc...

    }

    // etc...

};


// Trying to add elements to a <int, int> map works fine:

void SomeClassX::SomeFunc (const int a, const int b)
{
    // _testmap is a Map<int, int>

    _testmap.Add(5, 12);
}


// But using MyObject * does not compile:      

void SomeClassY::SomeFunc (const uint32_t a, const MyObject *p)
{
    // _testmap is a Map<uint32_t, MyObject *>

    _testmap.Add(a, p);
}

/*
Compiler error: converting to 'MyObject *' from 'const MyObject *' discards qualifiers in passing argument 2 of 'Map<unsigned int,MyObject *>::Add(const uint32_t &, MyObject *const &)
*/
    
Why is 'const Tvalue &value' expanded to 'MyObject *const &'?? I read various websites about templates and const parameters, and tried tricks like using 'Tvalue const &value' as the parameter declaration. I even tried to do a 'typdef Tvalue &Tvalueref' and using 'const Tvalueref value'... but nothing helps... Does anybody know what's going on there? fov [edited by - fov on July 16, 2002 12:10:29 AM]
Advertisement
The correct syntax is:
inline void Add(const typename TKey& key, const typename Tvalue& value) 

But I doubt that will help MSVC6 any...

Post the exact error message, and the line of code that emits it... perhaps Tvalue is already const?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
The problem here is you''re passing in a const* into SomeFunc() and trying to add it to _testmap, which is expecting a non-const*.

Actually, you''re not, are you - at least, in the code you provided, you have a local variable with the same name as one of the parameters...so either you are in your original code and made a typo here, or shame on you for having such a bad coding practice.

Either way, I''ve lost interest in solving the problem.

Cheers,
-scott
I think it also might be a conflict between the parameter and the local variable, although why wouldn''t it generate a compile error that "p" is already declared or something like that?

And make sure to post the error number too, i,e E4012 or whatever. That helps us find it in MSDN
Ooops, sorry... yes, the local variable is nonsense. Seems like I made a mistake while cuttting down the code...

The exact compiler error is in the last lines of the source above (gcc-2.95.3 output)...

Using ''const typename Tvalue &value'' results in: parse error before ''&''

I also tries msvc6 (version 12.00.8804, german), which gives me: error C2664: ''Add'' : Konvertierung des Parameters 2 von ''const class MyObject *'' in ''class MyObject *const & '' nicht moeglich Durch die Konvertierung gehen Qualifizierer verloren

Wether I use ''typename'' or not seems to do not matter for msvc6.

''Add'' seems to expect a non-const even though I declared the parameter as ''const Tvalue &value'', but why?

Casting to a non-const ''Add(a, (MyObject *)p)'' makes the error disappear, but I don''t like that way as a solution

fov
You''re confusing where the "const" goes.

Let''s dissect the code.

You have Map, which means you have:

void Map::Add(const uint32_t &key, const MyObject *&value)

So the second parameter is expected to be a const-ref to "MyObject *".

But in SomeClassY::SomeFunc,

void SomeClassY::SomeFunc (const uint32_t a, const MyObject *p)
{
_testmap.Add(a, p);
}

"p" has the type "const MyObject *".

Clearly, "p" has different type to what Add() expects.

Add() wants "MyObject *" but you''re giving it "const MyObject *"...


~~~~
Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!

This topic is closed to new replies.

Advertisement