ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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] =
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
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.
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();
}
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.