473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Ambiguous MsVC!

Hello everyone,

Please consider the following bit of code:

namespace x
{
class X
{
public:
X(){}
~X(){}
void set(int a) {i=a;}
int i;
};
}

x::X X;
int _tmain(int argc, _TCHAR* argv[])
{
using namespace x; // commenting this out shuts up the compiler's
whining

X.set(4); // error C2872: 'X' : ambiguous symbol
// could be 'c:\temp\test\test\test\test.cpp(27) : x::X X'
// or 'c:\temp\test\test\test\test.cpp(18) : x::X'
}
Isn't the compiler supposed to automatically resolve the ambiguity
since X.set(4) refers to *instance X* and not class X? In fact, I
don't see any ambiguity whatsoever. X.set(4) clearly refers to
'instance X' and not abstract type X, as that would implicate using
the operator '::'.

What do the standards say on this?
PS: I think I'm just gonna have to go back to prefixing every class
name with some letter. I can't afford to be bothered with these stupid
puzzles involving ridiculous ambiguities!

Aug 28 '07 #1
6 1545
mi*************@gmail.com wrote:
Isn't the compiler supposed to automatically resolve the ambiguity
since X.set(4) refers to *instance X* and not class X? In fact, I
don't see any ambiguity whatsoever. X.set(4) clearly refers to
'instance X' and not abstract type X, as that would implicate using
the operator '::'.

What do the standards say on this?
I don't know. But, at a guess, I think you are expecting the compiler to use
the dot . to help it look up the symbol. At X time, before the dot, all the
compiler knows is the current scope contains two co-eval X symbols, so it
will decline to pick one.

If it instead silently picked one, and if you changed your namespacies, then
the compiler might silently pick another one, and that would be bad.

--
Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
http://tinyurl.com/23tlu5 <-- assert_raise_message
Aug 28 '07 #2
Alf P. Steinbach wrote:
>
Probably nobody here can afford to be bothered with checking out the
standard for you.

However, almost as good: run the code through Comeau Online.

Of course, for that you need to first use a standard "main" function.
I've just done it and it generates similar errors, which means MsVC is
right. I guess that what angered me was that I thought I'd never run
into this sort of problems by using non-prefixed class names.

Anyway, I have no other way but to go back to my old ways and prefix
my class names with a letter so as to avoid running into ambiguities.

Thanks for your reply, Alf.

--
Miguel Guedes

- X marks the spot for spammers. If you wish to get in touch with me
by email, remove the X from my address. -

Aug 28 '07 #3
mi*************@gmail.com wrote:
Anyway, I have no other way but to go back to my old ways and prefix
my class names with a letter so as to avoid running into ambiguities.
You could also try naming the namespaces different from the classes.

--
Phlip
http://www.oreilly.com/catalog/9780596510657/
Aug 28 '07 #4
AG
x::X X;
This looks awful to me. Why don't you rather change the name of your
variable instead. A variable and its type are two different things.

You never name your son 'son'. You must give him a real name.

AG.
Aug 28 '07 #5
mi*************@gmail.com wrote:
:: Alf P. Steinbach wrote:
:::
::: Probably nobody here can afford to be bothered with checking out
::: the standard for you.
:::
::: However, almost as good: run the code through Comeau Online.
:::
::: Of course, for that you need to first use a standard "main"
::: function.
:::
::
:: I've just done it and it generates similar errors, which means
:: MsVC is right. I guess that what angered me was that I thought I'd
:: never run into this sort of problems by using non-prefixed class
:: names.
::
:: Anyway, I have no other way but to go back to my old ways and
:: prefix my class names with a letter so as to avoid running into
:: ambiguities.
::

The other option, of course, is to stop using namespace directives,
which introduce the ambiguity in the first place. :-)
Bo Persson
Aug 28 '07 #6
AG wrote:
>x::X X;
This looks awful to me. Why don't you rather change the name of your
variable instead. A variable and its type are two different things.

You never name your son 'son'. You must give him a real name.

AG.
Agreed. However, I'm dealing with a few classes that were designed to
be singletons and statically instantiated, ie a class FontsManager is
instantiated as FontsManager. Thus, there are no "sons" here.

AFAIK the only solution is to change the name of the class. Maybe
someone has a better solution for this that doesn't involve (1)
removing the namespace and (2) changing the instance variable name?

Any comments/suggestions will be greatly appreciated.
--
Miguel Guedes

- X marks the spot for spammers. If you wish to get in touch with me
by email, remove the X from my address. -

Aug 28 '07 #7

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

Similar topics

2
by: Eric Brunel | last post by:
Hi all, We're trying to communicate with Microsoft Visual C++ 6.0 via COM to make it perform a few operations. Our main need is to set a breakpoint on a given line in a given file. We ended up...
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
1
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base...
0
by: Leor Zolman | last post by:
The Intel C++ version of STLFilt (my STL Error Message Decryptor utility) has now been updated to support Intel C++ 8 (currently in Beta) along with version 7. All three MSVC libraries are...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
8
by: Chris Stankevitz | last post by:
I can't tell you how frusterated I and my coworkers are with "MSVC 7.1 .net 2003" (what am I supposed to call this thing anyway?). Compiling and linking take twice as long with "MSVC 7.1 .net...
6
by: Uli | last post by:
Hello, I'm trying to use a DLL (by static linking) which was compiled with Borland C++Builder (BCB) in Visual C++ (Visual-Studio 2003). All functions are declared with the directive 'extern...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
2
by: BruceWho | last post by:
I downloaded boost1.35.0 and built it with following command: bjam --toolset=msvc-7.1 --variant=release --threading=multi -- link=shared --with-system stage and it failed to compile, error...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.