Error Report...

Started by
20 comments, last by Lacutis 18 years, 8 months ago
Quote:Original post by zix99
Most of those errors are caused by redinitions. The Source in header of the main code already defines the variables, making them public to all functions. You don't need to redefine the variables where you use them. So take out MaxLeft, MaxRight, etc inside the main() function (Because they are public anyway).

Hope I could help.
~Zix~


The definitions in the main function aren't causing compilation errors, this is valid if somewhat useless code:

int main(){  int myint = 5;  {    int myint = 7;    std::cout << myint << std::endl; // prints 7  }  std::cout << myint << std::endl; // prints 5}


You can redeclare operators at different scope levels without error.
Hes defining them in the globals.h.

Orc:
Use the keyword extern before each of the variables in your global header, for example:

extern int MaxLeft;

That will fix the errors.
Advertisement
'extern' has been in my header ever since I posted the topic.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Quote:Original post by orcfan32
'extern' has been in my header ever since I posted the topic.


Quote:
C:/CPP/Console Tetris/Tetris.cpp:13: error: redefinition of `int MaxRight'
C:/CPP/Console Tetris/Globals.h:4: error: `int MaxRight' previously defined
here


Then you have:

extern int MaxLeft = something;

instead of:

extern int MaxLeft;

In your globals.h.

Its an error because you are defining it in multiple places. Not because you are declaring it in multiple places.
It runs fine now for me...of course it errors out when the block reaches the bottom, because you tell it to move past the bounds of the output array. Just change the "while (Looper != '1')" to "while (Looper != 1)" and you can keep developing.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Tetris.cpp:
#include <iostream>#include <windows.h>#include <string>#include "Globals.h"using namespace std;int Rnd(int min, int max) {  return (rand() / static_cast<double>(RAND_MAX + 1)) * (max - min + 1) + min;}string PlayingField[25];int Block = 0;int MaxLeft = 0;int MaxRight = 0;  int CurrBLine = 0;char BlockFrag = '0';void Refresh(){    system("CLS");    cout<<PlayingField[1]<<PlayingField[2]<<PlayingField[3]<<PlayingField[4];    cout<<PlayingField[5]<<PlayingField[6]<<PlayingField[7]<<PlayingField[8];    cout<<PlayingField[9]<<PlayingField[10]<<PlayingField[11]<<PlayingField[12];    cout<<PlayingField[13]<<PlayingField[14]<<PlayingField[15]<<PlayingField[16];    cout<<PlayingField[17]<<PlayingField[18]<<PlayingField[19]<<PlayingField[20];    cout<<PlayingField[21]<<PlayingField[22]<<PlayingField[23]<<PlayingField[24];    }void BlockDown(){         if ( Block == 1 )         {             PlayingField[CurrBLine - 1] = "                          |                |\n";             PlayingField[CurrBLine] = "                          |       00       |\n";             PlayingField[CurrBLine + 1] = "                          |       00       |\n";             CurrBLine = CurrBLine + 1;                       }         if ( Block == 2 )         {                       }         if ( Block == 3 )         {                       }         if ( Block == 4 )         {                       }         if ( Block == 5 )         {                       }         if ( Block == 6 )         {                       }         if ( Block == 7 )         {                       }    Refresh();}void MakeBlock(){     Block = Rnd(1, 7);         if ( Block == 1 )         {              PlayingField[2] = "                          |       00       |\n";              PlayingField[3] = "                          |       00       |\n";              PlayingField[4] = "                          |                |\n";              PlayingField[5] = "                          |                |\n";              MaxLeft = 7;              MaxRight = 7;              CurrBLine = 3;         }         if ( Block == 2 )         {              PlayingField[2] = "                          |       0        |\n";              PlayingField[3] = "                          |       0        |\n";              PlayingField[4] = "                          |       0        |\n";              PlayingField[5] = "                          |       0        |\n";              MaxLeft = 7;              MaxRight = 8;              CurrBLine = 5;         }         if ( Block == 3 )         {              PlayingField[2] = "                          |       0        |\n";              PlayingField[3] = "                          |       0        |\n";              PlayingField[4] = "                          |       00       |\n";              PlayingField[5] = "                          |                |\n";              MaxLeft = 7;              MaxRight = 7;              CurrBLine = 4;         }         if ( Block == 4 )         {              PlayingField[2] = "                          |        0       |\n";              PlayingField[3] = "                          |        0       |\n";              PlayingField[4] = "                          |       00       |\n";              PlayingField[5] = "                          |                |\n";              MaxLeft = 7;              MaxRight = 8;              CurrBLine = 4;         }         if ( Block == 5 )         {              PlayingField[2] = "                          |       0        |\n";              PlayingField[3] = "                          |       00       |\n";              PlayingField[4] = "                          |        0       |\n";              PlayingField[5] = "                          |                |\n";              MaxLeft = 7;              MaxRight = 7;              CurrBLine = 4;         }         if ( Block == 6 )         {              PlayingField[2] = "                          |        0       |\n";              PlayingField[3] = "                          |       00       |\n";              PlayingField[4] = "                          |       0        |\n";              PlayingField[5] = "                          |                |\n";              MaxLeft = 7;              MaxRight = 8;              CurrBLine = 4;         }         if ( Block == 7 )         {              PlayingField[2] = "                          |       0        |\n";              PlayingField[3] = "                          |      000       |\n";              PlayingField[4] = "                          |                |\n";              PlayingField[5] = "                          |                |\n";              MaxLeft = 6;              MaxRight = 7;              CurrBLine = 3;         }     Refresh();}int main(){    int Looper = 0;        PlayingField[1] = "                          |----------------|\n";    PlayingField[2] = "                          |                |\n";    PlayingField[3] = "                          |                |\n";    PlayingField[4] = "                          |                |\n";    PlayingField[5] = "                          |                |\n";    PlayingField[6] = "                          |                |\n";    PlayingField[7] = "                          |                |\n";    PlayingField[8] = "                          |                |\n";    PlayingField[9] = "                          |                |\n";    PlayingField[10] = "                          |                |\n";    PlayingField[11] = "                          |                |\n";    PlayingField[12] = "                          |                |\n";    PlayingField[13] = "                          |                |\n";    PlayingField[14] = "                          |                |\n";    PlayingField[15] = "                          |                |\n";    PlayingField[16] = "                          |                |\n";    PlayingField[17] = "                          |                |\n";    PlayingField[18] = "                          |                |\n";    PlayingField[19] = "                          |                |\n";    PlayingField[20] = "                          |                |\n";    PlayingField[21] = "                          |                |\n";    PlayingField[22] = "                          |                |\n";    PlayingField[23] = "                          |                |\n";    PlayingField[24] = "                          |----------------|\n";    // Draw the playing field:    cout<<PlayingField[1]<<PlayingField[2]<<PlayingField[3]<<PlayingField[4];    cout<<PlayingField[5]<<PlayingField[6]<<PlayingField[7]<<PlayingField[8];    cout<<PlayingField[9]<<PlayingField[10]<<PlayingField[11]<<PlayingField[12];    cout<<PlayingField[13]<<PlayingField[14]<<PlayingField[15]<<PlayingField[16];    cout<<PlayingField[17]<<PlayingField[18]<<PlayingField[19]<<PlayingField[20];    cout<<PlayingField[21]<<PlayingField[22]<<PlayingField[23]<<PlayingField[24];    // Start the main game loop:             MakeBlock();    while ( Looper != 1 )    {         BlockDown();         Sleep(500);    }    cin.get();system("CLS");}


and Globals.h:
extern string PlayingField[25];extern int Block;extern int MaxLeft;extern int MaxRight;extern int CurrBLine;extern char BlockFrag;


By compiling this I recieve the errors:
In file included from C:/CPP/Console Tetris/Tetris.cpp:4:C:/CPP/Console Tetris/Globals.h:1: error: syntax error before `[' tokenC:/CPP/Console Tetris/Globals.h:3: warning: `MaxLeft' initialized and declared    `extern'C:/CPP/Console Tetris/Globals.h:4: warning: `MaxRight' initialized and declared    `extern'C:/CPP/Console Tetris/Globals.h:5: warning: `CurrBLine' initialized and    declared `extern'C:/CPP/Console Tetris/Globals.h:6: warning: `BlockFrag' initialized and    declared `extern'C:/CPP/Console Tetris/Tetris.cpp: In function `int Rnd(int, int)':C:/CPP/Console Tetris/Tetris.cpp:8: warning: return to `int' from `double'C:/CPP/Console Tetris/Tetris.cpp:8: warning: argument to `int' from `double'C:/CPP/Console Tetris/Tetris.cpp: At global scope:C:/CPP/Console Tetris/Tetris.cpp:12: error: redefinition of `int MaxLeft'C:/CPP/Console Tetris/Globals.h:3: error: `int MaxLeft' previously defined hereC:/CPP/Console Tetris/Tetris.cpp:13: error: redefinition of `int MaxRight'C:/CPP/Console Tetris/Globals.h:4: error: `int MaxRight' previously defined    hereC:/CPP/Console Tetris/Tetris.cpp:14: error: redefinition of `int CurrBLine'C:/CPP/Console Tetris/Globals.h:5: error: `int CurrBLine' previously defined    hereC:/CPP/Console Tetris/Tetris.cpp:15: error: redefinition of `char BlockFrag'C:/CPP/Console Tetris/Globals.h:6: error: `char BlockFrag' previously defined    here


It sure doesn't compile for me, TDragon.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Quote:
C:/CPP/Console Tetris/Tetris.cpp:12: error: redefinition of `int MaxLeft'
C:/CPP/Console Tetris/Globals.h:3: error: `int MaxLeft' previously defined here
C:/CPP/Console Tetris/Tetris.cpp:13: error: redefinition of `int MaxRight'
C:/CPP/Console Tetris/Globals.h:4: error: `int MaxRight' previously defined
here
C:/CPP/Console Tetris/Tetris.cpp:14: error: redefinition of `int CurrBLine'
C:/CPP/Console Tetris/Globals.h:5: error: `int CurrBLine' previously defined
here
C:/CPP/Console Tetris/Tetris.cpp:15: error: redefinition of `char BlockFrag'
C:/CPP/Console Tetris/Globals.h:6: error: `char BlockFrag' previously defined
here


It still says you are defining it in Globals.h
Try making sure everything is saved and doing a rebuild all.

Also, make sure the Globals.h that you are editing is in the C:\CPP\Console Tetris\ directory, if you did a save as and put it in a Include\ directory under that or something the compiler is still finding the old one.
Heh, guess I should've mentioned that I left globals.h out entirely. It's pointless in a single-file program.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
Ohhhhhhhh...rebuild all. Forgot about that. That cleaned most errors out, just leaves me with a syntax error with the line "extern string PlayingField[25];". (Syntax error before '[' token.)
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
That line comes before the use of "using namespace std;". Since it does, you either need to move the namespace usage up, or use std::string instead of string.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
I would like to suggest that this project is too advanced for you at the moment, because your code indicates that you are clearly not comfortable with the 0-based indexing of arrays, using for loops or switch statements to avoid redundancy, etc.

As for your syntax error, it smells to me like it can't find a definition of "string" at that point. It would be a good idea to #include string from the header as well as the .cpp (it provides the necessary include guards - while you're at it, put in your own).

Edit: TDragon is correct - although I will maintain that the header should include the things that it needs (here, 'string') - because that will avoid the need for you to include things "in the right order" in the .cpp. Also, you should definitely be writing "std::string" in the header rather than putting in a "using namespace" declaration - putting that in the header would automatically use the namespace for every file that includes that header, which will seriously annoy you later on (trust me).

This topic is closed to new replies.

Advertisement