CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    May 2009
    Posts
    28

    multiple instances of a class

    Dear All,
    I m trying to use a class that does not have a default constructor. For this class, i m unable to declare 2 instances.

    eg:
    option 1 (successful compilation)
    MyClass obj1; // ok

    option 2 (error)
    MyClass obj1, obj2;

    option 3 (error)
    MyClass obj1;
    MyClass obj2;

    The error I get is: "no appropriate default constructor"

    Is it the lack of default constructor or something else that prevents multiple instances? I remember in java there are mechanisms by which we can restrict the number of instances of a class; but i dont see anything special in the class that I m using.

    Thanks for looking,
    sgiri

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: multiple instances of a class

    The strange thing here is that the first one works. It really shouldn't.

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: multiple instances of a class

    No idea. Post the declaration of the class (using code tags).

    Viggy

  4. #4
    Join Date
    May 2009
    Posts
    28

    Re: multiple instances of a class

    Here is the code: (its a long header file and I had to cut down on other details. Also, I had to change some names as this is a client's proprietary code)
    Code:
    class __IMP_EXP MyClass : public ParentClass
    {
    
    public:
    
        MyClass (SomeClass* pSomeClass);
        
        virtual ~MyClass();
        
        virtual bool fnc1 (parameters);
        
        virtual bool fnc2 (parameters);
        
    private:
    
        //private declarations
        
        
    private:
        
        MyClass(const MyClass &right);
        
        const MyClass & operator=(const MyClass &right);
    };
    And, here is the relevant code from the .cpp file
    Code:
    MyClass::MyClass (SomeClass* pSomeClass)
    : member1(SOME_CONSTANT), ParentClass(pSomeClass)
    {
        memberN = some_value;
    }
    
    
    MyClass::~MyClass()
    {}
    Finally, here is how I m using it in a class called MyProcess
    It is a protected member in MyProcess.h. In the constructor initializer list of MyProcess, I pass a NULL to the instance of MyClass.


    On a slightly divergent note, is anyone familiar with __IMP_EXP? I believe its got something to do with import/export of DLLs, but could not see how it fits into the scheme described at Microsoft's tutorial: http://msdn.microsoft.com/en-us/libr...f8(vs.71).aspx

    Thanks,
    sgiri
    Last edited by sgiri1981; June 3rd, 2009 at 07:01 PM.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: multiple instances of a class

    Quote Originally Posted by sgiri1981 View Post
    Dear All,
    I m trying to use a class that does not have a default constructor. For this class, i m unable to declare 2 instances.

    eg:
    option 1 (successful compilation)
    MyClass obj1; // ok
    Where is this declaration? Inside a class? You can't just post "naked" code without the proper context of where that line of code resides.
    If it is not a class member, the first one should not have worked if you state it does not have a default constructor.

    Why not post a complete example of what you're trying to compile? Don't post only "relevant" code, as compiler errors need to be accompanied by the exact code you're compiling. By doing this, we are not guessing and coming up with dummy code to try and duplicate the error.
    Is it the lack of default constructor or something else that prevents multiple instances?
    There is no such compile-time mechanism in C++ that limits the number of instances. That's why you need to post a complete, but small example. It doesn't matter what you're actually trying to program -- a simple, do nothing, dummy class that has the characteristics you speak of (no default constructor, etc.) is all that's necessary.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 3rd, 2009 at 07:51 PM.

  6. #6
    Join Date
    May 2009
    Posts
    7

    Re: multiple instances of a class

    Quote Originally Posted by sgiri1981 View Post
    Here is the code: (its a long header file and I had to cut down on other details. Also, I had to change some names as this is a client's proprietary code)
    Code:
    class __IMP_EXP MyClass : public ParentClass
    {
    
    public:
    
        MyClass (SomeClass* pSomeClass);
        
        virtual ~MyClass();
        
        virtual bool fnc1 (parameters);
        
        virtual bool fnc2 (parameters);
        
    private:
    
        //private declarations
        
        
    private:
        
        MyClass(const MyClass &right);
        
        const MyClass & operator=(const MyClass &right);
    };
    And, here is the relevant code from the .cpp file
    Code:
    MyClass::MyClass (SomeClass* pSomeClass)
    : member1(SOME_CONSTANT), ParentClass(pSomeClass)
    {
        memberN = some_value;
    }
    
    
    MyClass::~MyClass()
    {}
    Finally, here is how I m using it in a class called MyProcess
    It is a protected member in MyProcess.h. In the constructor initializer list of MyProcess, I pass a NULL to the instance of MyClass.


    On a slightly divergent note, is anyone familiar with __IMP_EXP? I believe its got something to do with import/export of DLLs, but could not see how it fits into the scheme described at Microsoft's tutorial: http://msdn.microsoft.com/en-us/libr...f8(vs.71).aspx

    Thanks,
    sgiri
    Also, please remember that ___ is for library writing and it is truly hard to read for ALL general coders. Don't think that writing so will show how you are _obeying_ Microsoft. If you are good enough then do the stuff on your own after reading the information of its use on the web, I don't advise you to buy any C++ books because their information is all in MSDN and around the web and get compiled into books by the authors.

    Back to what you might be wanting to imply as I think you might already have the answer.
    Your problem is about type (class struct can be considered as types)
    Type S
    {
    S(); //default constructor
    S(Type F * pF); // pointer to type F ezisting somewhere in your program
    };

    Use of S with pointer needs you to pass a raw pointer or a reference (&) of a type F instance

    Exporting a class with those macro via #define, clearly it must be somewhere in the headers your program is including!

    Regardlessly!

  7. #7
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: multiple instances of a class

    There are three reasons why you should have default constructor.
    Thanks for your help.

  8. #8
    Join Date
    May 2009
    Posts
    28

    Re: multiple instances of a class

    Quote Originally Posted by Paul McKenzie View Post
    Where is this declaration? Inside a class? You can't just post "naked" code without the proper context of where that line of code resides.

    Why not post a complete example of what you're trying to compile? Don't post only "relevant" code, as compiler errors need to be accompanied by the exact code you're compiling. By doing this, we are not guessing and coming up with dummy code to try and duplicate the error.
    Sorry about that. I was just trying to post the sections of code that I suspected of having problems and thereby avoid unnecessarily large post with unrelated code. I was mistaken and I apologize as the error was in the code that I didnt post.


    Quote Originally Posted by NonstopChachacha View Post
    Also, please remember that ___ is for library writing and it is truly hard to read for ALL general coders....
    ...
    Again I apologize for this misunderstanding. The only reason I included ___ in my code is that I myself dont understand it (i didnt write that code; i m just making changes to the project) and that I suspected it as a possible reason for the error. Thats why I included a query about it at the end of my post.


    Back to the problem at hand:
    I did manage to understand the problem, which was due to the vestiges of java programming in my thinking (i used to work on java and now I m shifting to C++). Briefly, I wanted to just declare (not instantiate) the second object; at a later stage, I wanted assign the instance of first object to the second. Alas, this is not possible in C++ without pointers, whereas in Java, each object of a Class is "like" pointer.

    These differences are summarized by jefranki in reply to one of my earlier post, which i had read then, but didnt recollect during the few hours that I spent debugging my problem: http://www.codeguru.com/forum/showth...ight=sgiri1981

    Thanks to all,
    sgiri

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: multiple instances of a class

    Quote Originally Posted by sgiri1981 View Post
    I did manage to understand the problem, which was due to the vestiges of java programming in my thinking (i used to work on java and now I m shifting to C++).
    Do not use Java as a model in coding C++, or try to leverage what you learned in Java to C++. That will lead you down the path to nowhere very fast. Buggy, inefficient, and plain crappy looking C++ code will await you if you try to use Java-style coding in C++.

    Java and C++ are two different languages -- just because they look similar means nothing.
    Briefly, I wanted to just declare (not instantiate) the second object; at a later stage, I wanted assign the instance of first object to the second. Alas, this is not possible in C++ without pointers,
    Why not just have accessor methods to set the object accordingly after it has been constructed? You don't need pointers for this, what you need is to think in C++ and not in Java.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 5th, 2009 at 04:16 AM.

  10. #10
    Join Date
    May 2009
    Posts
    28

    Re: multiple instances of a class

    Quote Originally Posted by Paul McKenzie View Post
    Why not just have accessor methods to set the object accordingly after it has been constructed? You don't need pointers for this, what you need is to think in C++ and not in Java.
    Thanks for your comments. I tried a few approaches and realized that I cant do this because
    1. I dont want to construct this second object
    2. The '=' operator in this object was not working (it gives some message, which reads something like "cannot access private methods"). Much of this class in inherited and I m not sure how operator overloading works across multiple hierarchies. I saw an easy way out with pointers (and some earlier posts recommend excessive use of pointers in C++, esp. to support polymorphism)

    Thanks again,
    sgiri

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: multiple instances of a class

    Quote Originally Posted by sgiri1981 View Post
    (and some earlier posts recommend excessive use of pointers in C++, esp. to support polymorphism)
    Pointers are to some extent necessary to support polymorphism. "Excessive" use of them is a bad idea, but when dealing with inheritance-based polymorphism you have to pull them out now and then. You should consider using smart pointers when you do.

    It's also worth noting that while C++ supports inheritance-based "runtime polymorphism", the language also supports template-based "compile-time polymorphism", which Java does not. This type of polymorphism presents its own set of challenges, but should be kept in mind as an alternative to inheritance.
    Last edited by Lindley; June 5th, 2009 at 12:41 PM.

  12. #12
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    Re: multiple instances of a class

    Quote Originally Posted by sgiri1981 View Post
    Dear All,
    I m trying to use a class that does not have a default constructor. For this class, i m unable to declare 2 instances.

    eg:
    option 1 (successful compilation)
    MyClass obj1; // ok

    option 2 (error)
    MyClass obj1, obj2;

    option 3 (error)
    MyClass obj1;
    MyClass obj2;

    The error I get is: "no appropriate default constructor"

    Is it the lack of default constructor or something else that prevents multiple instances? I remember in java there are mechanisms by which we can restrict the number of instances of a class; but i dont see anything special in the class that I m using.

    Thanks for looking,
    sgiri
    What constructor do you think is called when you write: MyClass obj1; ?
    Remember when you instantiate a class its constructor always gets called. If you realise which one is called, you have found the cause of your problem.

    Note that while in Java you always instantiate a class with the new keyword, in C++ this isn't the same.

  13. #13
    Join Date
    May 2009
    Posts
    28

    Re: multiple instances of a class

    Quote Originally Posted by AlastrionaAdair View Post
    Note that while in Java you always instantiate a class with the new keyword, in C++ this isn't the same.
    Yes. However, it took me more than 5 hours to learn this- I looked at all other possible places for error, pored over the other concepts.

    Experience is what you get when you dont get what you want!!! :-)

  14. #14
    Join Date
    Sep 2008
    Posts
    113

    Re: multiple instances of a class

    Quote Originally Posted by sgiri1981 View Post
    Thanks for your comments. I tried a few approaches and realized that I cant do this because
    1. I dont want to construct this second object
    2. The '=' operator in this object was not working (it gives some message, which reads something like "cannot access private methods"). Much of this class in inherited and I m not sure how operator overloading works across multiple hierarchies. I saw an easy way out with pointers (and some earlier posts recommend excessive use of pointers in C++, esp. to support polymorphism)

    Thanks again,
    sgiri
    Ah, I think I can quickly explain something about number 2 there. The copy constructor and overloaded '=' operator are declared in the private section of the class, which means you can't use them the normal way they'd be used. I assume the code was made that way for a reason, of course, but just trying to pass on some knowledge.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured