passing object to function of int main()
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;
}
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.
// 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.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement