LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-04-2012, 08:57 PM   #1
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Rep: Reputation: Disabled
Using ADTs


Hello all,

I am trying to create a program for a game called Gravilex. At the moment I am focusing on my game.cpp, grid.h, and game.h files. I am stuck on getting the grid to be printed out in 7 rows and 7 columns. The error in particular that has got me scratching my head is "game.cpp:18: error: declaration of 'char grid [7][7]' shadows a parameter"

Here are the files

Code:
#ifndef GRID_H
#define GRID_H

class Grid {

public:

  //A non-alphabetic character
  static const char FILL_CHARACTER;

  //Initialize the grid for a game using
  //set up from indicated file
  void printGrid (char grid);


  //After a player's move, computer or user, update the grid
  //reflecting their choices
  void updateGrid ();

  //If the grid is completely filled annouce game over followed
  //by whoever one
  void fullGrid ();

};


#endif
Code:
#ifndef GAME_H
#define GAME_H

class Game {

  //

  // Initialize the data for a game
  // using words & letters from indicated files
  Game (const char* letterListFilename, const char* wordListFilename);

  //Print the Total scores for both players
  void printTotal ();

};

#endif
Code:
#include <iostream>
#include <string>
#include "game.h"
#include "grid.h"

using namespace std;

class Grid;

//A non-alphabetic character
const char Grid::FILL_CHARACTER = '.';

//Set up a new Game
printGrid (char grid)
{
    const int rows = 7;
    const int columns = 7;
    char grid [rows][columns] =
    {
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'1', '2', '3', '4', '5','6','7'}
    }
    for (int i=0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                cout << left << grid [i][j];
            }
            cout << endl;
        }
    cout << User::hLetters();
}
Errors:
game.cpp:14: error: ISO C++ forbids declaration of 'printGrid' with no type
game.cpp: In function 'int printGrid(char)':
game.cpp:18: error: declaration of 'char grid [7][7]' shadows a parameter
game.cpp:20: error: expected '}' before ';' token
Project\game.cpp:21: error: expected ';' before '}' token
game.cpp:22: error: expected ';' before '}' token
game.cpp:23: error: expected ';' before '}' token
game.cpp:24: error: expected ';' before '}' token
game.cpp:25: error: expected ';' before '}' token
game.cpp:26: error: expected ';' before '}' token
game.cpp: At global scope:
game.cpp:28: error: expected unqualified-id before 'for'
game.cpp:28: error: expected constructor, destructor, or type conversion before '<' token
game.cpp:28: error: expected constructor, destructor, or type conversion before '++' token
Process terminated with status 1 (0 minutes, 0 seconds)
12 errors, 0 warnings


Any help would be appreciated.
 
Old 12-05-2012, 11:26 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,514

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
The error in particular that has got me scratching my head is "game.cpp:18: error: declaration of 'char grid [7][7]' shadows a parameter"
To "shadow" a parameter (or variable) means to have a variable of the same name as one declared earlier, so that the name now only refers to the later one, ie the later "shadows" (as in covers) the earlier declaration. In this case you have:
Code:
//Set up a new Game
printGrid (char grid)
{
    const int rows = 7;
    const int columns = 7;
    char grid [rows][columns] =
 
Old 12-05-2012, 11:51 AM   #3
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Lol wow well that would make sense. I changed the second declaration of grid to char g and it compiled fine. Another question is that when I get errors telling me:
"game.cpp:26: error: expected ';' before '}' token"
"game.cpp: In function 'char printGrid(char (*)[7], int, int)':
game.cpp:20: error: expected '}' before ';' token
game.cpp:21: error: expected ';' before '}' token
game.cpp:22: error: expected ';' before '}' token
game.cpp:23: error: expected ';' before '}' token
game.cpp:24: error: expected ';' before '}' token
game.cpp:25: error: expected ';' before '}' token
game.cpp:26: error: expected ';' before '}' token
game.cpp:36: error: 'User' has not been declared
Process terminated with status 1 (0 minutes, 0 seconds)
8 errors, 0 warnings
"

I have placed a ; at the end of every row? Also as far as the User declaration goes, I am trying to call the list of letters I created in my user.h file which I will show below.
Code:
//Set up a new Game
char printGrid (char grid [][7], int, int)
{
    const int rows = 7;
    const int columns = 7;
    char g [rows][columns] =
    {
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'.', '.', '.', '.', '.','.','.'};
        {'1', '2', '3', '4', '5','6','7'}

    for (int i=0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                cout << left << grid [i][j];
            }
            cout << endl;
        }
    cout << User::hLetters();
}
Code:
#ifndef USER_H
#define USER_H

class Grid;

class User {
public:
  //Initialize the grid for a game using
  //set up from indicated file
  void printGrid ();

  //Print the letters remaining to the user
  static const char *hLetters ()
{
    return "aabcdeefghiijklmnoopqrstuuvwxyz";
}
  //Gather user's moves
  void hPlay ();

  //Update the grid, reflecting the player's choice
  void updateGrid ();

  //Determine word(s) user wishes to claim
  void wordCheck (const char* wordListFileName);

  //Following a claim, determine point value, if any, and print
  //the turn score
  void awardPoints (int wordLength);


};


#endif
Thanks in advance
 
Old 12-05-2012, 05:10 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,514

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
Originally Posted by R2B Boondocks View Post
I have placed a ; at the end of every row?
Yeah, that's the problem. Items in an array initialization are separated by , not ;. You need to end the statement with a ;.

Code:
    char g [rows][columns] =
    {
        { '.', '.', ... '.' },
        { '.', '.', ... '.' },
        ...
        { '1', '2', ... '7' }
    };
Quote:
Also as far as the User declaration goes, I am trying to call the list of letters I created in my user.h file which I will show below.
It looks like you forgot to #include user.h in game.cpp.

Was printGrid supposed to be a method of Grid or User? If so, it should be referred to as Grid::printGrid or User::printGrid respectively in the definition.
 
Old 12-06-2012, 10:47 AM   #5
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Thank you so much ntubski!! I managed to get rid of all the errors and yea I did forget to include my user.h file for some reason. I also had to declare my int's i and j sooner. The final cpp file looks as follows so far.
Code:
#include <iostream>
#include <string>
#include "game.h"
#include "grid.h"
#include "user.h"


using namespace std;


//A non-alphabetic character
const char Grid::FILL_CHARACTER = '.';

//Set up a new Game
char Grid::printGrid (char grid [][7], int, int)
{
    int j;
    int i;
    const int rows = 7;
    const int columns = 7;
    char g [rows][columns] =
    {
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'.', '.', '.', '.', '.','.','.'},
        {'1', '2', '3', '4', '5','6','7'},
    };
    for (i=0; i < rows; i++);
        {
            for (j = 0; j < columns; j++)
            {
                cout << grid [i][j] << endl;
            }
        }
    cout << User::hLetters();
}
Regards
 
Old 12-06-2012, 11:16 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,514

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
Originally Posted by R2B Boondocks View Post
I also had to declare my int's i and j sooner.
Actually, you didn't: there is another mistake which made it seem that way:
Code:
    for (int i=0; i < rows; i++);
        {
            for (int j = 0; j < columns; j++)
            {
                cout << grid [i][j] << endl;
            }
        }
By putting the semicolon there you are writing a for loop with an empty body. In other words, what you wrote means this:
Code:
    for (int i=0; i < rows; i++)
    {
    }

    {
        for (int j = 0; j < columns; j++)
        {
            cout << grid [i][j] << endl;
        }
    }
Which is why the compiler thinks that i is out of scope.
 
Old 12-06-2012, 01:57 PM   #7
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Ah, amazing how one simple ";" can throw things off like that. I changed it again and declared them where they were prior. Everything compiles still and only has two warnings (which I don't think are major because I am not finished with the program [ie no main driver]).

Thank you for teaching me and helping me work through this section of my program.
 
  


Reply

Tags
c++


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



All times are GMT -5. The time now is 05:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration