0

In my one c++ file named "two_elements.cpp", I am defining two classes within same file, and the code goes like below:

#include <iostream>
#include <cstream>

class A{
    public:
        void methodA(){
            //do something
        }
};

class B{
    public:
        A a_obj;
        a_obj.methodA();
};

This throws the following error:

error: unknown type name 'a_obj'

I folowed the following stack overflow links comprehensively, but could not find a work around:

Unknown type name class

unknown type name 'class'

C++ - 2 classes 1 file

Please note most of these questions had classes in the individual header files.

1 Answers1

0

I found the solution:

#include <iostream>
#include <cstream>

class A{
    public:
        void methodA(){
            //do something
        }
};

class B{
    public:
        A a_obj;
        B(){
            a_obj.methodA();
        }
};

An instance needs to be called inside a constructor or a function in c++.