"Toke H?iland-J?rgensen" <toke@toke.dk> wrote in message
news:5974f7d5.0312310648.73c0c0f5@posting.google.c om...[color=blue]
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message[/color]
news:<EPqIb.11490$I07.23677@attbi_s53>...[color=blue][color=green]
> > Most certainly, yes. However, don't attach it, copy-and-paste it.[/color][/color]
Also,[color=blue][color=green]
> > remove all non-essential parts of the code and make sure that after that
> > it still compiles with exactly the same error message as you claim it[/color][/color]
does.[color=blue]
>
> Okay...the program is an attempt at making a text adventure as an
> excercise...
>
> //adventure.cpp
> #include <iostream.h>
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> #include <fstream.h>
> #include <windows.h>
>
> const int WEAPON = 100;
> const int ARMOR = 101;
> const int CHARM = 102;
> const int TEXTMODE = 200;
> const int BINARYMODE = 201;
> const int NUMRANDOMS = 10000;
> const int EAST = 1;
> const int WEST = 2;
> const int NORTH = 3;
> const int SOUTH = 4;
>
> #include "random.h"
> #include "DataHandler.h"
> #include "TreasureChest.h"
>
> using namespace adventure;
>
> RandomHandler randomBase;
>
> TreasureChest treasureChest;
>
> int main()
> {
> return 0;[/color]
This looks like kind of a short adventure. :)
[color=blue]
> }
>
> //random.h
>
> #ifndef __random_h__
>
> #define __random_h__
>
> namespace adventure
> {
> const int NUMRANDOMS = 10000;
>
> //
> // RandomHandler class
> // used to store a bunch of randoms...used as a workaround
> //
>
> class RandomHandler
> {
> int randoms[NUMRANDOMS];
> int randomCounter;
>
> public:
> RandomHandler();
> int GetRandom();
> };
>
> }
> #endif
>
>
> //random.cpp
> #include "random.h"
> #include <stdlib.h>
> #include <time.h>
>
>
> namespace adventure
> {
>
> RandomHandler::RandomHandler()
> {
> int i;
> srand( (unsigned)time( NULL ) );
>
> for( i = 0; i < NUMRANDOMS; i++ )
> randoms[i] = rand();
>
> randomCounter = 0;
> }
>
> int RandomHandler::GetRandom()
> {
> randomCounter++;
> if(randomCounter >= NUMRANDOMS)
> randomCounter = 0;
>
> return randoms[randomCounter];
> }
> }
>
> //DataHandler.h / DataHandler.cpp defines the class DataHandler
> //They work fine, however, so i excluded them...
>
> //TreasureChest.h
> #ifndef __TreasureChest_h__
> #define __TreasureChest_h__
>
> #include "DataHandler.h"
>
> namespace adventure
> {
> class DataHandler;
> class RandomHandler;
>
> //
> // Treasure struct.
> // Contains information about treasure.
> //
> struct Treasure {
> char name [50];
> int isWearable;
> int gold, hBonus, sBonus, aBonus;
> char type;
> int attackModifier, defenceModifier;
> };
>
> //
> // TreasureChest class
> // Manages treasures.
> //
> class TreasureChest
> {
> Treasure * treasures;
> Treasure emptyTreasure;
> int numTreasures;
> void AddTreasure(Treasure);
> void LoadTreasures();
>
> public:
> TreasureChest();
> Treasure GetTreasure(int);
> };
> }
>
> #endif
>
> //TreasureChest.cpp
> #include "TreasureChest.h"
> #include "DataHandler.h"
> #include <string.h>
> #include <stdlib.h>
>
>
> namespace adventure
> {
>
> const int TEXTMODE = 200;
> const int BINARYMODE = 201;
>
> TreasureChest::TreasureChest()
> {
> numTreasures = 0;
> treasures = new Treasure[1];
> strcpy(emptyTreasure.name, "No treasure");
> LoadTreasures();
> }
>
> void TreasureChest::AddTreasure(Treasure add)
> {
>
> treasures = (Treasure *) realloc(treasures, (numTreasures+1) *
> sizeof(Treasure));
> treasures[numTreasures] = add;
> numTreasures++;
> }
>
> void TreasureChest::LoadTreasures()
> {
> char input[100] = "";
> Treasure thisTreasure;
> DataHandler treasureGet ("treasure.txt",TEXTMODE);
>
> while(treasureGet.GetLine(input) != false) {
> if(input[0] != '#') {
> strcpy(thisTreasure.name,strtok(input, ";"));
> thisTreasure.isWearable = atoi(strtok(NULL, ";"));
> thisTreasure.type = atoi(strtok(NULL, ";"));
> thisTreasure.gold = atoi(strtok(NULL, ";"));
> thisTreasure.hBonus = atoi(strtok(NULL, ";"));
> thisTreasure.sBonus = atoi(strtok(NULL, ";"));
> thisTreasure.aBonus = atoi(strtok(NULL, ";"));
> thisTreasure.attackModifier = atoi(strtok(NULL, ";"));
> thisTreasure.defenceModifier = atoi(strtok(NULL, ";"));
>
> AddTreasure(thisTreasure);
> }
> }
>
> }
>
> Treasure TreasureChest::GetTreasure(int type)
> {
> int t;
> double r;
>
> do {
> r = ( (double) randomBase.GetRandom() / (double)(RAND_MAX+1) );
> //class instantiated in adventure.cpp - problem
> t = (int) (r * numTreasures);
> } while(treasures[t].type != type);
> return treasures[t];
> }
>
> }
>
>
> The compiler errors:
> --------------------Configuration: adventure - Win32
> Debug--------------------
> Compiling...
> TreasureChest.cpp
> d:\dokumenter\coding\adventure\treasurechest.cpp(6 0) : error C2065:
> 'randomBase' : undeclared identifier
> d:\dokumenter\coding\adventure\treasurechest.cpp(6 0) : error C2228:
> left of '.GetRandom' must have class/struct/union type
> adventure.cpp
> DataHandler.cpp
> Error executing cl.exe.
>
> adventure.exe - 2 error(s), 0 warning(s)
>
>
>
> I put in comments where the errors are. Hope this helps... :)
>
> -Toke[/color]
OK, randomBase is a global variable defined in adventure.cpp. Then you try
to use it in TreasureChest.cpp. Since that symbol isn't defined in that
file, the compiler says, "undeclared identifier". That makes sense. The
other error is spurious, trying to keep going without knowing what
randomBase means.
So how to fix it? You could declare randomBase external, but that would be a
poor idea. I suggest you avoid global variables as much as possible. In this
case I would suggest randomBase be declared in your main function and passed
as an argument as needed; e.g.
Treasure TreasureChest::GetTreasure(int type, RandomHandler &randomBase)
--
Cy
http://home.rochester.rr.com/cyhome/