Advertisement

no appropriate default constructor available

Started by July 04, 2001 12:24 PM
5 comments, last by stefu 23 years, 7 months ago
I''m trying to implement abstaction but the following error occurres. I just can''t find a way to solve it. error C2512: ''GFX_Win32_OpenGL'' : no appropriate default constructor available //FILE: gfx.h // includes abstract class class GFX { public: virtual void Render() = 0; }; // FILE: gfx_win32_opengl.cpp // implementation for win32 & OGL // note: I include functions directly to cpp file with class, no // include file used for each class. class GFX_Win32_OpenGL : public GFX { public: void Render() { // blaa blaa } }; // FILE main.cpp // just trying to create new GFX class GFX_Win32_OpenGL; class GFX_Win32_D3D; GFX *g_pGFX = 0L; void main() { if( bUseOpenGL ) g_pGFX = new GFX_Win32_OpenGL; else g_pGFX = new GFX_Win32_D3D; } Thanks for your time! ps. how can I put my code into white boxes. I''v seen may doing so! Thanks!
Did you create a constructor?
Every class should have a constructor and a destructor. Declare it like this:

.h file:
    class CSomeClass{public:   CSomeClass();   // Constructor   virtual ~CSomeClass(); // Destructorprotected:}  Implement it like this:.cpp file:      CSomeClass::CSomeClass{}CSomeClass::~CSomeClass{}If you have a constructor for your base class which takes a few arguments, you should have your derived class call the base class'constructor. Like this:.cpp file      CSomeDerivedClass::CSomeDerivedClass(int argument) : CSomeClass(argument)    


Edited by - Imois on July 4, 2001 1:33:58 PM
Advertisement
there is a default constructor for that class, but it''s not available since the class definition isn''t in scope where you try to instance it in main.

you can''t create an instance of a class with only a forward declaration, there must be a definition available. so, put your class definitions in headers, implimentations in .cpp''s, and then #include the appropriate header in your main.cpp.
I''ve tried the constructors, but it won''t help. My constructors doesn''t get any parameters.
Obviously it works well, if I put the class GFX_Win32_OpenGL to include file.
But in my case I''d like to have the class only in one cpp file and then just include the lines

class GFX_Win32_OpenGL;
class GFX_Win32_D3D;

in the file where the object is created. But this gives an error. Isn''t the lines above enought to enable using the class.

Bobtree: I was just answering Imois while you pushed your reply.

I was afraid it would be so I have to put my class definition to header file. Just thinking of tidier solution..?
Stefu, I think you don't understand something.


First, there is the object declaration, and the object definition.

The declaration is usually in the header file like the following :


    #ifndef A_H#define A_Hclass A{public:    void func1();    bool func2( int i ) const;private:    int i;};#endif  a definition is the actual implementation of the type and is usually in a .cpp      #include A.hvoid A::func(){  //do stuff}bool A::func2( int i ) const{  //do more stuff}  Somethimes the declaration and the definition are in the same file, but it doesn't matter to us.When you use a function of an object, you NEED to know the declaration(usually put in an header file.h). (just like when you use a standalone function you need it's prototype).  But, if you are only declaring pointers are reference, you don't need the full declaration because the compiler knows how to make a pointer. The only thing is that to know is if the type of the pointer is an existing type. Forward declaration of use for that.class A;class B;is a forward declaration of class A and B, meaning that those types are valid types, but are declared later.so writing code like this is validA* a;B* b;but writing code like this is an error, because the compiler cannot find the prototype of the constructor.A a;B b;So. If you are going to CREATE an object in a file, the object declaration MUST be included in that file( directly or indirectly(from another include).Hence(just like bobtree said), in your case, since you are using the constructor of the objectg_obj = new OPENG_GF();you need  to include the declaration of the OPEN_GF.P.s : to show code in white box use <source> then write your code and then </source>. Replace the '<' by '[' and '>' by ']'. If I had put '[' you would have seen the white box.Edited by - Gorg on July 4, 2001 4:47:23 PM  
Advertisement
Grob, that was perfect answer.
Sorry if I had misused some terms. I''v learned all myself and those english terms are not always so clear!

Thank you!

This topic is closed to new replies.

Advertisement