Jump to content
  • Advertisement

Archived

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

wank8734

C++ Some real quick help using classes and objects...

This topic is 5240 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Real quick I need to know how to access a function of an object in another class, from within one class. For example. I need to call print of the object printer (usually printer.print() but I need to do it within a function of computer. doing printer.print(); comes back with undeclared Identifier. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
You will either need to pass a pointer to the printer to your computer object, or make printer a member of computer. The second one makes more sense to me (a computer HAS a printer):

class Printer
{
//printer stuff
void print();
};

class Computer
{
//computer stuff
public:
void doSomething()
{
printer->print();
}
private:
Printer* printer;
};

Share this post


Link to post
Share on other sites
let me give a little more info. the actual program is that of a schedule for a student that has courses and instructors. I am creating the objects of courses and instructor in the main function, and when I run the toString function of courses, inside it is supposed to call toString function of instructor. so for example:

instructor instructtest(name); //creates instructor object
course coursetest(coursename, room); //creates course object
coursetest.toString(); //call toString function of coursetest

then for course::toString(); I have the following:

cout << name << " " << room;
instructtest.toString();

and it says instructtest is an undeclared identifier, which I assume is because I created the object in the main function. So how do I make it work?

Share this post


Link to post
Share on other sites
Like I said, either pass in a pointer to the object or make it a member of the the object that you want to use it in. The two objects live in a different scope right now. They have no knowledge of each other which is what the error is about.

edit: see my example above, it still holds for this. Logically a course HAS an instructor, so make the instructor and member of course.

[edited by - CodeMunkie on March 17, 2004 4:47:27 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!