Archived

This topic is now archived and is closed to further replies.

BSXrider

Basic class question

Recommended Posts

Class A has a public member, an object of class B. I think this is called "composition". I want to call a member function of the B object which is a part of object A. objA.objB.function(); Doesn''t work. How should I be doing this?

Share this post


Link to post
Share on other sites
Is the function in Class B public?

This has to be defined like this:

class B
{
public:
void function();
}

class A
{
public:
class B b;
}

This should work:

class A a;
a.b.function();

(I am so used to using typedefs, that this "class A a" stuff looks weird to me )

-Chris

Share this post


Link to post
Share on other sites
Yep, I''ve got the function public and object b is a public member of class a.

A a;
a.b.function(); //line 23

gives me:

G:\c++\My projects\PonGLe\PonGLe.cpp(23) : error C2143: syntax error : missing '';'' before ''.''

Share this post


Link to post
Share on other sites
How about?

#include <iostream.h>

class B
{
public:
void function();
};

class A
{
public:
B member;
};

int main()
{
A ClassA;
ClassA.member.Function();

return 1;
}


-= Off World Technologies, Inc. =-

Edited by - faet83 on February 7, 2002 8:33:40 PM

Share this post


Link to post
Share on other sites
Unfortunately, without seeing the class defs, I have no idea what could be causing this. Try putting the word "class" before the variable declaration: "class A a;" Some of the standards junkies around here could tell you what is the generic standard for these situations (I use typedef most of the time, so I don''t generally worry about it).

-Chris

quote:
Original post by BSXrider
Yep, I''ve got the function public and object b is a public member of class a.

A a;
a.b.function(); //line 23

gives me:

G:\c++\My projects\PonGLe\PonGLe.cpp(23) : error C2143: syntax error : missing '';'' before ''.''


Share this post


Link to post
Share on other sites
if your getting that error then check to see tat all ur stuff is ended right.
check for semicolons in the class scope (did u forget to end ur class in a ; ?), check to see if you have semicolons in the wrong places, like, void func() {}; .

you probably have a semicolon missing somewhere, be it inside your class our in your procedure.

Share this post


Link to post
Share on other sites
Just a little hint...

I don''t know what is causing your bug, but anyway, following regular OOP rules, it is best to always hide everything you can in private, unless you need to access it from derived classes (i.e. protected interfaces) or make it available to the namespace level (public interface). It is best to always avoid declaring a member variable (int, float or an object of any type) in the public section of your class definition. Make it private, and write a little inline function that will return a const reference to it. Or maybe a non-const reference if you REALLY need to.

But anyway, I don''t have a direct explanation to your problem, this is just a little hint of how OOP C++ code is usually written.


Etienne Begin,
Computer Science student

Share this post


Link to post
Share on other sites
    
class Bat
{
private:
float xcoord;
public:
Bat &setx (const float value) { xcoord = value; return *this; }

};



class Player
{
public:
int score;
Bat batty;

Player()
{
score=0;
}

};



int main()
{
Player player1;

player1.batty.setx(5);

return 0;
}



Edited by - bsxrider on February 8, 2002 4:59:08 AM

Share this post


Link to post
Share on other sites
Whoops, sorted it.

Because I was just testing to see if thing.thing.anotherthing was possible like this I had stuck it in with all my global variables to see if it would work. naturally such a call has to be made from within a function. Doh.

- seb

Share this post


Link to post
Share on other sites