Passing variable between cpp files

This is a discussion on Passing variable between cpp files within the C++ Programming forums, part of the General Programming Boards category; We know you're not as skilled as some of the members on the board, but passing and getting variables should ...

  1. #16
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    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.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    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.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    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.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    You are not passing an extra argument named "title" to the output function, are you?
    If not, then please show the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    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();
    }

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Quote Originally Posted by Todd88 View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    how else do I call that then?

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Do you fail to understand what you are doing here?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Yes Elysia, I do.

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    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.

  14. #29
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Alright I fixed it. Thanks a lot for your help Elysia and scwizzo. I really appreciate it!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing temporary variable by value
    By DL1 in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2009, 12:56 AM
  2. Help with multiple cpp files
    By Sclorch in forum C++ Programming
    Replies: 9
    Last Post: 02-11-2009, 09:58 AM
  3. Variable passing
    By seizmic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2005, 08:22 AM
  4. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM
  5. How to convert dll file to cpp files?
    By uday222 in forum C++ Programming
    Replies: 1
    Last Post: 06-18-2002, 12:32 AM

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