Struct defining in function parameters?

This is a discussion on Struct defining in function parameters? within the C++ Programming forums, part of the General Programming Boards category; I'm work for a snake game in console mode, I want that my: struct Player{int x,y;}; gets in the funtion ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Question Struct defining in function parameters?

    I'm work for a snake game in console mode, I want that my:
    struct Player{int x,y;};
    gets in the funtion parameter:

    Code:
    void movedir(struct Player,int i)//strut Player has errors.
    {
        switch(i)
            {
                case '1':Player.x--;Player.y--;
                break;
                case '2':Player.Y--;
                break;
                case '3':Player.x++;Player.Y--;
                break;
                case '4':Player.x--;
                break;
                //case '5':
                //break;
                case '6':Player.x++;
                break;
                case '7':Player.x--;Player.Y++;
                break;
                case '8':Player.Y++;
                break;
                case '9':Player.x++;Player.Y++;
                break;
            }
    How can I define the struct in the function?
    Thanks.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You don't want to define the struct in the function. You have to define the struct before the function.

    Code:
    struct Player {
        int x;
        int y;
    };
    
    void movedir(struct Player,int i) {
        switch(i)
     .....
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    I wanted to define thje player.x before the "int main", it gives errors, that's the reason. I did it define before the function, I didn't explain my problem well.

    Now I have another error: expected primary-expression before '.' token. This, in all "case:"es:
    Code:
    switch(i)
        {
            case '1':
            Player.nx=Player.x--;
            Player.y--;
            break;
            case '2':
            Player.y--;
            break;
            case '3':
            Player.x++;
            Player.y--;
            break;
            case '4':
            Player.x--;
            break;
            //case '5':
            //break;
            case '6':
            Player.x++;
            break;
            case '7':
            Player.x--;
            Player.y++;
            break;
            case '8':
            Player.y++;
            break;
            case '9':
            Player.x++;
            Player.y++;
            break;
        }
    What can be the reason of this?
    Last edited by Karakus; 01-14-2006 at 04:38 AM.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You don't have an instance.
    Code:
    struct Player
    {
      int x;
      int y;
    };
    
    void movedir(Player &player, int i)
    {
      switch(i)
      {
      case '1':
        --player.x;
        break;
      }
    }
    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

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Think I have overlooked the real problem.
    Code:
    void movedir(struct Player,int i) {
    is not a valid definiton for a function. should be sth. like this

    Code:
    void movedir(struct Player player,int i) {
       switch(i)        {
                case '1':player.x--;player.y--;
       ....
    Kurt

    edit: seems to be one of theese days where I should not say anything. This is c++ so CornedBee's solution is the right one.
    Last edited by ZuK; 01-14-2006 at 04:53 AM.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    It works, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

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