-4

I a beginner in programming. I coded two classes(having constructors with requirement to pass arguments) and want to declare and use one class's object in another class.

I have tried to find the solution to my error on many website, but none of them worked. I also saw a solution to this problem using the 'new' syntax. Please suggest some(any) way to sought out this problem.

A short program similar the one in which I am facing problems is as follows: (I know this program is stupid but, this is not actual program I am facing problem in. Instead this is a narrowed down version of the part of the program in which I am facing error)

The error is in Class2.h and main.cpp

main.cpp

#include <iostream>
#include "Class2.h"
using namespace std;

int main()
{
   Class2 Class2_Obj;
   Class2_Obj.Class2_Function(); // error: undefined reference to `Class2::Class2_Function

   return 0;
}

Class1.h

#ifndef CLASS1_H_INCLUDED
#define CLASS1_H_INCLUDED

class Class1
{
   private:
   const int c1_Variable;

   public:
   Class1(int);

   // Displays the value of c1_Variable on output screan
   void Class1_Function();
};

#endif // CLASS1_H_INCLUDED

Class1.cpp

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

Class1::Class1(int receivedInt) : c1_Variable(receivedInt) {}

void Class1::Class1_Function()
{
   cout << c1_Variable;
}

Class2.h

#ifndef CLASS2_H_INCLUDED
#define CLASS2_H_INCLUDED


#include"Class1.h"

class Class2
{
   private:
   Class1 Class1_Obj(4); // 4 is just a random number.
                         //error: expected identifier before numeric constant
                         //error: expected ',' or '...' before numeric constant

   public:
   // Calls Class1_Function()
   void Class2_Function();
};

#endif // CLASS2_H_INCLUDED

Class2.cpp

#include <iostream>
#include "Class1.h"
#include "Class2.h"

void Class::Class2_Function()
{
   Class1_Obj.Class1_Function();
}

Here are the links to snapshots of the errors:

Screenshot of Error in Class2.h - https://i.stack.imgur.com/WpK9k.jpg
Screenshot of Error in main.cpp - https://i.stack.imgur.com/yDBD7.jpg

Please help me out! Thanks in advance for any responses :)

Dhruv Garg
  • 19
  • 5

2 Answers2

1

The issue is that this in-place initialization of non-static data members syntax is invalid:

class Class2
{
private:
   Class1 Class1_Obj(4);
   ....
};

You can use {} instead,

class Class2
{
private:
  Class1 Class1_Obj{4};
  ....
};

or this form

class Class2
{
private:
  Class1 Class1_Obj = Class1(4);
  ....
};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
0

C++ is a Object Oriented Language. It has classes to structure its data.

To put one class into another, you make an object of one class a member of another class.

Syntactically, it works like

class A {
   int x;
   public:
   A (int x1) : x(x1) {}
};

class B {
   A a; // this is how you do it ..
   public:
   B() : A(4) {}
};

B b; // b is an object which has a member b.a

As you can see, b is an object of class B. It has a member a of class A.

Ashish Negi
  • 5,193
  • 8
  • 51
  • 95