public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/7467: method & class w/same name gives unhelpful error.
@ 2002-08-01 10:46 ben
  0 siblings, 0 replies; 2+ messages in thread
From: ben @ 2002-08-01 10:46 UTC (permalink / raw)
  To: gcc-gnats


>Number:         7467
>Category:       c++
>Synopsis:       method & class w/same name gives unhelpful error.
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Aug 01 10:46:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     ben@timing.com
>Release:        gcc version 2.95.3 [FreeBSD] 20010315 (release)
>Organization:
>Environment:
piglet.ttyp3.0$ uname -a
FreeBSD piglet.timing.com 4.3-RELEASE-TSC FreeBSD 4.3-RELEASE-TSC #1: Mon Aug 20 16:03:52 MDT 2001     root@piglet.timing.com:/home/ben/newpig/FreeBSD-tsc-4/sys/compile/SMPIGLET  i386
>Description:
When a class is given the same name as a method in your inheritance hierarchy, g++ emits the same error as if the class name were undefined.

Example:

If you have a class, A, with a method A::foo() and then also define a class foo, if you attempt to use a variable of type foo within class A, you get the error "syntax error before `;'" from g++. This is the same error you would get if you mistyped foo as f00.

This error can be easy to reproduce if you have a complex inheritance lattice (perhaps from a third party library) and you accidentally name a class in a program to have the same name as a method in the inheritance lattice. You then end up with the puzzling situation where your newly defined class can be used in some instances, and not in others.

I request a clarification of the syntax error in this case to emit a more particular error message. Since in the scope of the class with the method name that clashes with the class name you are attempting to define a variable of type "method name", the error message should indicate that there is such a clash, or at least that the compiler thinks you're a dummy for trying to declare a variable with a type that has the same name as a method in that classes inheritance hierarchy.
>How-To-Repeat:
Simple test case to reproduce:

#include <iostream>

class A
{
public:
    A() {};
    void foo() {};
    foo bar;
};

class foo
{
public:
    foo() {};
};

main()
{
    A oink;
// gives same error as:
// B oink;
// (B not defn'd)
}
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: c++/7467: method & class w/same name gives unhelpful error.
@ 2002-10-26  7:47 lerdsuwa
  0 siblings, 0 replies; 2+ messages in thread
From: lerdsuwa @ 2002-10-26  7:47 UTC (permalink / raw)
  To: ben, gcc-bugs, gcc-prs, nobody

Synopsis: method & class w/same name gives unhelpful error.

State-Changed-From-To: open->closed
State-Changed-By: lerdsuwa
State-Changed-When: Sat Oct 26 07:47:07 2002
State-Changed-Why:
    Not a bug.  The following is a correct version of your code.
    First, you need to declare the class foo before using it.
    Second, to distinguish between class foo and function foo,
    you can use the scope operator "::".
    
    #include <iostream>
    
    class foo
    {
    public:
        foo() {};
    };
    
    class A
    {
    public:
        A() {};
        void foo() {};
        ::foo bar;
    };
    
    int main()
    {
        A oink;
    // gives same error as:
    // B oink;
    // (B not defn'd)
    }

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7467


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-10-26 14:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-01 10:46 c++/7467: method & class w/same name gives unhelpful error ben
2002-10-26  7:47 lerdsuwa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).