Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > constructors & instantiation

constructors & instantiation
Thread Tools
zanyterp
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status: Offline
Reply With Quote
Mar 1, 2005, 09:46 PM
 
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.
     
ideasculptor
Forum Regular
Join Date: Dec 2004
Status: Offline
Reply With Quote
Mar 1, 2005, 11:22 PM
 
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
     
zanyterp  (op)
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status: Offline
Reply With Quote
Mar 1, 2005, 11:31 PM
 
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.
     
ideasculptor
Forum Regular
Join Date: Dec 2004
Status: Offline
Reply With Quote
Mar 2, 2005, 08:26 PM
 
your arguments to the main functions were incorrect.

--sam
     
zanyterp  (op)
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status: Offline
Reply With Quote
Mar 2, 2005, 09:45 PM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 03:22 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,