Basic OOP question...
-
Hi, I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ? Thanks, Bye ! Braulio
-
Hi, I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ? Thanks, Bye ! Braulio
Hello, You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example, class Basic { Basic(int nOne); .... } Here you cannot say... Basic* pBasic = new Basic; // since nOne is not known Basic theBasic; You will have to say Basic* pBasic = new Basic(1111); // for instance Basic theBasic(1111); For the definition class Basic { Basic(); ...... } You can write... Basic theBasic; Basic* pBasic = new Basic; OR Basic* pBasic = new Basic(); I hope this clears the problem. If it does, then consider a little more study of constructors/destructors. Bye, Paul. ================== The original message was: Hi,
I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?
Thanks, Bye !
Braulio -
Hello, You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example, class Basic { Basic(int nOne); .... } Here you cannot say... Basic* pBasic = new Basic; // since nOne is not known Basic theBasic; You will have to say Basic* pBasic = new Basic(1111); // for instance Basic theBasic(1111); For the definition class Basic { Basic(); ...... } You can write... Basic theBasic; Basic* pBasic = new Basic; OR Basic* pBasic = new Basic(); I hope this clears the problem. If it does, then consider a little more study of constructors/destructors. Bye, Paul. ================== The original message was: Hi,
I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?
Thanks, Bye !
BraulioThanks but..., Sorry, the problems that I have are with the following derived class: class CDrawToScreenThread : public CGeneralDrawingThread { CDrawToScreenThread (){} // I f not, give a compiler error CDrawToScreenThread (CWnd *pWnd, ScreenParam ScreenParams); // My constructor with params... } } ================== The original message was: Hello,
You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example,
class Basic
{
Basic(int nOne);
....
}Here you cannot say...
Basic* pBasic = new Basic; // since nOne is not known
Basic theBasic;You will have to say
Basic* pBasic = new Basic(1111); // for instance
Basic theBasic(1111);For the definition
class Basic
{
Basic();
......
}You can write...
Basic theBasic;
Basic* pBasic = new Basic;
OR Basic* pBasic = new Basic();I hope this clears the problem. If it does, then consider a little more study of constructors/destructors.
Bye,
Paul.==================
The original message was:Hi,
I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?
Thanks, Bye !
Braulio -
Hi, I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ? Thanks, Bye ! Braulio
================== The original message was: Hi,
I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?
Thanks, Bye !
Braulio Hi Braulio! example: Class A { public: A(int x, int y) {...} ... } int main() { A p(1,2); // Call the constructor defined A p; // incorrect! the default constructor doesn't exist. You have to declare it. ... } If you defined your own constructor, you have to respect the list of arguments. Not if you add the default constructor (without argument). Is your case? Tchao Sandrine -
================== The original message was: Hi,
I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?
Thanks, Bye !
Braulio Hi Braulio! example: Class A { public: A(int x, int y) {...} ... } int main() { A p(1,2); // Call the constructor defined A p; // incorrect! the default constructor doesn't exist. You have to declare it. ... } If you defined your own constructor, you have to respect the list of arguments. Not if you add the default constructor (without argument). Is your case? Tchao SandrineHi Sandrine ! Good To hear from you here in CodeProject ! I'm not sure what have I made wrong, It was with a class that inherited from a CWinThread, and this class I declare it as a dinamyc class ( I think that is the name :-( ), then I think the problem is that it needs a default constructor, but I'm not quite sure... ( sometimes C++ is a "pretty" maze and the easiest thing make me a little bit crazy). Ciao ! Braulio
-
Hi Sandrine ! Good To hear from you here in CodeProject ! I'm not sure what have I made wrong, It was with a class that inherited from a CWinThread, and this class I declare it as a dinamyc class ( I think that is the name :-( ), then I think the problem is that it needs a default constructor, but I'm not quite sure... ( sometimes C++ is a "pretty" maze and the easiest thing make me a little bit crazy). Ciao ! Braulio
The VC++ docs say that a default constructor is generated if (and only if) no other constructor has been defined. Once you define the constructor with parameters, you must also explicitly define the 'default'constructor if it is needed. ================== The original message was: Hi Sandrine !
Good To hear from you here in CodeProject !
I'm not sure what have I made wrong, It was with a class that inherited from a CWinThread, and this class I declare it as a dinamyc class ( I think that is the name :-( ), then I think the problem is that it needs a default constructor, but I'm not quite sure... ( sometimes C++ is a "pretty" maze and the easiest thing make me a little bit crazy).
Ciao !
Braulio -
Thanks but..., Sorry, the problems that I have are with the following derived class: class CDrawToScreenThread : public CGeneralDrawingThread { CDrawToScreenThread (){} // I f not, give a compiler error CDrawToScreenThread (CWnd *pWnd, ScreenParam ScreenParams); // My constructor with params... } } ================== The original message was: Hello,
You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example,
class Basic
{
Basic(int nOne);
....
}Here you cannot say...
Basic* pBasic = new Basic; // since nOne is not known
Basic theBasic;You will have to say
Basic* pBasic = new Basic(1111); // for instance
Basic theBasic(1111);For the definition
class Basic
{
Basic();
......
}You can write...
Basic theBasic;
Basic* pBasic = new Basic;
OR Basic* pBasic = new Basic();I hope this clears the problem. If it does, then consider a little more study of constructors/destructors.
Bye,
Paul.==================
The original message was:Hi,
I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?
Thanks, Bye !
BraulioI suppose you get the compiler error in a IMPLEMENT_DYNCREATE macro. When you want to be able to create the thread with AfxBeginThread, your thread gets instantiated for you and therefore MFC will call the constructor. The logic for this is in CObject//CreateObject i believe. The MFC logic has no way to pass parameters to your c onstructor, so whenenver you use a DECLARE_DYNCREATE macro you need a default constructor. ================== The original message was: Thanks but...,
Sorry, the problems that I have are with the following derived class:
class CDrawToScreenThread : public CGeneralDrawingThread
{CDrawToScreenThread (){} // I f not, give a compiler error
CDrawToScreenThread (CWnd *pWnd, ScreenParam ScreenParams); // My constructor with params...
}
}==================
The original message was:Hello,
You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example,
class Basic
{
Basic(int nOne);
....
}Here you cannot say...
Basic* pBasic = new Basic; // since nOne is not known
Basic theBasic;You will have to say
Basic* pBasic = new Basic(1111); // for instance
Basic theBasic(1111);For the definition
class Basic
{
Basic();
......
}You can write...
Basic theBasic;
Basic* pBasic = new Basic;
OR Basic* pBasic = new Basic();I hope this clears the problem. If it does, then consider a little more study of constructors/destructors.
Bye,
Paul.==================
The original message was:Hi,
I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?
Thanks, Bye !
Braulio -
Hi, I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ? Thanks, Bye ! Braulio
This is a basic C++ question, not OOP. When ever you create a class the compiler automatically creates a default constructor and a copy constructor. The default constructor is required if you want to create objects like this:
CFoo foo;
As soon as you create ANY constructor, the compiler requires you to write ALL constructors. In other words, it no longer creates the default constructor for you. That means if you write:struct Point { int x; int y; }; Point origin; // legal - using compiler-supplied default constructor
...but if you write this...struct Point { int x; int y; Point(int x, int y): x(x), y(y) {} };
...the compiler no longer generates the default constructor...Point origin; // illegal! no default constructor Point origin (12,45); // legal - using Point(int,int)
Note that a default constructor does not have to take 0 arguments, you simply must be able to call it with 0 arguments. Like this...struct Point { int x; int y; Point(int x=0, int y=0): x(x), y(y) {} // default parameters };
Now everything's happy again...Point origin; // legal - using Point(int,int) Point origin (12,45); // legal - using Point(int,int)
Hope this clarifies more than it confuses! Cheers, Eric