Compiler Fehler

Gerade am Anfang ist es schwer zu verstehen was der Compiler uns mit einer Fehlermeldung sagen will. Wir wollen uns deshalb 3 Compiler näher ansehen: den g++ der GNU Compiler Collection (Version 3.2.3), den frei erhätlichen Borland C++ Compiler (Version 5.6) und den Microsoft Visual C++ Compiler (cl) (Version 7.0).

Ich habe die Compiler mit folgenden Flags aufgerufen:

g++ -W -Wall -pedantic -std=c++98

bcc32 -w

cl /Zc:forScope /W4 /Wp64 /Za

Ich habe das höchste Warnlevel gewählt - das sollte jeder verwenden. Denn in C++ werden die meisten Fehler zur Compiletime erkannt, deshalb sollte man gerade hier dem Compiler soviele Warnungen wie möglich entlocken. Die meisten Compiler bieten noch weitere Optionen zur Erkennung von Fehlern: am besten siehst du dir die Dokumentation zu deinem Compiler an. Ich habe nur die gängigsten Optionen verwenden - das fein Tuning musst du selber machen ;).

01.cpp

    #include <iostream>
    using namespace std;

    class Foo
    {
    private:
      int i;
    public:
      Foo(int i) : i(i)
      {}

      int get() const
      {
        return i;
      }
    }

    int main()
    {
      Foo f(3);
      cout<<f.get()<<'\n';
    }
    

Fehlerbeschreibung: Wir haben das ; nach der Klassendefinition vergessen.

g++:

01.cpp:18: semicolon missing after declaration of `Foo'
01.cpp:19: warning: ISO C++ forbids defining types within return type
01.cpp:19: extraneous `int' ignored
01.cpp:19: `main' must return `int'
01.cpp:19: semicolon missing after declaration of `class Foo'

cl:

01.cpp
c:\fehler\01.cpp(18) : error C2628: 'Foo' followed by 'int' is illegal (did you forget a ';'?)
c:\fehler\01.cpp(19) : warning C4326: return type of 'main' should be 'int' instead of 'Foo'

bcc32:

Error E2176 01.cpp 18: Too many types in declaration
Error E2111 01.cpp 19: Type 'Foo' may not be defined here

02.cpp

    #include <iostream>
    using namespace std;

    int main()
    {
      int i;
      cin>>i;
      switch(i)
      {
      case 2:
      case 4:
      case 6:
      case 8:
      case 10:
        int t=i/2;
        cout<<t;
        break;

      default:
        cout<<"nicht durch 2 teilbar";
      }
      cout<<'\n';
    }
    

Fehlerbeschreibung: Innerhalb eines case Labels darf man keine Variablen deklarieren. Wir müssten einen neuen Block aufmachen: case '0': { int i; }.

g++:

02.cpp: In function `int main()':
02.cpp:19: warning: jump to case label
02.cpp:15:   crosses initialization of `int t'

cl:

02.cpp
c:\fehler\02.cpp(19) : error C2361: initialization of 't' is skipped by 'default' label
        c:\fehler\02.cpp(15) : see declaration of 't'

bcc32:

Error E2126 02.cpp 19: Case bypasses initialization of a local variable in funct
ion main()

03.cpp

    #include <iostream>
    using namespace std;

    class Foo
    {
    private:
      int i;
    public:
      Foo(int i=0) : i(i)
      {}

      int get() const
      {
        return i;
      }
    };

    int main()
    {
      Foo f();
      cout<<f.get()<<'\n';
    }
    

Fehlerbeschreibung: Foo f(); deklariert eine Funktion (vergleiche: int func();). Wir wollten Foo f; schreiben.

g++:

03.cpp: In function `int main()':
03.cpp:21: request for member `get' in `f()', which is of non-aggregate type `
   Foo ()()'

cl:

03.cpp
c:\fehler\03.cpp(21) : error C2228: left of '.get' must have class/struct/union type

bcc32:

Error E2294 03.cpp 21: Structure required on left side of . or .* in function ma
in()

04.cpp

    void divide(int a, int b, int& res)
    {
      res=a/b;
    }

    int foo(int const& i)
    {
      return divide(3,2,i);
    }

    int main()
    {
      int res;
      foo(res);
    }
    

Fehlerbeschreibung: i ist innerhalb von foo konstant, wir übergeben es aber an divide, welches ein nicht konstantes i erwartet.

g++:

04.cpp: In function `int foo(const int&)':
04.cpp:8: could not convert `i' to `int&'
04.cpp:2: in passing argument 3 of `void divide(int, int, int&)'

cl:

04.cpp
c:\fehler\04.cpp(8) : error C2664: 'divide' : cannot convert parameter 3 from 'const int' to 'int &'
        Conversion loses qualifiers

bcc32:

Warning W8030 04.cpp 8: Temporary used for parameter 'res' in call to 'divide(in
t,int,int &)' in function foo(const int &)
Warning W8057 04.cpp 9: Parameter 'i' is never used in function foo(const int &)

05.cpp

    #include <iostream>
    using namespace std;

    class Foo
    {
    private:
      int i;
    public:
      Foo(int i) : i(i)
      {}

      int get() const
      {
        return i;
      }
    };

    int main()
    {
      Foo* p=new Foo(3);
      cout<<p.get()<<'\n';
      delete p;
    }
    

Fehlerbeschreibung: p ist ein Zeiger, wir müssen deshalb p->get(); und nicht p.get(); schreiben.

g++:

05.cpp: In function `int main()':
05.cpp:21: request for member `get' in `p', which is of non-aggregate type `
   Foo*'

cl:

05.cpp
c:\fehler\05.cpp(21) : error C2228: left of '.get' must have class/struct/union type

bcc32:

Error E2294 05.cpp 21: Structure required on left side of . or .* in function ma
in()

06.cpp

    #include <iostream>
    using namespace std;

    int const& divide(int a, int b)
    {
      return a/b;
    }

    int main()
    {
      cout<<divide(3,2);
    }
    

Fehlerbeschreibung: Wir returnen eine Referenz auf ein lokales Objekt. Dies ist kein Fehler ansich, allerdings erzeugt die Zeile cout<<divide(3,2); undefiniertes Verhalten, da das Objekt nicht mehr existiert.

g++:

06.cpp: In function `const int& divide(int, int)':
06.cpp:6: warning: returning reference to temporary

cl:

06.cpp
c:\fehler\06.cpp(6) : warning C4172: returning address of local variable or temporary

bcc32:

Error E2357 06.cpp 6: Reference initialized with 'int', needs lvalue of type 'in
t' in function divide(int,int)
Warning W8057 06.cpp 7: Parameter 'a' is never used in function divide(int,int)
Warning W8057 06.cpp 7: Parameter 'b' is never used in function divide(int,int)

07.cpp

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int main()
    {
      string file;
      cin>>file;
      ifstream f(file);

      //do something
    }
    

Fehlerbeschreibung: ifstream hat keinen Konstruktor der einen string nimmt, wir müssten ifstream f(file.c_str()); schreiben.

g++:

07.cpp: In function `int main()':
07.cpp:9: no matching function for call to `std::basic_ifstream<char,
   std::char_traits<char> >::basic_ifstream(std::string&)'
C:/mingw/include/c++/3.2.3/iosfwd:86: candidates are: std::basic_ifstream<char,

   std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char,
   std::char_traits<char> >&)
C:/mingw/include/c++/3.2.3/fstream:351:
   std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*,
   std::_Ios_Openmode = std::ios_base::in) [with _CharT = char, _Traits =
   std::char_traits<char>]
C:/mingw/include/c++/3.2.3/fstream:338:
   std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char,
   _Traits = std::char_traits<char>]

cl:

07.cpp
c:\fehler\07.cpp(9) : error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const char
*,std::_Iosb<_Dummy>::openmode)' : cannot convert parameter 1 from 'std::string' to 'const char *'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Dummy=int
        ]
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot
        be called

bcc32:

Error E2285 07.cpp 9: Could not find a match for 'ifstream::basic_ifstream(strin
g)' in function main()
Warning W8004 07.cpp 12: 'f' is assigned a value that is never used in function
main()

08.cpp

    #include <iostream>
    using namespace std;

    int a1=0, b1=0, c1=0, a1=0, b2=0, c2=0;

    int main()
    {
      cin>>a1>>b1>>c1>>a1>>b2>>c2;
    }
    

Fehlerbeschreibung: Wir haben die Variable a1 doppelt definiert.

g++:

08.cpp:4: redefinition of `int a1'
08.cpp:4: `int a1' previously defined here

cl:

08.cpp
c:\fehler\08.cpp(4) : error C2374: 'a1' : redefinition; multiple initialization
        c:\fehler\08.cpp(4) : see declaration of 'a1'

bcc32:

Error E2238 08.cpp 4: Multiple declaration for 'a1'
Error E2344 08.cpp 4: Earlier declaration of 'a1'

09.cpp

    void foo(float);

    int main()
    {
      foo(3.0);
    }

    void foo(double)
    {
      //bla
    }
    

Fehlerbeschreibung: Wir haben foo(float); deklariert aber foo(double); definiert.

g++:

C:\DOKUME~1\ADMINI~1\LOKALE~1\Temp/cc23aaaa.o(.text+0x27):09.cpp: undefined refe
rence to `foo(float)'

cl:

09.obj : error LNK2019: unresolved external symbol "void __cdecl foo(float)" (?foo@@YAXM@Z) referenced in
 function _main
Debug/fehler.exe : fatal error LNK1120: 1 unresolved externals

bcc32:

Error: Unresolved external 'foo(float)' referenced from C:\FEHLER\09.OBJ

10.cpp

    #include <iostream>
    using namespace std;

    void print(int i);
    {
      cout<<i<<'\n';
    }

    int main()
    {
      print(3);
    }
    

Fehlerbeschreibung: Wir haben nach dem Funktionskopf von print ein ; geschrieben.

g++:

10.cpp:5: parse error before `{' token

cl:

10.cpp
c:\fehler\10.cpp(5) : error C2447: '{' : missing function header (old-style formal list?)

bcc32:

Error E2040 10.cpp 5: Declaration terminated incorrectly

11.cpp

    #include <iostream>
    using namespace std;

    int main()
    {
      int i;
      cout<<i*2<<'\n';
    }
    

Fehlerbeschreibung: Wir haben i verwendet, bevor wir i einen Wert zugewiesen haben. Dies ist nicht direkt ein Fehler, allerdings ist die Ausgabe des Programms undefiniert.

g++:

no message

cl:

11.cpp
c:\fehler\11.cpp(7) : warning C4700: local variable 'i' used without having been initialized

bcc32:

Warning W8013 11.cpp 7: Possible use of 'i' before definition in function main()

12.cpp


    template <class T>
    struct Foo
    {
        typedef T::Type Type;
    };

    struct S
    {
      typedef int Type;
    };

    int main()
    {
      Foo<S>::Type i;
    }
    

Fehlerbeschreibung: T::Type ist ein 'dependant name'. Wir müssen deshalb typedef typename T::Type Type; schreiben.

g++:

12.cpp:5: warning: ISO C++ forbids declaration of `Type' with no type
12.cpp:5: `::Type' is not a valid declarator
12.cpp:5: parse error before `;' token
12.cpp: In function `int main()':
12.cpp:15: `Type' is not a member of type `Foo<S>'
12.cpp:15: parse error before `;' token

cl:

12.cpp
c:\fehler\12.cpp(5) : error C2146: syntax error : missing ';' before identifier 'Type'
        c:\fehler\12.cpp(6) : see reference to class template instantiation 'Foo<T>' being compiled
c:\fehler\12.cpp(5) : error C3254: 'Foo<T>' : class contains explicit override 'T::Type' but does not
 derive from an interface that contains the function declaration
c:\fehler\12.cpp(5) : error C2838: 'Type' : illegal qualified name in member declaration

bcc32:

Error E2139 12.cpp 10: Declaration missing ;
Warning W8080 12.cpp 16: 'i' is declared but never used in function main()

13.cpp

    #include <iostream>
    using namespace std;

    int zahl()
    {
      return 3;
    }

    int main()
    {
      cout<<zahl;
    }
    

Fehlerbeschreibung: zahl ist ein Zeiger auf eine Funktion (genauso wie int* p; ein Zeiger auf einen int ist) - wir müssen sie deshalb erst dereferenzieren (aufrufen): zahl();. Der Code ist nicht Fehlerhaft, aber die Funktion wird nicht aufgerufen (was wir eigentlich wollen).

g++:

13.cpp: In function `int main()':
13.cpp:11: warning: the address of `int zahl()', will always be `true'

cl:

13.cpp
c:\fehler\13.cpp(11) : warning C4305: 'argument' : truncation from 'int (*)(void)' to 'std::_Bool'
c:\fehler\13.cpp(11) : warning C4800: 'int (*)(void)' : forcing value to bool 'true' or 'false' (performance
 warning)

bcc32:

no message

14.cpp

    #include <iostream>
    #include <string>
    using namespace std;

    int main()
    {
      string s;
      cin>>s;
      int len;
      cin>>len;

      if(s.size()<len)
        cout<<"zu kurz";
    }
    

Fehlerbeschreibung: len ist ein int und s.size() liefert einen size_t (welcher ein unsigned int-Typ ist). Wir vergleichen also signed mit unsigned - das kann zu Problemen führen, wenn wir zB -1 eingeben. Dies ist aber dennoch kein Fehler, allerdings ist das Ergebnis nicht immer das Gewünschte.

g++:

14.cpp: In function `int main()':
14.cpp:12: warning: comparison between signed and unsigned integer expressions

cl:

14.cpp
c:\fehler\14.cpp(12) : warning C4018: '<' : signed/unsigned mismatch

bcc32:

14.cpp:
Warning W8012 14.cpp 12: Comparing signed and unsigned values in function main()

15.cpp

    #include <iostream>
    using namespace std;

    int main()
    {
      for(unsigned char c=0; c<256; ++c)
        cout<<static_cast<int>(c)<<' '<<c<<'\n';
    }
    

Fehlerbeschreibung: Ein unsigned char geht auf der Testplattform (Win32) nur von 0 bis 255. Er kann deshalb nie "nicht kleiner als 256" sein. Dies ist kein Fehler, allerdings haben wir ungewollt eine Endlosschleife produziert.

g++:

no message

cl:

no message

bcc32:

15.cpp:
Warning W8068 15.cpp 6: Constant out of range in comparison in function main()

16.cpp

    #include <string>
    using namespace std;

    int main()
    {
      string s("hallo");
      string::const_iterator i=s.begin();
      s.erase(i);
    }

    

Fehlerbeschreibung: i ist ein const_iterator, aber s.erase() verlangt einen 'normalen' nicht konstanten iterator.

g++:

C:/mingw/include/c++/3.2.3/bits/stl_iterator.h: In constructor
   `__gnu_cxx::__normal_iterator<_Iterator,
   _Container>::__normal_iterator(const __gnu_cxx::__normal_iterator<_Iter,
   _Container>&) [with _Iter = const char*, _Iterator = char*, _Container =
   std::basic_string<char, std::char_traits<char>, std::allocator<char> >]':
16.cpp:8:   instantiated from here
C:/mingw/include/c++/3.2.3/bits/stl_iterator.h:589: warning: invalid conversion

   from `const char* const' to `char*'

cl:

16.cpp
c:\fehler\16.cpp(8) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::_Myt
 &std::basic_string<_Elem,_Traits,_Ax>::erase(std::basic_string<_Elem,_Traits,_Ax>::size_type,std::bas
 ic_string<_Elem,_Traits,_Ax>::size_type)' : cannot convert parameter 1 from
  'std::basic_string<_Elem,_Traits,_Ax>::const_iterator' to
   'std::basic_string<_Elem,_Traits,_Ax>::size_type'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        and
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        and
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be
	 called

bcc32:

16.cpp:
Error E2285 16.cpp 8: Could not find a match for 'string::erase(const char *)' i
n function main()
Warning W8004 16.cpp 9: 'i' is assigned a value that is never used in function m
ain()

17.cpp

    #include <map>
    using namespace std;

    struct Position
    {
      //...
    };

    int main()
    {
      map<Position, int> m;
      m.insert(make_pair(Position(), 3));
    }

    

Fehlerbeschreibung: Da eine map sortiert ist, braucht sie eine Möglichkeit die verschiedenen Schlüssel miteinander zu vergleichen. Defaultmäßig nimmt sie deshalb den operator< - welchen wir aber nicht implementiert haben.

g++:

C:/mingw/include/c++/3.2.3/bits/stl_function.h: In member function `bool
   std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp =
   Position]':
C:/mingw/include/c++/3.2.3/bits/stl_tree.h:1042:   instantiated from `std::pair<
std::_Rb_tree_iterator<_Val, _Val&, _Val*>, bool> std::_Rb_tree<_Key, _Val, _Key
OfValue, _Compare, _Alloc>::insert_unique(const _Val&) [with _Key = Position, _V
al = std::pair<const Position, int>, _KeyOfValue = std::_Select1st<std::pair<con
st Position, int> >, _Compare = std::less<Position>, _Alloc = std::allocator<std
::pair<const Position, int> >]'
C:/mingw/include/c++/3.2.3/bits/stl_map.h:245:   instantiated from `std::pair<st
d::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _K
ey, _Tp> >, _Compare, _Alloc>::iterator, bool> std::map<_Key, _Tp, _Compare, _Al
loc>::insert(const std::pair<const _Key, _Tp>&) [with _Key = Position, _Tp = int
, _Compare = std::less<Position>, _Alloc = std::allocator<std::pair<const Positi
on, int> >]'
17.cpp:12:   instantiated from here
C:/mingw/include/c++/3.2.3/bits/stl_function.h:197: no match for `const
   Position& < const Position&' operator

cl:

17.cpp
c:\Programme\Microsoft Visual Studio .NET\Vc7\include\functional(139) : error C2784: 'bool std::operator
 <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument
 for 'const std::_Tree<_Traits> &' from 'const Position'
        c:\Programme\Microsoft Visual Studio .NET\Vc7\include\functional(138) : while compiling class-template
	 member function 'bool std::less<_Ty>::operator ()(const _Ty &,const Position &) const'
        with
        [
            _Ty=Position
        ]
        c:\Programme\Microsoft Visual Studio .NET\Vc7\include\map(36) : see reference to class template
	 instantiation 'std::less<_Ty>' being compiled
        with
        [
            _Ty=Position
        ]
        c:\Programme\Microsoft Visual Studio .NET\Vc7\include\xtree(19) : see reference to class template
	 instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
        with
        [
            _Kty=Position,
            _Ty=int,
            _Pr=std::less<Position>,
            _Alloc=std::allocator<std::pair<const Position,int>>,
            _Mfl=false
        ]
        c:\Programme\Microsoft Visual Studio .NET\Vc7\include\xtree(61) : see reference to class template
	 instantiation 'std::_Tree_nod<_Traits>' being compiled
        with
        [
            _Traits=std::_Tmap_traits<Position,int,std::less<Position>,std::allocator<std::pair<con
	    st Position,int>>,false>
        ]
        c:\Programme\Microsoft Visual Studio .NET\Vc7\include\xtree(83) : see reference to class template
	 instantiation 'std::_Tree_ptr<_Traits>' being compiled
        with
        [
            _Traits=std::_Tmap_traits<Position,int,std::less<Position>,std::allocator<std::pair<con
	    st Position,int>>,false>
        ]
        c:\Programme\Microsoft Visual Studio .NET\Vc7\include\xtree(101) : see reference to class template
	 instantiation 'std::_Tree_val<_Traits>' being compiled
        with
        [
            _Traits=std::_Tmap_traits<Position,int,std::less<Position>,std::allocator<std::pair<con
	    st Position,int>>,false>
        ]
        c:\Programme\Microsoft Visual Studio .NET\Vc7\include\map(77) : see reference to class template
	 instantiation 'std::_Tree<_Traits>' being compiled
        with
        [
            _Traits=std::_Tmap_traits<Position,int,std::less<Position>,std::allocator<std::pair<con
	    st Position,int>>,false>
        ]
        c:\fehler\17.cpp(11) : see reference to class template instantiation 'std::map<_Kty,_Ty,_Pr,_Alloc>'
	 being compiled
        with
        [
            _Kty=Position,
            _Ty=int,
            _Pr=std::less<Position>,
            _Alloc=std::allocator<std::pair<const Position,int>>
        ]
c:\Programme\Microsoft Visual Studio .NET\Vc7\include\functional(139) : error C2784: 'bool std::operator
 <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt> &)' : could not deduce
  template argument for 'const std::reverse_iterator<_RanIt> &' from 'const Position'
c:\Programme\Microsoft Visual Studio .NET\Vc7\include\functional(139) : error C2784: 'bool std::operator
 <(const std::_Ptrit<_Ty,_Diff,_Pointer2,_Reference2,_Pointer2,_Reference2> &,const
  std::_Ptrit<_Ty,_Diff,_Pointer,_Reference,_Pointer2,_Reference2> &)' : could not deduce template argument
   for 'const std::_Ptrit<_Ty,_Diff,_Pointer2,_Reference2,_Pointer2,_Reference2> &' from 'const Position'
c:\Programme\Microsoft Visual Studio .NET\Vc7\include\functional(139) : error C2784: 'bool std::operator
 <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument
  for 'const std::pair<_Ty1,_Ty2> &' from 'const Position'
c:\Programme\Microsoft Visual Studio .NET\Vc7\include\functional(139) : error C2676: binary '<' : 'const
 Position' does not define this operator or a conversion to a type acceptable to the predefined operator

bcc32:

17.cpp:
Error E2093 C:\bcc\include\stl/_function_base.h 73: 'operator<' not im
plemented in type 'Position' for arguments of the same type in function less<Pos
ition>::operator ()(const Position &,const Position &) const

18.cpp

    class Foo
    {
    public:
      void callback( void (*func)(int) )
      {
        func(7);
      }

      void to_call(int) {}
    };

    int main()
    {
      Foo f;
      f.callback(&Foo::to_call);
    }

    

Fehlerbeschreibung: void (*func)(int) ist ein Zeiger auf eine Funktion, wir übergeben aber einen Zeiger auf eine Methode (void (Foo::*)(int)).

g++:

18.cpp: In function `int main()':
18.cpp:15: no matching function for call to `Foo::callback(void (Foo::*)(int))'
18.cpp:5: candidates are: void Foo::callback(void (*)(int))

cl:

18.cpp
c:\fehler\18.cpp(15) : error C2664: 'Foo::callback' : cannot convert parameter 1 from 'void (Foo::* )(int)' to
 'void (*)(int)'
        There is no context in which this conversion is possible

bcc32:

18.cpp:
Error E2034 18.cpp 15: Cannot convert 'void (Foo::*)(int)' to 'void (*)(int)' in
 function main()
Error E2342 18.cpp 15: Type mismatch in parameter 'func' (wanted 'void (*)(int)'
, got 'void (Foo::*)(int)') in function main()

19.cpp

    int foo(int i)
    {
      return i;
    }

    int main()
    {
      int* p=0;
      foo(p);
    }
    

Fehlerbeschreibung: foo verlangt einen int, wir übergeben einen int*.

g++:

19.cpp: In function `int main()':
19.cpp:9: warning: invalid conversion from `int*' to `int'

cl:

19.cpp
c:\fehler\19.cpp(9) : error C2664: 'foo' : cannot convert parameter 1 from 'int *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast

bcc32:

19.cpp:
Error E2034 19.cpp 9: Cannot convert 'int *' to 'int' in function main()
Error E2342 19.cpp 9: Type mismatch in parameter 'i' (wanted 'int', got 'int *')
 in function main()
Warning W8004 19.cpp 10: 'p' is assigned a value that is never used in function
main()

20.cpp

    int** foo(int** p)
    {
      return p;
    }

    int main()
    {
      int* p=0;
      foo(p);
    }

    

Fehlerbeschreibung: foo verlangt einen int*, wir übergeben aber einen int**

g++:

20.cpp: In function `int main()':
20.cpp:9: cannot convert `int*' to `int**' for argument `1' to `int**
   foo(int**)'

cl:

20.cpp
c:\fehler\20.cpp(9) : error C2664: 'foo' : cannot convert parameter 1 from 'int *' to 'int ** '
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

bcc32:

20.cpp:
Error E2034 20.cpp 9: Cannot convert 'int *' to 'int * *' in function main()
Error E2342 20.cpp 9: Type mismatch in parameter 'p' (wanted 'int * *', got 'int
 *') in function main()
Warning W8004 20.cpp 10: 'p' is assigned a value that is never used in function
main()

21.cpp

    #include __FILE__

    int main()
    {
    }
    

Fehlerbeschreibung: Wir haben eine Rekursion beim #include die nicht terminiert. Normalerweise includet man __FILE__ natürlich nicht, aber so eine Rekursion kann auch entstehen, wenn man die Includeguards bei Headerdateien vergisst.

g++:

sehr viele male:
21.cpp:4: redefinition of `int main()'
21.cpp:4: `int main()' previously defined here

cl:

21.cpp
c:\fehler\21.cpp(1) : warning C4182: #include nesting level is 545 deep; possible infinite recursion
c:\fehler\21.cpp(1) : fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher
 limit

bcc32:

21.cpp:
Fatal F1005 21.cpp 1: Include files nested too deep

22.cpp

    int main()
    {
      ifstream file("foobar");
    }
    

Fehlerbeschreibung: Wir haben vergessen <fstream> zu includen.

g++:

22.cpp: In function `int main()':
22.cpp:3: `ifstream' undeclared (first use this function)
22.cpp:3: (Each undeclared identifier is reported only once for each function
   it appears in.)
22.cpp:3: parse error before `(' token

cl:

22.cpp
c:\fehler\22.cpp(3) : error C2065: 'ifstream' : undeclared identifier
c:\fehler\22.cpp(3) : error C2146: syntax error : missing ';' before identifier 'file'
c:\fehler\22.cpp(3) : error C2065: 'file' : undeclared identifier

bcc32:

22.cpp:
Error E2451 22.cpp 3: Undefined symbol 'ifstream' in function main()
Error E2379 22.cpp 3: Statement missing ; in function main()

23.cpp

    class C;
    int main()
    {
      C c;
    }
    

Fehlerbeschreibung: Wir haben C nur deklariert, aber nicht definiert.

g++:

23.cpp: In function `int main()':
23.cpp:4: aggregate `C c' has incomplete type and cannot be defined
23.cpp:4: warning: unused variable ` c'

cl:

23.cpp
c:\fehler\23.cpp(4) : error C2079: 'c' uses undefined class 'C'

bcc32:

23.cpp:
Error E2450 23.cpp 4: Undefined structure 'C' in function main()
Error E2449 23.cpp 4: Size of 'c' is unknown or zero in function main()
Error E2450 23.cpp 4: Undefined structure 'C' in function main()
Warning W8080 23.cpp 5: 'c' is declared but never used in function main()

Wie wir sehen sind die Fehlermeldungen oft nicht klar. Im Prinzip hat der Compiler zwar fast immer recht, aber er drückt sich oft sehr undeutlich aus. Deshalb ist es wichtig, sich nicht zu sehr auf die Fehlermeldung zu versteifen, sondern zu versuchen die Stelle mit dem Fehler genauer zu analysieren. Hierbei gilt zu beachten, dass der Fehler auch 2-3 Zeilen vor der Stelle stattgefunden haben kann, an der der Compiler den Fehler meldet.

Oft hilft es auch über Google nach der Fehlermeldung zu suchen, da wir sicher nicht die Ersten sind, die so eine Fehlermeldung bekommen haben ;).

Sollten wir dennoch nicht mehr weiterwissen: in einem Forum, Chat oder Newsgroup fragen - und immer daran denken die exakte Fehlermeldung anzugeben.

Bei Fehlermeldungen mit Templates ist STLFilt oft sehr nützlich. Denn gerade Fehler mit Templates sind sehr schwer zu lesen - am besten man kopiert sich die Fehlermeldung in einen Texteditor und löscht die Templateparameter raus. So dass aus

C:/mingw/include/c++/3.2.3/bits/stl_iterator.h: In constructor
   `__gnu_cxx::__normal_iterator<_Iterator,
   _Container>::__normal_iterator(const __gnu_cxx::__normal_iterator<_Iter,
   _Container>&) [with _Iter = const char*, _Iterator = char*, _Container =
   std::basic_string<char, std::char_traits<char>, std::allocator<char> >]':
16.cpp:8:   instantiated from here
C:/mingw/include/c++/3.2.3/bits/stl_iterator.h:589: warning: invalid conversion

   from `const char* const' to `char*'

ein einfacher zu lesendes

stl_iterator.h: In constructor
   `iterator<char*>::iterator(const iterator<const char*>&)
16.cpp:8:   instantiated from here
stl_iterator.h:589: warning: invalid conversion
   from `const char* const' to `char*'

wird.

top