Jump to content
  • Advertisement
Sign in to follow this  
luinil

passing object to function of int main()

This topic is 4444 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

ok what i am TRYING to do is pass(by reference) a object called baby(note lowercase) to a function of main, called void test(Baby &t). void test(Baby &t) then TRIES to calles a member method of Baby class. This method returnName works ok if called in main().I get an error message something like baby is out of scope..........aaaaaaaaaaaaaaarrrrrrrrrrrrrgggggggg . Not sure whats wrong. Is it ok to pass an object by reference to a func of main and then that function calls a method of the class of the object u passed it? is this something to do with virtual key word and passing objects? Do i need to have virtual keyword before each member method in the class declaration section(function of class)? would this solve the problem? just wondering if i am close or just way off the mark. This is my first posting so im not sure if im giving too little or not enough information . thanks:) #include "baby.h" #include <iostream> #include<string> using namespace std; void test(Baby &t); //func prototype int main() { Baby baby("silvia",5); // creates instance(object) of Baby class called baby. //works ok test(baby); //functional call to int main function void test(Baby &t) return 0; } void test(Baby &t) { string name =""; name = baby.returnName(); //returnName method returns data member of // Baby class - works ok in main but not in func of //main. cout << name << endl; }

Share this post


Link to post
Share on other sites
Advertisement
name = baby.returnName(); //returnName method returns data member of
// Baby class - works ok in main but not in func of
//main.

That's because baby only exists in main, it is not global.

What you want is: name = t.returnName(); since t is the name of the referenced object passed to test.

Share this post


Link to post
Share on other sites
You may want to post code within [ source ][ /source ] tags (but without the spaces.)

In any case, when you say "a function of main", it looks like you mean a function call from within the main() function. main() is a function itself, so it can't "have" a function.

You may want to supply more information with your question. When you say "it worked okay in main" do you mean the line cout << name << endl;? Also, what do you mean when you say it no longer works? What doesn't work about it? Does it crash? Is the output garbled?

Those are just tips for posting, but I think the real answer is that you're passing a Baby& variable named t, but you're using a variable named baby

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!