Link to home
Start Free TrialLog in
Avatar of liseperu
liseperu

asked on

How do you save an instance of a class in another class?

I have a class structure as follows:

Interface contains one Application;

Originally I had the class application taking two parameters via its constructor, i.e.

In the Interface header file I had:

#ifndef INTERFACE_H
#define INTERFACE_H

#include "Application.h"

class Interface
{
public:
      Interface();
      ~Interface();

      void _mainMenu();
      void _networkMenu();

private:
      Application _application;

};

#endif

and in the .cpp file I had:

Interface::Interface()
{
      Application a(0.03, 1000);

      _application = a;
}

the Application header file was as follows:

#ifndef APPLICATION_H
#define APPLICATION_H

#include "Learning_Algorithm.h"

class Application
{
public:
      Application();
                Appliction(double,int);
      ~Application();

      void _trainNetwork();
      void _createNetwork(); //function to create the neural network
      void _getResults();

private:
      Learning_Algorithm _trainer;
};

At this point everything eorked perfectly.
Then I changed it so that there would be no passing of parameters to this:

Interface header file is the same:

Application header file:

#ifndef APPLICATION_H
#define APPLICATION_H

#include "Learning_Algorithm.h"

class Application
{
public:
      Application();
      ~Application();

      void _trainNetwork(); //train network function, takes parameters: number of iterations and learning rate
      void _createNetwork(); //function to create the neural network
      void _getResults();

private:
      Learning_Algorithm _trainer;
};

#endif

and the interface .cpp file to this:

Interface::Interface()
{
      Application a();

      _application = a;
}


#endif

now I get this error:

C:\Documents and Settings\Matt\My Documents\Uni\Dissertation\Program\NN for Data Clustering\Interface.cpp(32) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Application (__cdecl *)(void)' (or there is no acceptable conversion)

I don't get why it doesn't work any more!?

Please help.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AlexFM
AlexFM

You can remove both lines from Interface constructor since _application is already created with default constructor.

Interface::Interface()
{
     // Application a();
     // _application = a;
}
Try

Interface::Interface()
{
    Application a; // without '()'

    _application = a;
}

Also, provide some code surrounding that statment, this might just be a side effect.
FYI:
I also recommend that you not using variable names and variable functions that begin with an underscore.

IAW C++ standard, names that begin with underscore are reserved for the implementation.

If used in the wrong way, this can make your code non-portable.

You can instead use underscore at the end.
application_ = a;

Then your code will be portable.