double pointer issue....const-correctness

P

puzzlecracker

The code fails to compile: why?

can't figure out how to fix it....

std::map<string, const Foo*> m;
//assign to pFoo if it is in the map
bool TryToAssign(int i, const Foo* & pFoo) /*fails to compile*/
{
std::map<int , const Foo* >::const_iterator iter=m.find(i);
if(iter!=m.end())
pFoo=iter->second;
else
return false;
return true;

}


void Test()
{
const Foo* f;
if(TryToAssign(6, f))
cout<<"SUCCESS... õòá\n";

}
 
A

Alf P. Steinbach

* puzzlecracker:
The code fails to compile: why?

can't figure out how to fix it....

std::map<string, const Foo*> m;

This maps strings to pointers.

//assign to pFoo if it is in the map
bool TryToAssign(int i, const Foo* & pFoo) /*fails to compile*/
{
std::map<int , const Foo* >::const_iterator iter=m.find(i);

This tries to find an integer.

if(iter!=m.end())
pFoo=iter->second;
else
return false;
return true;

}


void Test()
{
const Foo* f;
if(TryToAssign(6, f))
cout<<"SUCCESS... õòá\n";

}

Cheers & hth.,

- Alf
 
L

litb.os

The code fails to compile: why?
std::map<string, const Foo*> m;
//assign to pFoo if it is in the map
bool TryToAssign(int i, const Foo* & pFoo)  /*fails to compile*/
{ .....

void Test()
{
   const Foo* f;
   if(TryToAssign(6, f))

On basis of the error message you gave us at stackoverflow ( here:
http://stackoverflow.com/questions/663950/double-pointer-const-issue-issue
) where you say the compile error message is "Error: no matching
function for call to TryToAssign(int, Foo *&)'", It looks to me that
in your code, you have declared f a different way.

The error message suggests you try to call the function with a Foo*.
Change the pointer to a const Foo*. While that conversion is implicit,
it's not anymore if there is a reference in between.

A may convert to B, but that does not mean that A converts to B&.
Simple other example is

int a = 0;
float b = a; // works!

int a = 0;
float &b = a; // doesn't work!

So, double check that in your code, f has the type const Foo* (like in
the code you pasted) as opposed to Foo*.
 
P

puzzlecracker

On said:
On basis of the error message you gave us at stackoverflow ( here:http://stackoverflow.com/questions/663950/double-pointer-const-issue-...
) where you say the compile error message is "Error: no matching
function for call to TryToAssign(int, Foo *&)'", It looks to me that
in your code, you have declared f a different way.

The error message suggests you try to call the function with a Foo*.
Change the pointer to a const Foo*. While that conversion is implicit,
it's not anymore if there is a reference in between.

A may convert to B, but that does not mean that A converts to B&.
Simple other example is

int a = 0;
float b = a; // works!

int a = 0;
float &b = a; // doesn't work!

So, double check that in your code, f has the type const Foo* (like in
the code you pasted) as opposed to Foo*.

The question asked on stackoverflow has NOTHING to do with this
question. Please disregard anything posted there. thx
 
M

mzdude

The code fails to compile: why?
Perhaps including the error message would help.

The following compiles and works for VC 2005
#include <map>
using namespace std;

struct Foo2
{};

std::map<int, const Foo2*> m;
//assign to pFoo if it is in the map
bool TryToAssign(int i, const Foo2* & pFoo) /*fails to compile*/
{
std::map<int , const Foo2* >::const_iterator iter=m.find(i);
if(iter!=m.end())
pFoo=iter->second;
else
return false;
return true;
}


void Test()
{
const Foo2* f=0;
if(TryToAssign(6, f))
cout<<"SUCCESS...\n";
}

void main()
{
m[6] = new Foo2();
Test();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top