-
June 3rd, 2009, 04:11 PM
#1
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
-
June 3rd, 2009, 04:34 PM
#2
Re: multiple instances of a class
The strange thing here is that the first one works. It really shouldn't.
-
June 3rd, 2009, 04:35 PM
#3
Re: multiple instances of a class
No idea. Post the declaration of the class (using code tags).
Viggy
-
June 3rd, 2009, 05:09 PM
#4
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.
-
June 3rd, 2009, 07:48 PM
#5
Re: multiple instances of a class
Originally Posted by sgiri1981
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.
-
June 3rd, 2009, 08:01 PM
#6
Re: multiple instances of a class
Originally Posted by sgiri1981
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!
-
June 3rd, 2009, 09:33 PM
#7
Re: multiple instances of a class
There are three reasons why you should have default constructor.
Thanks for your help.
-
June 4th, 2009, 10:33 AM
#8
Re: multiple instances of a class
Originally Posted by Paul McKenzie
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.
Originally Posted by NonstopChachacha
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
-
June 5th, 2009, 04:13 AM
#9
Re: multiple instances of a class
Originally Posted by sgiri1981
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.
-
June 5th, 2009, 10:57 AM
#10
Re: multiple instances of a class
Originally Posted by Paul McKenzie
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
-
June 5th, 2009, 11:06 AM
#11
Re: multiple instances of a class
Originally Posted by sgiri1981
(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.
-
June 5th, 2009, 04:34 PM
#12
Re: multiple instances of a class
Originally Posted by sgiri1981
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.
-
June 6th, 2009, 11:22 PM
#13
Re: multiple instances of a class
Originally Posted by AlastrionaAdair
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!!! :-)
-
June 6th, 2009, 11:46 PM
#14
Re: multiple instances of a class
Originally Posted by sgiri1981
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|