473,387 Members | 1,489 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,387 software developers and data experts.

why does not compile

Hello C++ fans!

Why the following program does not compile on my box:

typedef int my_int;

class A
{

public:
enum Items
{
item1=1000,
item2=2000
};

//typedef int my_int;
int foo(const my_int&)
{
return 0;
}
int foo(const bool&)
{
return 0;
}
};

int main()
{
A a;
a.foo(A::item1);
return 0;
}

it returns errors:

testtypedef.cpp: In function `int main()':
testtypedef.cpp:27: error: call of overloaded `foo(A::Items)' is ambiguous
testtypedef.cpp:15: error: candidates are: int A::foo(const my_int&)
testtypedef.cpp:19: error: int A::foo(const bool&)
but when I comment first line (typedef...) and uncomment 13 (typedef)
line compiles fine. I can't explain such behaviour.

Karol Szkudlarek

ps.

Compiler on my box: gcc (3.3.3)

Jul 22 '05 #1
6 1383
> typedef int my_int;

class A
{

public:
enum Items
{
item1=1000,
item2=2000
};

//typedef int my_int;
int foo(const my_int&)
{
return 0;
}
int foo(const bool&)
{
return 0;
}
};

int main()
{
A a;
a.foo(A::item1);
return 0;
}


There's no such thing as A::item1.

It is not static, it is a plain old member variable.
-JKop
Jul 22 '05 #2
JKop wrote:
typedef int my_int;

class A
{

public:
enum Items
{
item1=1000,
item2=2000
};

//typedef int my_int;
int foo(const my_int&)
{
return 0;
}
int foo(const bool&)
{
return 0;
}
};

int main()
{
A a;
a.foo(A::item1);
return 0;
}
There's no such thing as A::item1.


???
There is!
It is not static, it is a plain old member variable.

???
item1 is in the enum A::items

You may have misread the code

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3
Karol Szkudlarek wrote:

Hello C++ fans!

Why the following program does not compile on my box:

typedef int my_int;

class A
{

public:
enum Items
{
item1=1000,
item2=2000
};

//typedef int my_int;
int foo(const my_int&)
{
return 0;
}
int foo(const bool&)
{
return 0;
}
};

int main()
{
A a;
a.foo(A::item1);
return 0;
}

it returns errors:

testtypedef.cpp: In function `int main()':
testtypedef.cpp:27: error: call of overloaded `foo(A::Items)' is ambiguous
testtypedef.cpp:15: error: candidates are: int A::foo(const my_int&)
testtypedef.cpp:19: error: int A::foo(const bool&)

Obviously the compiler cannot decide which way to go

* First convert A::item1 to a my_int and call function int foo( const my_int& )
* First convert A::item1 to a bool and call function int foo( const bool& )

Both ways are equally good
but when I comment first line (typedef...) and uncomment 13 (typedef)
line compiles fine. I can't explain such behaviour.


It's simple. By commenting out the first typedef, there is no longer
a global alias for my_int.
Thus in function main, the compiler has no way of converting A::item1 to an
my_int, because there simply isn't a globally my_int available. It is as
if the compiler has never heared about something called my_int.
The second typedef (the one in line 13) has moved the alias into the
scope of the class.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
Karl Heinz Buchegger wrote:
Karol Szkudlarek wrote:
Hello C++ fans!

Why the following program does not compile on my box:

typedef int my_int;

class A
{

public:
enum Items
{
item1=1000,
item2=2000
};

//typedef int my_int;
int foo(const my_int&)
{
return 0;
}
int foo(const bool&)
{
return 0;
}
};

int main()
{
A a;
a.foo(A::item1);
return 0;
}

it returns errors:

testtypedef.cpp: In function `int main()':
testtypedef.cpp:27: error: call of overloaded `foo(A::Items)' is ambiguous
testtypedef.cpp:15: error: candidates are: int A::foo(const my_int&)
testtypedef.cpp:19: error: int A::foo(const bool&)

Obviously the compiler cannot decide which way to go

* First convert A::item1 to a my_int and call function int foo( const my_int& )
* First convert A::item1 to a bool and call function int foo( const bool& )

Both ways are equally good

but when I comment first line (typedef...) and uncomment 13 (typedef)
line compiles fine. I can't explain such behaviour.

It's simple. By commenting out the first typedef, there is no longer
a global alias for my_int.
Thus in function main, the compiler has no way of converting A::item1 to an
my_int, because there simply isn't a globally my_int available. It is as
if the compiler has never heared about something called my_int.
The second typedef (the one in line 13) has moved the alias into the
scope of the class.


I added printf to foo body and program call function int A::foo(const
my_int&) not int A::foo(const bool&). After reading your response I
expected that foo with bool will be called because my_int isn't
globally available.

In the meantime I tried to compile the program with my_int in global
scope on my another box (also gcc 3.3.3) and it compiles fine as I
previously expected. In real app my_int is globally available.

Jul 22 '05 #5
Karol Szkudlarek wrote:
Why the following program does not compile on my box:

typedef int my_int;

class A
{

public:
enum Items
{
item1=1000,
item2=2000
};

//typedef int my_int;
int foo(const my_int&)
{
return 0;
}
int foo(const bool&)
{
return 0;
}
};

int main()
{
A a;
a.foo(A::item1);
return 0;
}

it returns errors:

testtypedef.cpp: In function `int main()':
testtypedef.cpp:27: error: call of overloaded `foo(A::Items)' is ambiguous
testtypedef.cpp:15: error: candidates are: int A::foo(const my_int&)
testtypedef.cpp:19: error: int A::foo(const bool&)
but when I comment first line (typedef...) and uncomment 13 (typedef)
line compiles fine. I can't explain such behaviour.


There is at least one reason for it: a bug in the compiler.

Victor
Jul 22 '05 #6
Karol Szkudlarek wrote:
Hello C++ fans!

Why the following program does not compile on my box:


Oh, forgot to mention: please don't post attachments. Thanks.
Jul 22 '05 #7

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

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
4
by: Alex Vinokur | last post by:
Compiler GNU gpp.exe (GCC) 3.4.1 Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile that? ------ foo.cpp ------ struct Foo { explicit Foo(int) {}
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
11
by: MonkeeSage | last post by:
A quick question about how python parses a file into compiled bytecode. Does it parse the whole file into AST first and then compile the AST, or does it build and compile the AST on the fly as it...
5
by: Jeff | last post by:
hi asp.net 2.0 I get this compile error: 'Image' does not contain a definition for 'ImageUrl' Image image = (Image)e.Item.FindControl("img"); image.ImageUrl = "~/image.png";
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.