Basic class question
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?
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
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
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 ''.''
A a;
a.b.function(); //line 23
gives me:
G:\c++\My projects\PonGLe\PonGLe.cpp(23) : error C2143: syntax error : missing '';'' before ''.''
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
#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
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
-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 ''.''
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.
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.
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
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
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
If what you just posted doesn''t compile for you, I''m not sure what the problem is since i just copy pasted it into MSVC++ and it compiled with no errors..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement