473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VC++: Cast operator not used?

I'm porting some code to Visual C++ and have run into a problem - the
compiler won't use a user-written cast operator.

The code uses an envelope-letter approach to passing (potentially)
large pieces of data around, and requires that certain methods return
an Envelope with a specific kind of Letter as it's content. I have a
cast operator that converts from what I've got to what should be
returned, but it seems that the compiler only looks at constructors
for the return type.

The code works fine with the SGI MIPSPRO and Linux GCC compilers.
With MS Visual C++ .NET (v. 7.1.3008) it says:

CDP3x3LinearTransform.c++(196): error C2668: 'DataPacket::DataPacket' :
ambiguous call to overloaded function
DataPacket.h(53): could be 'DataPacket::DataPacket(Letter &)'
DataPacket.h(52): or 'DataPacket::DataPacket(const DataPacket &)'
DataPacket.h(51): or 'DataPacket::DataPacket(const Envelope<T> &)'
where the offending line in CDP3x3LinearTransform.c++ looks like this:

DataPacket
CDP3x3LinearTransform::Output() const {
ChannelDataPacket result;
//... compute result here...
return result; // line 196
}

In this case I've got a ChannelDataPacket and need to return a
DataPacket. ChannelDataPacket is NOT derived from DataPacket (even
tho' you'd expect that, given their names), but they ARE related.
ChannelDataPacket DOES have a conversion operator to convert itself to
a DataPacket, however (see below). I've found two work-arounds:

#1: Call the cast operator explicitly:

return result.operator omiDataPacket();

#2: Explicitly cause the cast operator to be called by adding code:

DataPacket dp;
dp = result;
return dp;

I have two objections to both of these workarounds: a) If it won't
work as written, what's the point in having a cast operator? and b)
This is done in about 150 different places in 70 different classes.
That's just way too much typing for someone as lazy as I am.
Questions:

- Is this a known problem with VC++? I've looked in the MS knowledge
base and found nothing, but I might be looking for the wrong thing.

- Is there some way of getting the VC++ compiler to notice (and use)
the cast operator without having to inflict a MS-specific kludge all
over the code?
Thanks!
Code:

//---------------------------------------------
// The Envelope to be wrapped around a Letter.
//---------------------------------------------
class EnvelopeBase
{
public:
virtual ~EnvelopeBase();
EnvelopeBase();
EnvelopeBase(Letter::WriteAndCopyModeType wcm);
EnvelopeBase(const EnvelopeBase& from);
EnvelopeBase& operator=(const EnvelopeBase& rhs);
};
template < class T >
class Envelope : public EnvelopeBase
{
public:
virtual ~Envelope();
Envelope();
Envelope( const Envelope& );
Envelope( Letter& letter );

Envelope& operator=( const Envelope& );
T* operator->();
const T* operator->() const;
};

//---------------------------------------------
// Letter - What gets put inside an Envelope.
//---------------------------------------------
class Letter
{
public:
enum WriteAndCopyModeType { ValueImmediate, ValueDelayed,
Pointer };
enum ExemplarType { exemplar };
virtual ~Letter();
Letter();
Letter( const String& );
Letter( const Letter& );

protected:
friend class Envelope< Letter >;
};
//---------------------------------------------
// DataPacketLetter - the data we're interested in.
//---------------------------------------------
class DataPacketLetter : public Letter
{
public:
virtual ~DataPacketLetter();
DataPacketLetter();
DataPacketLetter( const DataPacketLetter& );
DataPacketLetter( ExemplarType );
DataPacketLetter( const String& );

protected:
DataPacketLetter( const String&, ExemplarType );
};
//---------------------------------------------
// DataPacket - the data we're interested in, wrapped in an Envelope.
//---------------------------------------------
class DataPacket : public Envelope< DataPacketLetter >
{
public:
virtual ~DataPacket();
DataPacket();
DataPacket( const Envelope< DataPacketLetter >& from );
DataPacket( const DataPacket& from );
DataPacket( Letter& letter );
DataPacket& operator=( const DataPacket& rhs );
operator DataPacketLetter&() const;
};


//---------------------------------------------
// ChannelDataPacket(Letter) - the data we're interested in, wrapped &
unwrapped.
//---------------------------------------------
class ChannelDataPacketLetter : public DataPacketLetter
{
public:
virtual ~ChannelDataPacketLetter();
ChannelDataPacketLetter();
ChannelDataPacketLetter( int theInitialSize );
ChannelDataPacketLetter( const ChannelDataPacketLetter& );
ChannelDataPacketLetter( ExemplarType );
ChannelDataPacketLetter( const String& );
};
class ChannelDataPacket : public Envelope< ChannelDataPacketLetter >
{
public:
virtual ~ChannelDataPacket();
ChannelDataPacket();
ChannelDataPacket( float theValue );
ChannelDataPacket( const ChannelDataPacket& theOriginal );
ChannelDataPacket( Letter& letter );
ChannelDataPacket& operator=( const ChannelDataPacket& theRhs );
ChannelDataPacket& operator=( const float& theRhs );
operator ChannelDataPacketLetter&() const;
operator DataPacket() const;
operator float() const;
};

Jul 23 '05 #1
0 1661

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3603
by: Count Dracula | last post by:
I narrowed down the source of the error. The stand-alone program listed below reproduces it. The compilation finishes but the warning is too serious to ignore: cl -GX prog.cpp Microsoft (R)...
5
2032
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
4
2372
by: danip | last post by:
Hi, I used to have the following: class CArchive { public: // create and specify the filename to use CArchive(CString filename, DWORD size); virtual ~CArchive(); CArchive& operator<<(bool...
9
4635
by: Stuart Carnie | last post by:
Due to the tightening of the VC++ compiler in 2005, I have run into a compiler error (from code that previously worked in 2003) using a CComPtr<ITypeLib> as an element of a std::list, as follows...
5
2344
by: Frederick Gotham | last post by:
Before I begin, here's a list of assumptions for this particular example: (1) unsigned int has no padding bits, and therefore no invalid bit- patterns or trap representations. (2) All types have...
4
6229
by: nmrcarl | last post by:
I'm trying to upgrade a large project from VS 6.0 to VS 2005. After fixing a lot of things that changed (mostly sloppy coding in the original project that VS2005 didn't allow), I got the release...
3
3739
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
7
8426
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then...
7
3937
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.