Reputation: 119
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.
Upvotes: 0
Views: 1091
Reputation:
error C2259: 'CDocument' : cannot instantiate abstract class
The compiler is telling you exactly what you need to know. You're not supposed to create a concrete instance of CDocument
- in fact you can't, as you've just seen. Instead, to quote the MSDN:
To implement documents in a typical application, you must do the following:
- Derive a class from CDocument for each type of document.
You might ask why. The answer is actually also on the MSDN, albeit in a roundabout fashion:
Override the CObject::Serialize member function in your document class to write and read the document's data to and from disk.
When the surrounding application code's save methods are called, they can then be written against CDocument*
interfaces, rather than a concrete class. This allows them to do exactly the same thing for every type of document ever - namely, call the serialize
method.
Upvotes: 1
Reputation: 5341
To access the document object from the WinApp derived class, you need to do like this:
((CFrameWnd*)AfxGetMainWnd( ))->GetActiveDocument();
You need not create new instance of the document every time. In any case, you cannot create an abstract document yourself.
Upvotes: 0