|
|
constructors & instantiation
|
|
|
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
probably a stupid question, but i am curious about the answer and thought i would ask . . . in C++ when i declare an array of objects (or try to declare multiple instances not in an array) of a type i define and use a constructor in my class, i get an error saying no appropriate constructor (or something to that effet). i can fix that problem by inserting an empty constructor. is there something i am doing wrong? a made up example:
Code:
class MyClass{
private:
...
public:
MyClass(char value[], int numValue);
MyClass();
};
//declare my class and all that goes with it
MyClass::MyClass(char value[]="", int numValue=0)
{
insert code here to do stuff
}
//constructor that sets data fields
MyClass::MyClass()
{
}
//empty constructor
int main(argc, const * char argv[]) /*sorry if i have the parameters wrong, i am trying to do this from memory*/
{
MyClass myClassObjects[5];
return 0;
}
without the empty constructor that has nothing in it, i get the afore mentioned error. is there something wrong with my code? or is that just the way it works? thanks!!
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Dec 2004
Status:
Offline
|
|
Originally posted by zanyterp:
probably a stupid question, but i am curious about the answer and thought i would ask . . . in C++ when i declare an array of objects (or try to declare multiple instances not in an array) of a type i define and use a constructor in my class, i get an error saying no appropriate constructor (or something to that effet). i can fix that problem by inserting an empty constructor. is there something i am doing wrong? a made up example:
Code:
class MyClass{
private:
...
public:
MyClass(char value[], int numValue);
MyClass();
};
//declare my class and all that goes with it
MyClass::MyClass(char value[]="", int numValue=0)
{
insert code here to do stuff
}
//constructor that sets data fields
MyClass::MyClass()
{
}
//empty constructor
int main(argc, const * char argv[]) /*sorry if i have the parameters wrong, i am trying to do this from memory*/
{
MyClass myClassObjects[5];
return 0;
}
without the empty constructor that has nothing in it, i get the afore mentioned error. is there something wrong with my code? or is that just the way it works? thanks!!
It works fine for me. Sp long as you have a constructor which can be called with no parameters, it should work fine. I cut and pasted your code (and removed the default constructor declaration and definition) and it compiled and ran, once I corrected the syntax errors.
The following compiled with no errors:
Code:
class MyClass{
private:
public:
MyClass(char value[], int numValue);
};
//declare my class and all that goes with it
MyClass::MyClass(char value[]="", int numValue=0)
{
//insert code here to do stuff
}
//constructor that sets data fields
int main(int argc, const char *argv[]) /*sorry if i have the parameters wrong, i am trying to do this from memory*/
{
MyClass myClassObjects[5];
return 0;
}
--sam
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
Originally posted by ideasculptor:
It works fine for me. Sp long as you have a constructor which can be called with no parameters, it should work fine. I cut and pasted your code (and removed the default constructor declaration and definition) and it compiled and ran, once I corrected the syntax errors.
The following compiled with no errors:
Code:
class MyClass{
private:
public:
MyClass(char value[], int numValue);
};
//declare my class and all that goes with it
MyClass::MyClass(char value[]="", int numValue=0)
{
//insert code here to do stuff
}
//constructor that sets data fields
int main(int argc, const char *argv[]) /*sorry if i have the parameters wrong, i am trying to do this from memory*/
{
MyClass myClassObjects[5];
return 0;
}
--sam
hmmmm. . .i will have to re-check it then. see if i had something different in my example (or it could just be the compiler i use at school, but i thought i had had the same problem at home with xcode) . . . thanks! other than the wrong main(), what else did i miss?
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Dec 2004
Status:
Offline
|
|
your arguments to the main functions were incorrect.
--sam
|
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
Originally posted by ideasculptor:
your arguments to the main functions were incorrect.
--sam
this part?
int main(int argc, const char *argv[])
thanks!
nick
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|