Search code examples
c++visual-studio-2017mfc

Can't use MFC CObject class in VS 2017 application


I need to use MFC Serialization mechanism to serialize objects of class Product:

class Product : public CObject
{
protected:
    string name; 
    int expiring;
    double price;
public:
    Product();
    ~Product();

    virtual void input_data();
    virtual void print_data();
};

This is simple Windows Console Application. I got an error on CObject: not a class or struct name.

I tried to make MFC Console Application following the instruction in this comment: https://stackoverflow.com/a/50320168/6543699. Now I got a lot of errors (identifier not found or identifier not declared). The text of errors is in Russian, so I don't copy them here. This is how it looks: Errors

I don't know anything about MFC using and can't find guide where it described clearly. My questions are:

1) Is it possible to use CObject in console application (non-MFC) and how?

2) If not, what should I do to be able to use MFC serialazation? Maybe include some headers or some components were just missing while installation?


Solution

  • You can just adjust a console app in a couple of steps to use MFC. First is to include afx.h, like:

    #include <iostream>
    #include <afx.h>
    

    Then you will want to link with the MFC dynamic libraries.

    Project Properties > Configuration Properties > Advanced > Use MFC

    Select: Use MFC in a Shared DLL

    It should now compile with CObject.

    My note, I would not use MFC serialization, at the least use Boost Serialization I gave up using any serialization a long time ago because of the constant need to maintain versioning. I found it a night mare. Unless you see that your object structure will remain fairly static, I would recommend using XML to database your objects. It is a little more work to get going but way more often than not, you don't need to worry about versioning as you make changes.