Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ C++ classes, methods

C++ classes, methods

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Zeno   USA  (2,871 posts)  Bio
Date Fri 29 Sep 2006 01:24 AM (UTC)
Message
Okay, I have two classes. One is a list of warriors and another is an arena (3D array of rooms [bool]).

In the list class, I have a method that "battles" and warriors can be killed in this method. When a warrior is killed, the room they're in (in the arena) should be made true (bool). But apparently you're not allowed access to other classes in C++. I get the error:

'ArenaClass::CloseRoom' : illegal call of non-static member function


What can I do here to accomplish this? I cannot use inheritance yet on this assg.


Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Nick Gammon   Australia  (23,166 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 29 Sep 2006 01:46 AM (UTC)
Message
You need to call an instance of the class, not the class itself.

Instead of:

ArenaClass::CloseRoom ()

You need something like this:

ArenaClass myarena;

myarena.CloseRoom ();

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #2 on Fri 29 Sep 2006 01:50 AM (UTC)

Amended on Fri 29 Sep 2006 01:53 AM (UTC) by Zeno

Message
Well I did that in the client, but not implementation (with the methods).

So I should just pass that into the Battle method? I think I know what to do now.

Hmm doesn't seem to work. This is the method header:
void WarListClass::Battle( ArenaClass arena)


Getting error:
syntax error : identifier 'ArenaClass'

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Fri 29 Sep 2006 02:09 AM (UTC)
Message
Classes like all types need to have been declared (but not necessarily defined) before you can use them. Usually it's enough to include the header files. However you can get into nasty circular dependencies if each class needs to refer to the other in the headers; in that case you should use forward declarations.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #4 on Fri 29 Sep 2006 02:10 AM (UTC)
Message
Not sure I follow. Both these classes are in the same .h file and the methods in the same .cpp file.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #5 on Fri 29 Sep 2006 07:59 AM (UTC)
Message
Well, the compiler has to have seen the class 'Foo' before you use it in a method declaration. For example:


[.h file]
class Bar
{
  void doSomething(Foo f);
}

class Foo
{
  void bla();
}

This is invalid code because 'Foo' has not seen 'Bar' yet. Now consider this:


[.h file]
class Bar
{
  void doSomething(Foo f);
}

class Foo
{
  void doSomethingElse(Bar b);
}

In this case Foo will be happy but we still have the problem of Bar not seeing Foo and therefore the method signature is invalid. Here is the solution:


[.h file]

// Forward declarations:
class Bar;
class Foo;

// Full declarations:

class Bar
{
  void doSomething(Foo f);
}

class Foo
{
  void doSomethingElse(Bar b);
}


Here, we forward-declare the two classes, so that everything is happy -- when the compiler goes through the real declarations, it has already been told that there is such a class as 'Foo' and 'Bar', so everything will work.

Does this make more sense? Inter-dependencies of classes can get really tricky, especially when you have circular dependencies.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #6 on Fri 29 Sep 2006 01:45 PM (UTC)
Message
Yeah I figured that out last night and went to bed. Thanks though. :P

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,346 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.