-2

I have a problem in this code. I want vfunc to be public so any function in VI_Smooth can use the same instance and at the same time I cannot initiate the vfunc instance because I don't have Commands yet! the new command is giving me wrong results, I cannot change the code of vfunc class and there is no operator= to vfunc.

my problem is that I want to use the same name of the Object vfunc in VI_Smooth without initiating a new one with the constructorthat takes Commands inside the vfunc class

#include <vfunc.h>
class VI_Smooth
{   
public:
    const char* filename;
    Layers _layers; 
    vfunc VF;
    int nPts;

    VI_Smooth()
    {
        // ...
       int commands = 0;
        VF = vfunc(commands); // here is my problem
    }

    // ...
};

class vfunc
{
    vfunc()
    {
        // ...
    }

    vfunc(int commands)
    {
        // ...
    }

    // ...
}
Eyad Sibai
  • 811
  • 7
  • 21
  • The question is not clear to me. And please learn to properly format the code before posting. – Nawaz Aug 13 '11 at 08:39
  • I am sorry but you'll have to learn about the "new" keyword, as well as references to objects, and probably even (smart) pointers. – Benoît Aug 13 '11 at 08:39
  • 3
    The code and the question suggests that you're rather new to C++. I recommend picking up [a proper introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), because frankly there's lots of things wrong with `VI_Smooth` and `vfunc` even with the fact that I'm not understanding what they're supposed to do. – In silico Aug 13 '11 at 08:40
  • I have fixed the code the explained my problem – Eyad Sibai Aug 13 '11 at 08:50

2 Answers2

1

You need to declare your vfunc class in advance, e.g.

class vfunc; // let the compiler know that vfunc exists

class VI_smooth
{
  vfunc VF;
  ...
};

class vfunc
{
  ...
};
koan
  • 3,596
  • 2
  • 25
  • 35
0

I'm not sure I understand the question. If the question is "how do I call a constructor without using the keyword new?" the answer is:

vfunc vf;

If the question is "I already have a vfunc object, and want to clear it out, how do I call its constructor?" (like the title implies), the answer is that you don't call the constructor again, but instead write a different function called something like clear() so that clear() has the effect of clearing the object's state.

However, I think the question is actually "I have three classes that rely on each other, I can't figure out a good order to define the classes so that I have definitions for class vfunc when I'm defining class Commands and have definitions for class Commands when I'm defining class vfunc.

The answer to that is "you can split the definition and the declaration of a class, this is the main reason for header files (put the declaration in the header and the definition in a separate file). But with a mutual dependency like in your question, you'll also need to forward declare either Commands or vfunc before you fill out the methods and members of the other. If you forward declare, then you'll have to change the function signature to take either a reference or a pointer:

class Commands; // forward declaration
// at this point, the compiler knows that a class Commands
// will eventually be defined, but without more information, the
// compiler can only handle Commands& and Commands*
// (i.e., references and pointers)

class vfunc {
    vfunc() { ... }

    vfunc(const Commands& command);
    // since the compiler still doesn't know anything *about* Commands,
    // like what member variables or member functions it has,
    // or even what to return for sizeof(Commands),
    // you really aren't going to be able to define this constructor here;
    // once you've declared the necessary information, you can then define
    // this function (either in this file or in another file, which #includes
    // this file in order to know about the internals of Commands

    ...
};

class Commands { ... };

// define that constructor, this can be done in a separate file
vfunc::vfunc(const Commands& command) { ... }

Forward declarations have lots of uses, most notably reducing compile times (because fewer header files need to be loaded), but also to implement the PIMPL idiom and for efficient platform dependent code (the APR does this last one).

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
  • my problem is that I want to use the same name of the Object vfunc in VI_Smooth without initiating a new one with the constructorthat takes Commands inside the vfunc class – Eyad Sibai Aug 13 '11 at 09:00
  • Then the answer is "forward declare both `vfunc` and `Commands` before declaring `VI_Smooth`, change function signatures accordingly, and go from there. However, you should know that this level of interdependence is often a big source of bugs and complexity ( http://www.amazon.com/Large-Scale-Software-Design-John-Lakos/dp/0201633620 ), so a better answer may be "see if a complete redesign can get rid the need for three classes to know about each other." – Max Lybbert Aug 13 '11 at 09:08