Using class variable in a different class

I've been stuck in this for hours now. I'm trying to use the string 'from_file' in the function add() (within another class), but I'm not sure how to go about it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  string Simple_window::open()
{
   button_pushed = true;
    
   string from_file = file_in.get_string();
   string from_web = web_in.get_string();
   string from_tags = tag_in.get_string();

if (from_web != "")
    Wget(from_web);

if(from_file != "")
    {
    Try_again(from_file);
    Image mypic(Point(50,50), from_file);
	Picture_window pwin(Point(100,100), 600, 400, "Picture Window");
	pwin.attach(mypic);
	Fl::redraw();
	pwin.wait_for_button();
    }
}


This is the other class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

void Picture_window::add()
{

    button_pushed = true;

    string tags = add_tags.get_string();

    if(tags.find("family") != string::npos)
    {
        ofstream family_stream;
        family_stream.open("family.txt");

//I'm trying to use the variable 'from_file' here.
        family_stream <<from_file;
        family_stream.close();
    }


}
Right now your from_file string is a local variable of a function, so unless you return it from the function or change it to a member of your Simple_window class you won't have access to it outside of the open() function.

Without understanding the relationship between the two classes, it is hard to advise on how best to achieve what you're trying to do (e.g. are the classes part of an inheritance tree, or a composition, or completely separate).
My Simple_Window class is being used to display some input boxes, one of which provides the option to enter a file name. By clicking open, a separate window (a Picture_window) displays the image given by the user. What I hope to accomplish with the second piece of code is to place the name of the picture file (the input in the Simple_window) into text files corresponding to the picture's tags (which the user assigns through a Add Tags box in the Picture_Window. If you need some code and/or pictures to help visualize it...
Last edited on
OK, well since you are creating a Picture_window class in your call to open, you could add a function like SetFile() to Picture_window which would set a datamember in the class. It's not extensible, but it will work for that case.

1
2
3
4
5
6
7
8
9
10
if(from_file != "")
{
    Try_again(from_file);
    Image mypic(Point(50,50), from_file);
    Picture_window pwin(Point(100,100), 600, 400, "Picture Window");
    pwin.SetFile(from_file);  // Set the file name here
    pwin.attach(mypic);
    Fl::redraw();
    pwin.wait_for_button();
}

Topic archived. No new replies allowed.