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 ...

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.

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.

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.

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.

Originally Posted by MasterM

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