• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

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
0
liseperu
Asked:
liseperu
  • 2
1 Solution
 
AxterCommented:
Hi liseperu,
Try the following:

Interface::Interface()
{
     Application a; // *** remove () brackets  ***

     _application = a;
}

David Maisonave :-)
Cheers!
0
 
AlexFMCommented:
You can remove both lines from Interface constructor since _application is already created with default constructor.

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

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

    _application = a;
}

Also, provide some code surrounding that statment, this might just be a side effect.
0
 
AxterCommented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now