Compiling Error in VS C++ .NET that works fine in VS 6.0

B

bmwrob

I’m very new in C++ programming and am trying to make a decoder tool
just for the purpose of learning. I started my project in VC++ 6.0,
but after a change of PC, I continued my programming in VS .NET.

Now I get a lot of compiling error which I don’t understand. Here is
an extract from the code:

int iOct;
CString sASCII;

sASCII += ((iOct >> 4) + 48);

This works fine in VS 6.0 but gives an error stating it’s “ambiguous”
in .NET!

I do have this workaround :

char cTemp[8];
iOct = ((iOct >> 4) +48);
itoa(iOct,cTemp,16);
sASCII += cTemp;

But it would be nice if the simple VS 6.0 version of the code worked.
Any idea why .NET dislike the original code and if it’s possible to
fix?

BR /// Rob
 
W

William DePalo [MVP VC++]

bmwrob said:
But it would be nice if the simple VS 6.0 version of the code worked.
Any idea why .NET dislike the original code and if it’s possible to
fix?

It is often a good idea to post the problematic code AND the full text of of
the error message. That usually rings a bell with someone. Without the text
of the error, someone has to create a project, copy the code and compile.
Some here have the time for that, some don't.

Regards,
Will
 
B

bmwrob

Sorry for not being so clear. Here is the complete error message I
get:

c:\MFC\Decoder\DecoderDlg.cpp(592) : error C2593: 'operator +=' is
ambiguous
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\atlmfc\include\cstringt.h(1075): could be
'ATL::CStringT<BaseType,StringTraits>
&ATL::CStringT<BaseType,StringTraits>::blush:perator
+=(wchar_t)'
with
[
BaseType=char,
StringTraits=StrTraitMFC_DLL<char>
]
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\atlmfc\include\cstringt.h(1068): or
'ATL::CStringT<BaseType,StringTraits>
&ATL::CStringT<BaseType,StringTraits>::blush:perator +=(unsigned
char)'
with
[
BaseType=char,
StringTraits=StrTraitMFC_DLL<char>
]
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\atlmfc\include\cstringt.h(1061): or
'ATL::CStringT<BaseType,StringTraits>
&ATL::CStringT<BaseType,StringTraits>::blush:perator +=(char)'
with
[
BaseType=char,
StringTraits=StrTraitMFC_DLL<char>
]
while trying to match the argument list '(CString, int)'


BR /// Rob
 
T

tom_usenet

On 21 Jun 2004 09:03:08 -0500,
I’m very new in C++ programming and am trying to make a decoder tool
just for the purpose of learning. I started my project in VC++ 6.0,
but after a change of PC, I continued my programming in VS .NET.

Now I get a lot of compiling error which I don’t understand. Here is
an extract from the code:

int iOct;
CString sASCII;

sASCII += ((iOct >> 4) + 48);

This works fine in VS 6.0 but gives an error stating it’s “ambiguous”
in .NET!

I do have this workaround :

char cTemp[8];
iOct = ((iOct >> 4) +48);
itoa(iOct,cTemp,16);
sASCII += cTemp;

But it would be nice if the simple VS 6.0 version of the code worked.

Are you sure it worked? It may have compiled, but I suspect it didn't
do what you wanted. I suspect it added an ascii character with the
value ((iOct >> 4) +48) to the string, not a string representation of
the number ((iOct >> 4) +48) as you seem to want.
Any idea why .NET dislike the original code and if it’s possible to
fix?

There is no operator += for CString to add an int to a CString.
Instead, it is trying to call one of the 1 or 2 operator+= methods on
CString that take a char argument. I think the reason it used to work
is that you weren't compiling in UNICODE mode, and now you are, which
means that there are two versions of operator+= that can take an int
(one taking char and one wchar_t), hence the ambiguity.

What were you expecting it to do? Add on a string representation of
the integer to the string? In what base? From the workaround code
above you appear to want hex representation. I suggest you just use
the new code (making sure that your cTemp buffer is *definitely* large
enough - 8 might be too small? 64 might be safer...). It looks like
you have found a bug in your code by upgrading!

Tom
 
B

bmwrob

The code was part of a function that decodes a BCD number to ASCII.
Yes, the old code worked as I expected. But I did check the new code
and it did not! When I used “itoa” I shouldn’t have added “48”.
In the old code, by shifting my octet 4 bits I can assure that the
value is not greater then 9 as the number was BCD coded. Now by
simply adding 48 I got the number in ASCII format.

I have attached the complete function in the last part of the post.
The input to the function was the actual number of octets I needed to
decode and the file pointer.

I have no idea which mode I was compiling in. How can I check that?

I also tried this out in VS 6.0 for testing, assuming that the octet
never is greater then H’F

ASCII += oct + 48;

This worked in VS 6.0 and simply converted the binary number to
ASCII.

BR /// Rob



CString BCD2ASCII(int num_oct, FILE *fp)
{
int count, iOct, iTemp;
char cTemp[8];
CString sASCII;

for (count=0; count<num_oct; count++)
{

iOct = fgetc(fpHPSDFOA);
iTemp = (iOct & 0xF);

if (iOct == 0xFF); // End of even number found

else if (iTemp == 0xF)
{
iOct = (iOct >> 4) ;
itoa(iOct,cTemp,16);
sASCII += cTemp; // End of odd number found. Store last digit

// old code that doesn’t work in .NET

//sASCII += ((iOct >> 4) + 48); // End of odd number found.
Store last digit
}

else
{
if (iOct <10)
{
sASCII += "0"; // Filler zero for the octet
}

itoa(iOct,cTemp,16);
sASCII += cTemp;
}

}
return sASCII;
}
 
T

tom_usenet

On 28 Jun 2004 09:05:09 -0500,
The code was part of a function that decodes a BCD number to ASCII.
Yes, the old code worked as I expected. But I did check the new code
and it did not! When I used “itoa” I shouldn’t have added “48”.
In the old code, by shifting my octet 4 bits I can assure that the
value is not greater then 9 as the number was BCD coded. Now by
simply adding 48 I got the number in ASCII format.

If the value *is* an ascii one, you just want:

sASCII += static_cast<char>((iOct >> 4) + 48);

Tom
 

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

Top