-
We know you're not as skilled as some of the members on the board, but passing and getting variables should be a well developed skill by the time you build classes and ADT's. Anywho, you can make the same variable inside each class as a private member, and have functions that return the value when ever you need it. That would, in a sense, be making the variable (more over the value) global.
-
To that, there are several solutions:
1) Pass them as arguments to every function you call.
2) Make it global or a class member.
Globals should really, really be avoided unless you need them, however.
The rule is: use as local scope as you can. Consider passing them as arguments instead.
But to your original question, you don't really need more than local scope. All you need is to pass it as an argument!
-
Okay I pass the title using:
Code:
Page p;
p.passTitle(title);
from the Book ADT to the Page ADT. Now passTitle function looks like this:
Code:
string Page::passTitle(string theTitle)
{
title = theTitle;
return title;
}
I can't simply have "title" as a parameter for the output functions because that doesn't work.
-
You can, but that's up to your implementation.
The function should probably be called something along the lines of SetTitle, as well.
Aside from that, if title is a member variable, then it works like you want it to. I would, however, prefix any member variable with "m_" to identify it as a member variable.
-
title is a private class member. but it's not working the way it's suppose to....because i use the title variable in my output function and it's not outputting it.
-
You are not passing an extra argument named "title" to the output function, are you?
If not, then please show the code.
-
I don't think I am...but here is my code:
This is calling the output function:
Code:
output(out, title);
and this is the output function:
Code:
void Page::output(ofstream& out, string title) const
{
out.open("Page0001.html");
out << "<title>" << title << "</title>";
for(int i = 0; i<text.size(); ++i)
{
out << "<pre>" << text.at(i) << "</pre>";
}
out.close();
}
-
Quote:
Originally Posted by
Todd88
Code:
void Page::output(ofstream& out, string title) const
{
out.open("Page0001.html");
out << "<title>" << title << "</title>";
for(int i = 0; i<text.size(); ++i)
{
out << "<pre>" << text.at(i) << "</pre>";
}
out.close();
}
But you are.
The argument hides the member variable.
-
how else do I call that then?
-
Do you fail to understand what you are doing here?
-
-
Then you need to take a step back and look at your current implementation.
Your function takes two parameters.
You also have a member variable.
They even share the same name!
-
Okay I have a constructor named Page which reads from the input file and takes each line of the input file and places it into a vector. Now, I get the title variable from the Book ADT which I pass to the Page ADT. What my problem is, is that I need to fill the vector before passing the title variable to output or else nothing will be printed out but the title. The title passes fine if I use
Code:
output(out, title);
anywhere but the constructor because like I said, title is not global. I do not know how to do this and this is why I am asking the question.
-
Alright I fixed it. Thanks a lot for your help Elysia and scwizzo. I really appreciate it!