error C2259: 'CDocument' : cannot instantiate abstract class
-
terça-feira, 21 de agosto de 2012 13:13
I am getting the following error:
error C2259: 'CDocument' : cannot instantiate abstract class
for the following code:
BOOL CVisuComm::OnOpenDoc()
{
CDocument myCDoc; //LINE AT WHICH THE ERROR OCCURS
CInterfaceDoc myCInterfaceDoc;
char tabchar[80]="c:/test111.dat";
CString myFilename;
myFilename="c:/test111.dat";
/*if(!myCDoc.OnOpenDocument(tabchar))
{
MessageBox("Erreur à l'ouverture..","OnOpenDocument",MB_OK);
return false;
}
else
{
MessageBox("Ouverture OK..","OnOpenDocument",MB_OK);
}*/
myCInterfaceDoc.OnOpenDocument("c:/test111.dat");
return true;
}Any help appreciated.
Todas as Respostas
-
terça-feira, 21 de agosto de 2012 13:37
The error line is attempting to create a new CDocument object. It is not the correct way to get access the the already-existing CDocument object in your app. Instead, use GetDocument() to obtain a pointer to your document object.
- Marcado como Resposta Damon ZhengMicrosoft Contingent Staff, Moderator sexta-feira, 7 de setembro de 2012 09:29
-
terça-feira, 21 de agosto de 2012 13:41Agree with Scott. Also you shouldn't use CDocument class directly in you app. This is a base class for MFC document objects, so you have to derive your own class from CDocument to use it. Usually MFC Application wizard generates such class for you once you create new Application project.
Sergey
-
terça-feira, 21 de agosto de 2012 21:33
Let me explain why you are not able to use CDocumet class. It is not that you should not. It is because you cannot.
In C++ there is a special class type called an abstract class. Such a class nhas at least on pure virtual function.
Pure virtual function is a virtual member function of a class but it does not have the implementation. It is declared as:virtual void SampleOfPure(int iParameter) = 0;
I used return type void just as an example; it can be anything.
Designing class this way does not allow for an object of this class to be instantiated; it forces programmer to derive own class and override pure virtual with own implementation.
CDocument has one pure virtual: destructor.
Also note that constructor declaration uses AFX_NOVTABLE macro that expands to __declspec(novtable). It means that no vtable will be created for this class to reduce code size. It is possible only for the class that will have never any object instantiated.
Another example MFC abstract class is CView with virtual void OnDraw(CDC* pDC) = 0;Again it is not a matter you should not, you cannot.
JohnCz
- Editado JohnCz terça-feira, 21 de agosto de 2012 21:33
- Marcado como Resposta Damon ZhengMicrosoft Contingent Staff, Moderator sexta-feira, 7 de setembro de 2012 09:32
-
quarta-feira, 22 de agosto de 2012 12:13
Since ‘myCDoc’ is not used, then remove (comment) the line ‘CDocument myCDoc’.
-
quarta-feira, 22 de agosto de 2012 15:49
Since OP is trying to use document object to open file (another questionable approach) the document object is needed, hence commenting it is nor a solution.
As indicated in previous posts, since CDocumet is an abstract class OP should use CDocument derivative.
On the other hand, calling OpenDocumentFile directly will not produce expected results.I suspect that OP’s application is not supporting doc/view architecture and an attempt is made to use document’s serialization functionality.
If the assumption is correct, serialization or reading file directly to the buffer must be implemented.
If the assumption is not correct, the best way is to emply the application object and call OpenDocumentFile member of the app.JohnCz Please consider voting if you find this post helpful.
-
sexta-feira, 24 de agosto de 2012 16:59Thanks for the reply, but can u suggest an example for the same. I mean how to implement it in C++.
-
segunda-feira, 27 de agosto de 2012 19:24
You have to be clearer. What do you want to implement in C++?
JohnCz Please consider voting if you find this post helpful.

