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; I have a variable in my "book.cpp" file named title. I want to pass this variable over to my "page.cpp" ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    122

    Passing variable between cpp files

    I have a variable in my "book.cpp" file named title. I want to pass this variable over to my "page.cpp" file so I can use it to output a page. How do I go about doing this? Here is a portion of my book.cpp file for getting the string:

    Code:
    string Book::getTitle(string title)
    {
        size_t title_start;
    
        title_start = title.find("Title:");
    
        if (title_start == title.npos)
           return "";
        title = title.substr(title_start+7);
        return title;
    }
    and here is the book.h file class:

    Code:
    class Book {
        ifstream file;
        string title;
    
        public:
        string getTitle(string title);
        Book(istream& in);
        bool open(istream& in);
    };
    and I am trying to get it over to my page.cpp file:

    Code:
    void Page::output(ofstream& out) const
    {
        string title;
        out.open("Page0001.html");
        out << "<title>" << title << "</title>" << endl;
        out.close();
    }
    with this page.h file class:

    Code:
    class Page {
        int lines;
        int pagenumber;
        ifstream file;
        ofstream out;
        vector<string> text;
    
        public:
        Page(istream& in);
        void setPageNum(int pageNum);
        void addLine(string line);
        void output(ofstream& out) const;
        int getLines() const;
        int getPageNum() const;
    };
    Any help is appreciated!

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Generally when thinking of a book, you think that it has pages and not vica-versa. If anything, your book object should handle output rather then your page. If you want your page to be independent of your book, then I would think the best way is to give the page its own title variable and set it at construction.

    It just seems unusual to say a page has a title rather then saying a book has a title. (hope this helped somewhat).

    I guess you could give the page a pointer to the book that spawned it so that you could make a call like page.BookParent->getTitle();

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Well I am building a project that outputs pages as html files. I search for the title given that it starts at "Title:" and ends after the line. Now I just need to get that variable and place it as the <title></title> tags of my html output. That's ALL I need it for!!

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I don't quite get the question, but if this helps any here's my two cents. If you want to get the title variable inside the page function where it outputs it as "<title>" title "</title>" then just pass a second argument to output(). Or make the variable title a private member of the class and when you call getTitle() it changes the private member. Maybe something like this...?

    Code:
    void Page::output(ofstream& out, string title) const
    {
        out.open("Page0001.html");
        out << "<title>" << title << "</title>" << endl;
        out.close();
    }
    
    //and use it something like this...
    
    Page p;
    Book b;
    p.output(outputstream, b.getTitle("title"));
    
    //or even just...
    p.output(outputstream,"title");
    Last edited by scwizzo; 12-01-2008 at 10:38 PM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    and where do I put the Book b and Page p variables?

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Where ever you need to declare them. There's not really a right or wrong place to put them.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Wherever I put them I am getting "b is not a type."

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    scwizzo was just putting them there to show you an example of how you could call the new modified functions, not something you should copy and use.
    Adapt it to your own code - the example, I mean, not 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.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    yeah it really doesn't help though because i am not that advanced in c++

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    What did scwizzo do? scwizzo simpy added "title" to the list of parameters for the function so you can actually pass along the title you wish to write.
    Now, incorporate it into your design and carry on as usual.
    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. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Alright I can pass it now but I do not want to pass it to the output function. I want to pass it to a function where I can take the passed variable and store it in a variable in the Page ADT. Can anyone help me with that?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Why is it so difficult to pass and store variables?
    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. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    because I'm not as skilled in C++ as you are!!

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Of course not, but this is a simple operation which you have done oh so many times now!
    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.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Okay I can pass it to a different function but how do I use this variable globally between all the functions?

Page 1 of 2 12 LastLast
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