Initialising Class Object within another Class header file.

Hi All,

I am trying to create a class object within another class, this enables me to use the object within the main cpp file I want to use without it being enabled globally. However it doesn't seem to work and spits out an error if declared in the classtest.h. However if I initialise it within classtest.cpp it works fine as its within scope. Trying to understand why I cant initialise class object within another class header file. Also I have read that constructor/de constructor is default doest need to be used unless you specifically need to set variables within it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

repclass.h

#ifndef __REPCLASS_H__
#define __REPCLASS_H__

#include <iostream>

using namespace std;

class repclass
{

public:

    void methodFunction()
    {
        cout << "Your magic number is : " << (x + y) << endl;
    }

    int methodFunction2(int x, int y)
    {
          return ( x * y );
    }

    int methodFunction3()
    {
          return ( x * y * 2);
    }

private :

   static int const x = 2;
   static int const y = 2;

};

#endif // __CLASSTEST_H__


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

classtest.h

#ifndef __CLASSTEST_H__
#define __CLASSTEST_H__

#include <iostream>
#include "repclass.h"

using namespace std;

class classTest
{

public:

repclass Test1; <<<< Doesnt work when trying to use ClassTest.cpp

private :


};

#endif // __CLASSTEST_H__


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

classtest.cpp

#include <iostream>
#include "classtest.h"

using namespace std;

int main()
{
   // repclass Test1; << This works if initialised within main() but if removed and use this line in classtest header file it doesnt like it....

   Test1.methodFunction();
   cout << "The other magic number is : " << Test1.methodFunction2(5,5) << endl;
   cout << "The other other magic number is : " << Test1.methodFunction3() << endl;

   int a = 10;
   int b = 20;
   int c = 20;

   // 1. Lambada function only specifying referenced integers  &a &b into function

   auto Lambada1 = [ &a, &b ](int x, int y) { return x + y + a + b; };
      cout << "lambada example 1 : " << Lambada1(5, 2) << endl;

   // 2. Lambada function allows all references within Scope

   auto Lambada2 = [&](int x, int y) { return x + y + a + b + c; };
      cout << "lambada example 2 : " << Lambada2(5, 2) << endl;


}


Last edited on
I don't know what exactly the problem is, but you need an instance of the class in order to access its non static member:
1
2
classTest ct;
ct.Test1.methodFunction();
it doesn't seem to work

Its hard to guess the error. Please describe it.
Coder777 - I create the instance in classtest.h - "classtest Test1" - when I try use "Test1.methodfunction()" within the classtest.cpp. I dont want to create instance on main cpp file I want to create the instance on header then use its object components and methods on the main cpp?

tcs - I get the following error -
1
2
3
4
5
root@dev:/home/xxx/class# g++ -std=c++11 classtest.cpp -o classtest.exe
classtest.cpp: In function ‘int main()’:
classtest.cpp:10:4: error: ‘Test1’ was not declared in this scope
    Test1.methodFunction();
    ^
Last edited on
Your given files doesn't seem to generate this error message. Are those files you've given here are really the original ones? F.e. line 1..3 aren't original as well as lines 17 and 11 of "class.h" and "classtest.cpp". You've also named "classtest.h" as class.h. Does there exist both files? So the complier may choose the "classtest.h" while you does offer here "class.h".
Tcs your right it's classtest.h also commented line out on classtest.cpp as showed that it working when initiated on main. Hopefully the above now should error correctly.
Last edited on
Topic archived. No new replies allowed.