473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Scoping Problems

2
Hello everyone,

I'm a novice programmer and am having trouble with a text-adventure game I am programming. There are two classes, Hero and Monster, that contain functions that I want to be able to pass pointers to objects of the opposite class into, but the compiler complains that it cannot recognize the identifiers. I have included both "Hero.h" and "Monster.h" in each cpp file, and I get different compiler errors depending on the order I include the files, so I'm assuming it's a scoping issue. I can't figure out how to resolve it though. If anyone could give me some suggestions I would appreciate it. I've included some of the code to help clarify.

class Hero
{
public:
Hero();
void nameHero(string n);
void attack(Monster *monster);
...
}


class Monster
{
public:
Monster();
void nameMonster(string n);
void attack(Hero *hero);
...
}


I keep getting errors in the declaration of the 'attack' fuction for each class.
Nov 16 '06 #1
4 1263
sivadhas2006
142 100+
Hi,

do the forward declarations for the class.
It may solve your problem.

Expand|Select|Wrap|Line Numbers
  1.    class Monster;
  2.    class Hero
  3.    {
  4.       public:
  5.          Hero();
  6.          void nameHero(string n);
  7.          void attack(Monster *monster);
  8.       ...
  9.    }
  10.  
Regards,
M.Sivadhas.
Nov 16 '06 #2
Another cleaner (but bit expensive) solution is use of virtual functions. Define attack() virtual in base class and inherit in Hero and Monster classes. Define functionality in these classes n in the calling function(assuming main()), point to appropriate object.
Nov 16 '06 #3
bob357
2
Thanks for the advice guys. I decided to take the easy way out with the forward declarations and it compiled right away.
Nov 16 '06 #4
sivadhas2006
142 100+
Thanks for the advice guys. I decided to take the easy way out with the forward declarations and it compiled right away.
ok.

Regards,
M.Sivadhas.
Nov 17 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: David Stockwell | last post by:
Hi, Another of my crazy questions. I'm just in the process of learning so bear with me if you can. I actually ran it.... with two test cases TEST CASE 1: Say I have the following defined:...
4
by: Frederick Grim | last post by:
okay so the code in question looks like: namespace util { template<typename C> inline void mmapw(C *& ptr, size_t length, int prot, int flags, int fd, off_t offset) { if((ptr =...
3
by: John Yopp | last post by:
I seem to be have problems with what appears to be variable scoping. If I define a constant in my main HTML page, I do not seem to be able to reference it within functions in JavaScript include...
2
by: Diane Selby | last post by:
Hi- I am confused by C#'s scoping of variables within a child scope (for loop, conditional, block of code), and was hoping someone could explain. Here's the offending code: for (int i=0;...
4
by: Joel Gordon | last post by:
Hi, When I try and compile the a class containing the following method : public void doSomething() { for (int i=0; i<5; i++) { IList list = new ArrayList(); Console.WriteLine( i /...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
17
by: Chad | last post by:
The following question stems from Static vs Dynamic scoping article in wikipedia. http://en.wikipedia.org/wiki/Scope_(programming)#Static_versus_dynamic_scoping Using this sites example, if I...
3
by: SPECTACULAR | last post by:
Hi all. I have a question here.. what kind of scoping does C++ use? and what kind does Smalltalk use? I know smalltalk is a little bit old .. but any help would be appreciated.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.