Seite 1 von 1

GCC: expected identifier before ‘;’ token

Verfasst: Do Mai 26, 2011 6:16 pm
von naums
Hallihallöchen,

Ich kriege von GCC für mich ziemlich unverständliche Fehlermeldung beim Kompilieren und zwar:
main.cpp:8: error: expected identifier before ‘;’ token
main.cpp:9: error: expected identifier before ‘;’ token
sorting.cpp:3: error: expected identifier before ‘;’ token
style.cpp:4: error: expected identifier before ‘;’ token
Auszugsweise:
main.cpp

Code: Alles auswählen

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "style.h"
#include "sorting.h"
#include "arch.h"

using namespace style;
using namespace sorting;
style.h:

Code: Alles auswählen

#ifndef style
#define style

namespace style {
    ...
}
#endif
style.cpp:

Code: Alles auswählen

#include "style.h"
#include <stdio.h>

using namespace style;
Was mache ich falsch? Beachte: Solche Probleme habe ich in der arch.cpp nicht. arch.h:

Code: Alles auswählen

#ifndef sorting_arch
#define sorting_arch

namespace arch{
arch.cpp:

Code: Alles auswählen

#include "arch.h"
#include "style.h"
#include <iostream>

using namespace arch;
Wo liegt also mein fehler?

MfG Naums

Re: GCC: expected identifier before ‘;’ token

Verfasst: Do Mai 26, 2011 6:56 pm
von Xin
Kannst Du die style.h mal komplett posten, bzw. prüfen, ob Du alle Klammern geschlossen hast?

Re: GCC: expected identifier before ‘;’ token

Verfasst: Do Mai 26, 2011 7:05 pm
von naums

Code: Alles auswählen

#ifndef style
#define style

namespace style {
    
    enum Colors{
        Schwarz,
        Rot,
        Gruen,
        Gelb,
        Blau,
        Magenta,
        Cyan,
        Weiss
    };

    void clrscr(void);
    void setBold(void);
    void UnsetAllAttributes(void);
    bool FontColor(Colors Color);
    bool BackgroundColor(Colors Color);
    void gotoxy(int x, int y);
    void home(void);
    
}

#endif

Re: GCC: expected identifier before ‘;’ token

Verfasst: Do Mai 26, 2011 8:03 pm
von Xin
Okay, ich denke ich habe das Problem.

Dein Code ist absolut korrekt. Aaaaber... der g++ sieht style als Schlüsselwort. Wenn Du den Namespace in "Style" umbenennst oder "mystyle" kompiliert es.

Ist "style" ein C++0x-Schlüsselwort?

Re: GCC: expected identifier before ‘;’ token

Verfasst: Do Mai 26, 2011 8:08 pm
von naums
Habe keine Ahnung... Zu Kompilieren gehts, aber der Linker meckert nun rum, also kann doch mein Quellcode nicht so dolle sein ;)

Was hat folgendes zu bedeuten:
/tmp/ccvmN5Y5.o: In function `main':
main.cpp:(.text+0x1c): undefined reference to `arch::menu()'
/tmp/ccrtxtUX.o: In function `home()':
style.cpp:(.text+0x2a): undefined reference to `Style::gotoxy(int, int)'
/tmp/ccTqk7jp.o: In function `menu()':
arch.cpp:(.text+0x7): undefined reference to `Style::clrscr()'
arch.cpp:(.text+0xc): undefined reference to `Style::home()'
arch.cpp:(.text+0x11): undefined reference to `Style::setBold()'
arch.cpp:(.text+0x2a): undefined reference to `Style::UnsetAllAttributes()'
arch.cpp:(.text+0x3e): undefined reference to `Style::gotoxy(int, int)'
arch.cpp:(.text+0x76): undefined reference to `Style::gotoxy(int, int)'
arch.cpp:(.text+0x9e): undefined reference to `Style::gotoxy(int, int)'
arch.cpp:(.text+0xc6): undefined reference to `Style::gotoxy(int, int)'
arch.cpp:(.text+0xee): undefined reference to `Style::gotoxy(int, int)'
/tmp/ccTqk7jp.o:arch.cpp:(.text+0x116): more undefined references to `Style::gotoxy(int, int)' follow
collect2: ld returned 1 exit status
Ich kompiliere mit:

Code: Alles auswählen

g++ -o sorting main.cpp sorting.cpp style.cpp arch.cpp
MfG Naums

Re: GCC: expected identifier before ‘;’ token

Verfasst: Do Mai 26, 2011 8:22 pm
von Xin
Hast Du die Funktionen in style.cpp in den gleichen Namespace gelegt?

Code: Alles auswählen

void Style::gotoxy( ... )
{
}
oder

Code: Alles auswählen

namespace Style
{
  void gotoxy(..)
  {
  }
}

Re: GCC: expected identifier before ‘;’ token

Verfasst: Do Mai 26, 2011 8:26 pm
von naums
ich hab using namespace Style benutzt... hm... okay nun scheints zu gehn. hoffe ich mal... jup.. er kompiliert... :)

Re: GCC: expected identifier before ‘;’ token

Verfasst: Do Mai 26, 2011 9:06 pm
von Kerli
Mit so allgemeinen Namen sollte man stets vorsichtig sein. Ich hatte auch einmal so ein ähnliches Problem das aber nur unter Windows aufgetreten ist. Ich habe Parameter einer Funktion mit 'near' und 'far' benannt und Windows hat irgendwo die beiden als leer definiert (#define far). Der Funktionskopf hat noch ganz normal kompiliert da ja Typen ohne Identifier erlaubt sind. Nur im Funktionsrumpf hat er plötzlich die eigenen Parameter nicht mehr gekannt.

Also wenn irgendetwas plötzlich nicht definiert ist, liegt es meistens daran dass eine andere Bibliothek das überschrieben hat :P
Xin hat geschrieben:Ist "style" ein C++0x-Schlüsselwort?
Nein. Wenn man ein Schlüsselwort verwendet sollte sich der Kompiler eigentlich beschweren. -> http://www.cppreference.com/wiki/keywords/start