how to use Class with Other Classes as Member Data

Pages: 12
Hi

What is wrong with my script???

class t1{
public:
int variab;
t1(int h){
variab=h;
}
};
class t2{
public:
t1 fff();// why not t1 fff; ???

t2(int i){
t1 fff(9);
cout<<fff.variab;
};
private:
void local(){
cout<<fff.variab;
}
};


Thank you .

Julia
What is wrong with my script???

1 ) It isn't a script
2 ) It isn't inside [code][/code] tags
3 ) you are declaring a member function called fff, not a member variable. Remove the parentheses from
t1 fff();// why not t1 fff; ??? and it should be fine then
Last edited on
after I removed the parentheses the error is no matching function for call to ‘t1::t1()’
the script is the significant part of a Cpp program.
the error is for following line : t2(int i){
You must call the t2 constructor as you don't have a default one, use an initializer list:
t2(int i) : fff (9){ This calls fff's constructor passing 9 as argument
Thanks for answering, but that did not solve it. I was maybe a little vague. I have defined a class (as t1 is in my small example) and I'm using as a public variables of a second class(the t2), several (more than 50) instances of that t1 class. So the solution you gave me although it solves the previous script's problem does not solve my problem completely. I'm sure that is something theoretical that I'm missing, and I need to be pointed in the right direction. Although I've read many C++ tutorials, I wasn't able to find one with something similar.
Thanks for everything.
Do you get the same constructor error?
You need to call the constructor of all the members which don't have a default constructor - a constructor that doesn't take any argument -
I want to be able to use in the second object's private and public functions call to something like
instance1_of_class1.member, instance2_of_class1.member, .... , where "instance...." are objects of type class1, and ".member"s are variables, functions, etc.
What looks like common sense to me is to declare in private or public zone in the second class something like:
class1 instance1_of_class1; class1 instance2_of_class1;...... ,
for the various calls throughout the class2's script to acknowledge the objects instance1_of_class1, instance2_of_class1, etc.
I guess because I'm a newbie, I'm trying to apply the " type variable; " syntax to classes' objects inside of another class.... and I'm missing something either from syntax or as a concept.

Was I eloquent? Sorry for the bad English....
If you want to use private members you should declare the second class a friend of the first one.
Can you post the exact error messages?
Not private. Public members of the 1st class. But i'll use them in the functions allover the second class, in both sections of the second class, private and public. but they will be public for the objects of class2 type....
the error messages are:
when there is no t1() constructor:

no matching function for call to ‘t1::t1()’


If I'm putting in the first class that empty constructor, the error becomes:

g++ main.cpp
/tmp/ccJ4h5tj.o: In function `t2::t2(int)':
main.cpp:(.text._ZN2t2C1Ei[t2::t2(int)]+0xd): undefined reference to `t1::t1()'
collect2: ld returned 1 exit status

, although I can't see the reason why I should have an empty constructor....
Are you calling the t1 constructor in t2 constructor's initializer list?
Yes. Although not present explicitly in the above example, I will call the t2 constructor with some parameters, and after a quick compute in the t2, many t1 objects must be created. Yes, inside the t2 constructor.
Can you post the actual code of t2 constructor?
I managed finally to develop an workaround, and I came to the conclusion that this is yet another C++ serious concept bug. The bug consists in the fact that writing in the public section of a class " int variable; " and "classname object;" is fundamentally different, although they should be the same: a public declaration of variable. In the first case, it is, but in the second case, it is actually an abnormal call to the empty constructor !!! So actually the object is created there, with no parameters. And when the real declaration point is reached, later in the script, all you do is REDECLARE another object with the same name, and with the actual, useful, constructor. Object that, of course, like any other LOCAL variable will expire at the first "}", leaving you with an empty defective (public, though) object.
So the workarround is to declare locally a temporary object with a call to the complete constructor, and to send all the values to the right object...... A verry ugly and dissapointing solution.
Also the fact that you cannot use a call like: ObjectName.Constructor(param1,param2...); is also unjustified and absurd...
I'm starting to have serious doubts about Bjarne Stroustrup.......

The complete code is:

#include <iostream>
using namespace std;

class t1{
public:
int variab;
t1(){}; // added empty constructor
t1(int h){variab=h;}// constructor
};

class t2{
public:
t1 obj1;// error here if empty constructor not present
// in the t1 class !!!!!!!
t2(int i){
t1 fake(i);//constructing fake object
obj1=fake;
};

void print(){cout<<obj1.variab;}
};

int main()
{
// next call to the t2 constructor will actually
// create obj1 as member of obj2
// (as it was intended)
t2 obj2(10);
obj2.print();
return 0;
}



PS: did I mentioned that different compilers give a totally different error output? (some give no error, and no bin code ! )

I came to the conclusion that this is yet another C++ serious concept bug
First rule of programming: It's always your fault

You still aren't using initializer lists in the constructors!

1
2
3
4
5
6
7
8
9
10
11
class t1{
    public:
        int variab;
        t1(int h): variab(h){}// constructor
};

class t2{
    public:
        t1 obj1;
        t2(int i): obj1(i) {}
};

I'm sorry, but that is not my problem, and also that does not contradict my previous conclusion. It is impossible for me to use
t2(int i): obj1(i) {}
since t2 will contain an impressive number of parameters(t1 objects), and t2 will decide the number of "micro objects" to create. I would have put the entire listing if it wasn't more than 5k lines.... So unless I'm using a crystal boll to see the future, is impossible to pass obj1(i) when i call t2. ...

PS: a beautiful saying, that rule, but since I did not create C++ I guess is not for me... is for the bold guy...
PS2: the script works fine on Many other languages (object oriented or not)
I didn't mean to sound disrespectful before, and I greatly appreciate your effort.
Also if there is another way to do I'm keen to learn.

PS:also sorry for the bad English.
since t2 will contain an impressive number of parameters(t1 objects), and t2 will decide the number of "micro objects" to create. I would have put the entire listing if it wasn't more than 5k lines.... So unless I'm using a crystal boll to see the future, is impossible to pass obj1(i) when i call t2. ...
With this you mean that you don't know how many members t2 has nor how many parameters you have on t2 constructor?
the script works fine on Many other languages (object oriented or not)
How something concerning classes work for non OO languages?


BTW,
you can also used unnamed objects: obj = t1(i)
or overaload the = operator for t1 and use obj1 = i;
Since a class is a user defined data structure (the definition is not mine) and since fortran to present (past Cobol) it easier and easier to define your own data structures, it is possible. The original script was in Mega Basic 20 years ago, than in Java,... etc, and "objects" were implemented using GOSUB. Actually each dynamic homogeneous type was a variable and each function was a variable keeping the line number of a SUB....... and that is the object: a variable variables collection (BTW, basic, php and many others know variable variables, ... surprise: C does not!)

With this you mean that you don't know how many members t2 has nor how many parameters you have on t2 constructor?


Both of them. In real life, t2 has about 20 parameters half with default values, and depending on the number and the content of them a number of t1 objects are created. Then destroyed. Than created again, using public functions from t2 class as external commands. Since t2 is a tool of a tool, is impossible to pass a predefined number of parameters.
Also the fact that in the workaround I've found (with some help from nice people like you), the object is using a default constructor is a mess for me (takes a huge amount of memory, on objects that have 90% probability not to be used-initialized- at all!). So if you have any idea from this on please enlighten me, since I'm a newbie in C.

Thanks for answering

PS: I had objects 20 y ago on dBASE II . I used records as vectors and by convention the first field was the name of the script file to be loaded (and run), and a bunch of fields as parameters... I just did not know that someone will call them objects some day... :)
(BTW, basic, php and many others know variable variables, ... surprise: C does not!)
variable variables are tipical of scripting languages, C and C++ are not. Variables don't work with their names after the program gets compiled but with their memory location. Using pointers in C(++) is almost the same thing: you can create/delete variables as much as you want (Or at least as much as your computer can hold)

t2 has about 20 parameters half with default values
You can use parameters with default arguments as well as parameters without them

depending on the number and the content of them a number of t1 objects are created.
Dynamically? You shouldn't create member objects if you are going to crate them dynamically, you need pointers or containers for this

Since t2 is a tool of a tool, is impossible to pass a predefined number of parameters.
In C(++) the number of parameters in a function is hardcoded unless using variadic arguments, which is bad in C++

Also the fact that in the workaround I've found (with some help from nice people like you), the object is using a default constructor is a mess for me (takes a huge amount of memory, on objects that have 90% probability not to be used-initialized- at all!). So if you have any idea from this on please enlighten me, since I'm a newbie in C.
You can overload the () operator in a way that it will take the same arguments as the constructor eg:
1
2
3
4
5
6
7
8
9
10
class t1
{
   // some members
   t1& operator () ( /*parameters here*/ )
   {
        //do your stuff here
   }
};
t1 obj1; // default constructed
obj1 ( /*arguments here*/ ); // () operator called with no need of other instances of t1 





If I understand correctly, you are trying to create many t1 instances depending on t2 constructor arguments, am I right?
Pages: 12