Passing variable from one class to another

This is a discussion on Passing variable from one class to another within the C++ Programming forums, part of the General Programming Boards category; Hey, I have been trying to pass a class variable to another class but I have no clue how to ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    8

    Passing variable from one class to another

    Hey,
    I have been trying to pass a class variable to another class but I have no clue how to do this...
    Here is the basic code I have been fighting with :
    Code:
    #include <iostream>
    
    using namespace std;
    
    class Text
    {
        public :
        char words[80] ="This is a text line"; 
    };
    class Line
    {
        public :
        void showtext(void)
        {
            cout << words; 
        }
    };
    int main()
    {
    Line line;
    line.showtext();
    cin.get();
    return 0;
    }
    But apparently I got the following errors
    ISO C++ forbids initialization of member 'text'
    error : making 'text' static
    invalid in-class initialization of static data member of non-integral type 'char[50]'
    In member function 'void Line::showtext()':
    'words' was not declared in this scope
    I understand the last error(but I have no clue how to pass char 'text' to class 'Line')
    But I don't understand the first 3 errors, I mean why would it forbid initialization of 'text'.

    I hope you guys can help me figure out what my problem is and how to pass a variable from one class to another.
    Thanks in advanced.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Your problems come from design more than anything, or rather lack of design.

    There are a few ways to do what you want. The two biggest being you could store an instance of Text in your Line class. Or you could extend the Text class into the Line class.

    Logically speaking, in this case I would probably go with the former. Something like:

    Code:
    #include <iostream>
    
    class Text
    {
    public:
       std::string words;
    
       Text() : words("This is a text line") {}
    };
    
    class Line
    {
    private:
       Text label_;
    
    public:
    
       void showText()
       {
          std::cout << label_.words << std::endl;
       }
    
       void setText(const Text & tt)
       {
          label_ = tt; 
       }
    };
    
    int main(void)
    {
       Line line;
       Text tt;
    
       line.showText();
    
       std::cin >> tt.words; 
    
       line.setText(tt);   
       line.showText();
    
       return 0;
    }
    Which itself suffers from some design problems, but it's just a demonstration :-)

    > ISO C++ forbids initialization of member 'text'
    Just means... well what it says. You cannot initialise members like that (see my sample code for how I did it).
    You should also probably be using the "C++ way", that is std::string instead of a character array.
    Last edited by zacs7; 06-19-2009 at 11:19 PM.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    8
    Your problems come from design more than anything, or rather lack of design.
    How do I fix this problem?


    And there is some part of your code I don't understand...
    that's this part of your code
    Code:
     Text() : words("This is a text line") {}
    If I am not mistaking Text() is the constructor but what does the : mean and why is words acting like a function? isn't it a variable type? (I read allot of c++ books but I never saw this method before)

    Oh and can you explain
    Code:
    void setText(const Text & tt)
       {
          label_ = tt; 
       }
    Because I don't fully understand its function, according to me it just gets the Text memory address but I may be wrong so its best for you to explain the code a little.
    Thanks in advanced.

  4. #4
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by MasterM View Post
    And there is some part of your code I don't understand...
    that's this part of your code
    Code:
     Text() : words("This is a text line") {}
    If I am not mistaking Text() is the constructor but what does the : mean and why is words acting like a function? isn't it a variable type? (I read allot of c++ books but I never saw this method before)
    This is the constructor initialization list. You will most probably use this as you proceed with your programming. It's just the same as doing:

    Code:
    Text()
    {
        words = "This is a text line";
    }
    The initialization list is used more often (I think :S) and is the preferred way - in my case anyways - of initialising member variables using the constructor. Words isn't "acting like a function", it just contains parenthesis to save time and space. Whatever you want your variable to be set to, when using the constructor initialization list, should be defined inside the parenthesis following the variable. A good example of this is setting pointers created inside a class to NULL.

    Quote Originally Posted by MasterM View Post
    Oh and can you explain
    Code:
    void setText(const Text & tt)
       {
          label_ = tt; 
       }
    Because I don't fully understand its function, according to me it just gets the Text memory address but I may be wrong so its best for you to explain the code a little.
    Thanks in advanced.
    The ampersand(&) operator can be used for many things, depending on it's context. In this case, the setText() function passes a reference to a constant Text object. Read up on references, your book should tell you all about them

    --
    Legit
    Last edited by legit; 06-20-2009 at 02:31 AM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    The tool that is C++ comes with a wonderful, free, downloadable manual. It's always a good idea to work with one of the latter when trying to learn the former.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    8
    Where can i find this downloadable manual?
    Thanks in advanced.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MasterM View Post
    Where can i find this downloadable manual?
    Thanks in advanced.
    Did you click on the link he provided (i.e. the red text)?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    8
    Sorry my mistake, did not see that
    And between Thanks to everyone that helped me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Passing a string to a class
    By micpi in forum C++ Programming
    Replies: 1
    Last Post: 06-04-2007, 07:56 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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