tags:

views:

97

answers:

3

Fully aware, that the question I am asking is outside the purview of the C++ Standard, I am curious to know why GCC throws the same error twice? I know why the error is there, but am looking forwards to understand why the duplication in error message.

#include <iostream>
using namespace std;

struct A{
   virtual void f1() = 0;
};

struct B : A{
};

struct C : A{
   void f1(){}
};

struct D : C, B{
   void f2(){f1();}
};

int main(){}

Error:

prog.cpp: In member function ‘void D::f2()’:
prog.cpp:16: error: reference to ‘f1’ is ambiguous
prog.cpp:5: error: candidates are: virtual void A::f1()
prog.cpp:12: error:                virtual void C::f1()
prog.cpp:16: error: reference to ‘f1’ is ambiguous
prog.cpp:5: error: candidates are: virtual void A::f1()
prog.cpp:12: error:                virtual void C::f1()
+2  A: 

Which version of G++ are you using?

Interestingly, compiling the code you show on MacOS X 10.6.4, using Apple's G++ 4.2.1, I get the double error message.

With my own build of G++ 4.5.1, I only get a single warning.

It looks like there was a bug that has been fixed.

Jonathan Leffler
GCC version 4.3.4
Chubsdad
What changed in the source to g++ near the string "reference to %s is ambiguous" between version 4.2.1 and 4.5.1?
Thomas L Holaday
A: 

My first guess was one error per ambiguity:

#include <iostream>

using namespace std;

struct A{
   virtual void f1() = 0;
};

struct B : A{
};

struct C : A{
   void f1(){}
};



struct CPrime : A{
   void f1() {}
};

struct D : C, B, CPrime {
   void f2(){f1();}
};

int main(){ return 0; }

But no:

g++ prog.cpp
prog.cpp: In member function ‘void D::f2()’:
prog.cpp:20: error: reference to ‘f1’ is ambiguous
prog.cpp:5: error: candidates are: virtual void A::f1()
prog.cpp:16: error:                 virtual void CPrime::f1()
prog.cpp:5: error:                 virtual void A::f1()
prog.cpp:12: error:                 virtual void C::f1()
prog.cpp:20: error: reference to ‘f1’ is ambiguous
prog.cpp:5: error: candidates are: virtual void A::f1()
prog.cpp:16: error:                 virtual void CPrime::f1()
prog.cpp:5: error:                 virtual void A::f1()
prog.cpp:12: error:                 virtual void C::f1()
Thomas L Holaday
A: 

With gcc use -fmessage-length=0 to avoid breaking messages. http://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Language-Independent-Options.html#index-diagnostic-messages-211

Maxim Yegorushkin
g++ 4.2.1 still duplicates the error message even with that switch.
Thomas L Holaday