?
Solved

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

Posted on 2005-03-10
4
Medium Priority
?
187 Views
Last Modified: 2010-04-01
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
Comment
Question by:liseperu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 13506433
Hi liseperu,
Try the following:

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

     _application = a;
}

David Maisonave :-)
Cheers!
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13506435
You can remove both lines from Interface constructor since _application is already created with default constructor.

Interface::Interface()
{
     // Application a();
     // _application = a;
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 13506441
Try

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

    _application = a;
}

Also, provide some code surrounding that statment, this might just be a side effect.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13506495
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question