expected primary expression

This is a discussion on expected primary expression within the C++ Programming forums, part of the General Programming Boards category; my program wont compile because of an error i cant solve.. i have and c++ file that defines all of ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    expected primary expression

    my program wont compile because of an error i cant solve.. i have and c++ file that defines all of my class funtions, but in every function definition i have an error (sometimes 2 or 3) per function saying "primary expression expected before ")"" etc etc.. im stumped on how to debug this..
    Code:
    void room::Init(int newX, int newY, int newWidth, int newBreadth)
    {
        X = newX;
        Y = newY;
        Width = newWidth + 1; // This is so the room is as wide as we told it to be
        Breadth = newBreadth + 1; // This is so the room is as tall as we told it to be
        ASCIIMan.Init(X + Width - 2, Y + Breadth - 2, PlayerASCII);
    }
    
    int room::getBreadth()
    {
        return Breadth;
    }
    
    int room::getWidth()
    {
        return Width;
    }
    
    int room::getX()
    {
        return X;
    }
    
    int room::getY()
    {
        return Y;
    }
    
    bool room::isWalkable(char newASCIIChar)
    {
        int tile = newASCIIChar;
    
        if (tile == FloorTileASCII ||
            tile == PlayerASCII)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    Character room::getPlayer()
    {
        return ASCIIMan;
    }
    
    void room::draw()
    {
        int drawX = X;
        int drawY = Y;
    
        // Draw the floor tiles
        while(drawX < X + Width)
        {
            while(drawY < Y + Breadth)
            {
                // Draw tile drawX, drawY
                console.Draw(drawX, drawY, FloorTileASCII);
                drawY++;
            }
            drawX++;
            drawY = Y;
        }
    
        // Draw the walls
        drawX = X;
        while(drawX < X + Width)
        {
            console.Draw(drawX, Y, HorizontalWallASCII);
            console.Draw(drawX, Y + Breadth, HorizontalWallASCII);
            drawX++;
        }
        drawY = Y;
        while(drawY < Y + Breadth)
        {
            console.Draw(X, drawY, VerticalWallASCII);
            console.Draw(X + Width, drawY, VerticalWallASCII);
            drawY++;
        }
    
        // Draw the corners
        console.Draw(X, Y, TopLeftCornerASCII);
        console.Draw(X + Width, Y, TopRightCornerASCII);
        console.Draw(X + Width, Y + Breadth, BotRightCornerASCII);
        console.Draw(X, Y + Breadth, BotLeftCornerASCII);
    }void room::Init(int newX, int newY, int newWidth, int newBreadth)
    {
        X = newX;
        Y = newY;
        Width = newWidth + 1; // This is so the room is as wide as we told it to be
        Breadth = newBreadth + 1; // This is so the room is as tall as we told it to be
        ASCIIMan.Init(X + Width - 2, Y + Breadth - 2, PlayerASCII);
    }
    
    int room::getBreadth()
    {
        return Breadth;
    }
    
    int room::getWidth()
    {
        return Width;
    }
    
    int room::getX()
    {
        return X;
    }
    
    int room::getY()
    {
        return Y;
    }
    
    bool room::isWalkable(char newASCIIChar)
    {
        int tile = newASCIIChar;
    
        if (tile == FloorTileASCII ||
            tile == PlayerASCII)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    Character room::getPlayer()
    {
        return ASCIIMan;
    }
    
    void room::draw()
    {
        int drawX = X;
        int drawY = Y;
    
        // Draw the floor tiles
        while(drawX < X + Width)
        {
            while(drawY < Y + Breadth)
            {
                // Draw tile drawX, drawY
                console.Draw(drawX, drawY, FloorTileASCII);
                drawY++;
            }
            drawX++;
            drawY = Y;
        }
    
        // Draw the walls
        drawX = X;
        while(drawX < X + Width)
        {
            console.Draw(drawX, Y, HorizontalWallASCII);
            console.Draw(drawX, Y + Breadth, HorizontalWallASCII);
            drawX++;
        }
        drawY = Y;
        while(drawY < Y + Breadth)
        {
            console.Draw(X, drawY, VerticalWallASCII);
            console.Draw(X + Width, drawY, VerticalWallASCII);
            drawY++;
        }
    
        // Draw the corners
        console.Draw(X, Y, TopLeftCornerASCII);
        console.Draw(X + Width, Y, TopRightCornerASCII);
        console.Draw(X + Width, Y + Breadth, BotRightCornerASCII);
        console.Draw(X, Y + Breadth, BotLeftCornerASCII);
    }

    if anymore information is required.. just tell me.. ill edit this post.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i would recommend to post all code you have so that, if need be, we can copy/paste to files and try to compile on our own machines. i think it would be very hard to help you with what we have. perhaps the error isnt even in the code listed above? line numbers would be good (ie bold/color the lines that are being reported as errors).

    go through and make sure you dont have too many or too few brackets, make sure your semi colons are where theyre supposed to be, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Did you #include the room.h file? Did you spell room right?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Code:
    bool room::isWalkable(char newASCIIChar)
    {
        int tile = newASCIIChar;
    
        if (tile == FloorTileASCII ||
            tile == PlayerASCII)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    Character room::getPlayer()
    {
        return ASCIIMan;
    }
    
    ...
    
    void room::Init(int newX, int newY, int newWidth, int newBreadth)
    {
        X = newX;
        Y = newY;
        Width = newWidth + 1; // This is so the room is as wide as we told it to be
        Breadth = newBreadth + 1; // This is so the room is as tall as we told it to be
        ASCIIMan.Init(X + Width - 2, Y + Breadth - 2, PlayerASCII);
    }
    
    int room::getBreadth()
    {
        return Breadth;
    }
    
    int room::getWidth()
    {
        return Width;
    }
    
    int room::getX()
    {
        return X;
    }
    
    int room::getY()
    {
        return Y;
    }
    
    bool room::isWalkable(char newASCIIChar)
    {
        int tile = newASCIIChar;
    
        if (tile == FloorTileASCII ||
            tile == PlayerASCII)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    You can get rid of one of those.

    [edit]Actually, that's only one of the many repeats that there are, get rid of the extras.[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    there are repeats? i could have SWORN i checked every line of code there 2 - 3 times.. ill get rid of the extras.. then ill post most of my code if it doesnt work.. thank you for the speedy replies.

    EDITk. the repeats are only in the forum because of my own mistake.. and im not able to edit my first post for.. whatever reason. so ill add it all to here.

    first. the contents of main:
    Code:
    #include "headers.h"
    
    
    room MyRoom;
    
    int main()
    {
        MyRoom.Init(10, 10, 8, 4);
        Character ASCIIMan = MyRoom.getPlayer();
        int PlayerX;
        int PlayerY;
        while(1)
        {
    
               MyRoom.draw();
               PlayerX = ASCIIMan.GetX();
               PlayerY = ASCIIMan.GetY();
    
               char PressedKey = console.ReadKey();
               switch(PressedKey){
                                  case Down:
                                       if (MyRoom.isWalkable(console.GetASCIITile(PlayerX, PlayerY+1)))
                                       {
                                                      ASCIIMan.Walk(PlayerX, PlayerY+1);
                                                      };
                                                      };
    
               console.UpdateScreen();
        };
        return 0;
    };
    and the contents of character.h
    Code:
    #ifndef CHARACTER_
    #define CHARACTER_
    #include "headers.h"
    
    class Character
    {
          public:
          //methods:
                    void Init(int NewX, int NewY, char NewChar);
                    void Draw();
                    int Walk(int newX, int newY);
    
                    void Attack(Character);
    
                    int  GetHealth();
                    int  GetX();
                    int  GetY();
                    char GetName();
    
          private:
          //attributes
                    char Name;
                    int  Health;
                    int  X;
                    int  Y;
                    char ASCIICharacter;
    };
    #endif
    and the code for character.cpp
    Code:
    #include "headers.h"
    
    void Character::Init(int NewX, int NewY, char NewChar)
    {
    X = NewX;
    Y = NewY;
    ASCIICharacter = NewChar;
    };
    
    void Character::Draw()
    {
    console.Draw(X, Y, ASCIICharacter);
    };
    
    int Character::Walk(int newX, int newY)
    {
    X = newX;
    Y = newY;
    };
    
    int Character::GetHealth()
    {
    return Health;
    };
    
    int Character::GetX()
    {
    return X;
    };
    
    int Character::GetY()
    {
    return Y;
    };
    
    char Character::GetName()
    {
    return Name;
    };
    console.h
    Code:
    class ConsoleWindow
    {
    public:
    
       // constructor
       ConsoleWindow();
    
       // Draws an ASCII character to given location on backbuffer.
       void     Draw(int nX, int nY, char chrAscii);
    
       // Draws text at given location on backbuffer.
       void     DrawText(int nX, int nY, const char* szText);
    
       // Gets the character at a given location on screen.
       char     GetASCIITile(int nX, int nY);
    
       // Flips buffers. Actually draws the entire screen
       void     UpdateScreen();
    
       // Reads a key from keyboard.
       char     ReadKey();
    
    private:
    
        HANDLE   GetActiveBuffer();
        // Hides cursor on-screen.
        void     HideCursor();
    
        HANDLE  hBackBuffer;
        HANDLE  hFrontBuffer;
        int     nActiveBuffer;
    };
    
    extern ConsoleWindow console;
    and console.cpp
    Code:
    #include "headers.h"
    
    ConsoleWindow::ConsoleWindow()
    {
       hBackBuffer = CreateConsoleScreenBuffer(
            GENERIC_READ | GENERIC_WRITE,
            FILE_SHARE_READ,
            NULL,
            CONSOLE_TEXTMODE_BUFFER,
            NULL
        );
    
       hFrontBuffer = CreateConsoleScreenBuffer(
            GENERIC_READ | GENERIC_WRITE,
            FILE_SHARE_READ,
            NULL,
            CONSOLE_TEXTMODE_BUFFER,
            NULL
        );
    
        HideCursor();
        UpdateScreen();
    
    }
    
    void ConsoleWindow::HideCursor()
    {
      CONSOLE_CURSOR_INFO ci = {10,FALSE}; /* bVisible = FALSE */
      HANDLE hStdout = hBackBuffer;
      SetConsoleCursorInfo(hStdout, &ci);
      hStdout = hFrontBuffer;
      SetConsoleCursorInfo(hStdout, &ci);
    }
    
    void ConsoleWindow::Draw(int nX, int nY, char chrAscii)
    {
        if(nX<0 || nX > 79)
            return;
        if(nY<0 || nY > 23)
            return;
    
        COORD coordScreen;
          DWORD dwWritten;
    
        HANDLE hStdout = GetActiveBuffer();
    
        coordScreen.X=nX;
        coordScreen.Y=nY;
    
        SetConsoleCursorPosition(hStdout, coordScreen);
    
        DWORD dw;
        WriteConsole(hStdout, &chrAscii, 1, &dw, NULL);
    }
    
    void ConsoleWindow::DrawText(int nX, int nY, const char* szText)
    {
       COORD coordScreen;
       HANDLE hStdout = GetActiveBuffer();
       coordScreen.X=nX;
       coordScreen.Y=nY;
    
       SetConsoleCursorPosition(hStdout, coordScreen);
    
       DWORD dw;
       WriteConsole(hStdout, szText, strlen(szText), &dw, NULL);
    
      coordScreen.X=79;
      coordScreen.Y=24;
      SetConsoleCursorPosition(hStdout, coordScreen);
    
    }
    
    char     ConsoleWindow::GetASCIITile(int nX, int nY)
    {
      char chr;
      COORD coordScreen;
    
      HANDLE hStdout = GetActiveBuffer();
    
      coordScreen.X=nX;
      coordScreen.Y=nY;
    
      DWORD dw;
      ReadConsoleOutputCharacter(
            hStdout,
            &chr,
            1,
            coordScreen,
            &dw
            );
    
       return chr;
    
    }
    
    HANDLE   ConsoleWindow::GetActiveBuffer()
    {
        if(nActiveBuffer==0)
        {
            return hFrontBuffer;
        }
        else
        {
            return hBackBuffer;
        }
    
    }
    
    void ConsoleWindow::UpdateScreen()
    {
        SetConsoleActiveScreenBuffer(GetActiveBuffer());
        nActiveBuffer  = !nActiveBuffer;
    
    }
    
    char ConsoleWindow::ReadKey()
    {
        COORD coordScreen;
    
        coordScreen.X=79;
        coordScreen.Y=24;
    
        HANDLE hStdout = hBackBuffer;
    
        if(nActiveBuffer==0)
        {
    //         nActiveBuffer = 1;
            hStdout = hFrontBuffer;
        }
    
        SetConsoleCursorPosition(hStdout, coordScreen);
    
       return _getch();
    }
    
    ConsoleWindow console;
    room.h
    Code:
    #ifndef ROOM_
    #define ROOM_
    #include "headers.h"
    #include "character.h"
    #include "define.h"
    
    class room
    {
        public:
            void Init(int newX, int newY, int newWidth, int newBreadth);
            int getBreadth();
            int getWidth();
            int getX();
            int getY();
            bool isWalkable(char newASCIIChar);
            Character getPlayer();
            void draw();
    
        private:
            int Width;
            int Breadth;
            int X;
            int Y;
            Character ASCIIMan;
    };
    
    void room::Init(int newX, int newY, int newWidth, int newBreadth){
        X = newX;
        Y = newY;
        Width = newWidth + 1; // This is so the room is as wide as we told it to be
        Breadth = newBreadth + 1; // This is so the room is as tall as we told it to be
        ASCIIMan.Init(X + Width - 2, Y + Breadth - 2, PlayerASCII);
    }
    
    int room::getBreadth()
    {
        return Breadth;
    }
    
    int room::getWidth()
    {
        return Width;
    }
    
    int room::getX()
    {
        return X;
    }
    
    int room::getY()
    {
        return Y;
    };
    
    bool room::isWalkable(char newASCIIChar)
    {
        int tile = newASCIIChar;
    
        if (tile == FloorTileASCII ||
            tile == PlayerASCII)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    Character room::getPlayer()
    {
        return ASCIIMan;
    }
    
    void room::draw()
    {
        int drawX = X;
        int drawY = Y;
    
        // Draw the floor tiles
        while(drawX < X + Width)
        {
            while(drawY < Y + Breadth)
            {
                // Draw tile drawX, drawY
                console.Draw(drawX, drawY, FloorTileASCII);
                drawY++;
            }
            drawX++;
            drawY = Y;
        }
    
        // Draw the walls
        drawX = X;
        while(drawX < X + Width)
        {
            console.Draw(drawX, Y, HorizontalWallASCII);
            console.Draw(drawX, Y + Breadth, HorizontalWallASCII);
            drawX++;
        }
        drawY = Y;
        while(drawY < Y + Breadth)
        {
            console.Draw(X, drawY, VerticalWallASCII);
            console.Draw(X + Width, drawY, VerticalWallASCII);
            drawY++;
        }
    
        // Draw the corners
        console.Draw(X, Y, TopLeftCornerASCII);
        console.Draw(X + Width, Y, TopRightCornerASCII);
        console.Draw(X + Width, Y + Breadth, BotRightCornerASCII);
        console.Draw(X, Y + Breadth, BotLeftCornerASCII);
    }
    #endif
    earlier i bundled my room.h and room.cpp to make it easier to debug instead of going through multiple files.

    and lastly, the code for "headers.h"
    Code:
    #ifndef _HEADERS_
    #define _HEADERS_
    
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <wincon.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include "console.h"
    #include "room.h"
    
    #define PlayerASCII         = 0x01;    // ASCII Man
    #define DuckASCII           = 0x44;    // ASCII Duck
    #define VerticalWallASCII   = 0xBA;    // Vertical wall piece
    #define HorizontalWallASCII = 0xCD;    // Horizontal wall piece
    #define TopRightCornerASCII = 0xBB;    // Top right wall piece
    #define TopLeftCornerASCII  = 0xC9;    // Top left wall piece
    #define BotLeftCornerASCII  = 0xC8;    // Bottom right wall piece
    #define BotRightCornerASCII = 0xBC;    // Bottom left wall piece
    #define FloorTileASCII      = 0x2E;    // Floor tile 
    #endif
    headers.cpp
    Code:
    //nothing yet.
    and defines.h
    Code:
    #define PlayerASCII         = 0x01;    // ASCII Man
    #define DuckASCII           = 0x44;    // ASCII Duck
    #define VerticalWallASCII   = 0xBA;    // Vertical wall piece
    #define HorizontalWallASCII = 0xCD;    // Horizontal wall piece
    #define TopRightCornerASCII = 0xBB;    // Top right wall piece
    #define TopLeftCornerASCII  = 0xC9;    // Top left wall piece
    #define BotLeftCornerASCII  = 0xC8;    // Bottom right wall piece
    #define BotRightCornerASCII = 0xBC;    // Bottom left wall piece
    #define FloorTileASCII      = 0x2E;    // Floor tile
    if its of any concelation. im currently using code::blocks but the code was originally from from dev-cpp with the same errors.

    and the errors
    Project : Console application
    Compiler : GNU GCC Compiler (called directly)
    Directory : C:\Documents and Settings\john\My Documents\
    --------------------------------------------------------------------------------
    Switching to target: default
    Compiling: console.cpp
    In file included from headers.h:12,
    from console.cpp:1:
    room.h: In member function `void room::Init(int, int, int, int)':
    room.h:31: error: `PlayerASCII' undeclared (first use this function)
    room.h:31: error: (Each undeclared identifier is reported only once for each function it appears in.)
    room.h: In member function `bool room::isWalkable(char)':
    room.h:58: error: `FloorTileASCII' undeclared (first use this function)
    room.h:59: error: `PlayerASCII' undeclared (first use this function)
    room.h: In member function `void room::draw()':
    room.h:85: error: `FloorTileASCII' undeclared (first use this function)
    room.h:96: error: `HorizontalWallASCII' undeclared (first use this function)
    room.h:103: error: `VerticalWallASCII' undeclared (first use this function)
    room.h:109: error: `TopLeftCornerASCII' undeclared (first use this function)
    room.h:110: error: `TopRightCornerASCII' undeclared (first use this function)
    room.h:111: error: `BotRightCornerASCII' undeclared (first use this function)
    room.h:112: error: `BotLeftCornerASCII' undeclared (first use this function)
    Process terminated with status 1 (0 minutes, 4 seconds)
    11 errors, 0 warnings
    Last edited by lilhawk2892; 11-22-2007 at 06:03 AM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    My guess is that FloorTileASCII and related aren't defined. Indeed, I can't find them anywhere in your code.

    Furthermore, you have all the room methods out-of-line in a header. This will lead to linker errors.

    Then, you use include guards like _ROOM_. You're not allowed to use identifiers that start with a double underscore or an underscore followed by an uppercase letter.

    Your include guards should also go around the entire file, including any includes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    thank you.. for the past little while ive been doing some tinkering. so the defines were there at one point.. things just got a bit hectic.. one moment ill put them in. they should go in the header file

    and the linker errors. this was my FIRST idea. because only that file wouldnt work. and character.cpp seemed to work fine. if you wouldnt mind. could you help me reorder them?
    Last edited by lilhawk2892; 11-22-2007 at 04:33 AM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It's not about reordering them. Put them in their own .cpp file. (But you might want to keep the single-line accessors and just make them inline.)

    Still, the linker isn't even invoked in your build process currently, because the compiler already fails.


    You should also fix the missing newline warnings. GCC 4.3 will flag that as a hard error by default. Just insert a newline at the end of the affected files. (Probably after the #endif of the include guards.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    put them in there own .cpp file? but should what could possibly need to go in the cpp file? i thought a .h would suffice?

    oh but i did add newlines into my project in the compiler. not on the forum. ill just delete the newline errors from the post.

    edit ok. im not sure how or why it happened.. but the random expected primary expression errors have been dealt with. ill update the post with the new info. now my only problem is with the defines.
    Last edited by lilhawk2892; 11-22-2007 at 06:00 AM.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Not sure about those - probably it doesn't actually include the header - but anyway, #define constants don't have a =.
    #define PlayerASCII 0x01

    Better yet, use real constants.
    const char PlayerASCII = '\x1';
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    WOOOO for the first time in a week. i have a build with absolutely NO unmanageable errors. feels nice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected primary expression before "." token
    By melodious in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 04:39 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21