-2

I am using MFC Visual Studio 8 Version 9. I want to create an array or vector of CPngImage objects. If i declare the array to be global, its fine. If i try to add the array to one of my own classes in get the following error:

afxwin.h(312) : error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'

Here is an example of what i am trying to do:

#pragma once
#include <list>
#include <vector>
#include <iterator>
#include <afxtoolbarimages.h>
#include <afxwin.h>

using namespace std;
#define MAX_IMAGES 30

class PayAppReport
{
private:

CString m_projectName;
CString m_address;
CString m_city;
CString m_county;
CString m_GCcontact;
CString m_weather;
vector<CString> m_contractorsOnSite;
vector<CString> m_workInPlace;
vector<CString> m_workProjected;
vector<CString> m_issues;
vector<CString> m_report;

CPngImage pngImage[MAX_IMAGES];

float m_amount;

int m_payapp,zipCode,currentLine;

COleDateTime m_start,m_stop,m_observation;

bool m_OnSchedule;

CRect printRect;

public:
    vector<CString> m_pictures;
    PayAppReport(void);
    ~PayAppReport(void);
    void Clear();
    void SetProjectName(CString name);
    CString GetProjectName();
    void SetAddress(CString name);
    CString GetAddress();
    void SetCity(CString name);
    CString GetCity();
    void SetCounty(CString name);
    void SetCounty(int zipCode);
    CString GetCounty();
    void SetGCcontact(CString name);
    CString GetGCcontact();
    void SetAmount(float dollars);
    float GetAmountFloat();
    CString GetAmountString();
    void SetPayAppNumber(int num);
    int GetPayAppNumber();
    void SetZipCode(int code);
    int GetZipCode();
    void SetOnSchedule(bool ans);
    bool OnSchedule();
    void SetStartDate(COleDateTime date);
    void SetCompletionDate(COleDateTime date);
    void SetObservationDate(COleDateTime date);
    COleDateTime GetStartDate();
    COleDateTime GetCompletionDate();
    COleDateTime GetObservationDate();
    void AddContractorsOnSite(CString name);
    void AddWorkInPlace(CString name);
    void AddWorkProjected(CString name);
    void AddIssues(CString name);
    void AddPictures(CString name);
    int GetNumberOfContractorsOnSite();
    int GetNumberOfWorkInPlace();
    int GetNumberOfWorkProjected();
    int GetNumberOfIssues();
    int GetNumberOfPictures();
    CString GetContractorsOnSite(int num);
    CString GetContractorsOnSite();
    CString GetWorkInPlace(int num);
    CString GetWorkProjected(int num);
    CString GetIssues(int num);
    CString GetWorkInPlace();
    CString GetWorkProjected();
    CString GetIssues();
    CString GetPictures(int num);
    void SetWeather(CString name);
    CString GetWeather();
    CString PutList(vector<CString> theList);
    void InitializeContractorsOnSite();
    void InitializeWorkInPlace();
    void InitializeWorkProjected();
    void InitializeIssues();
    void InitializePictures();
    void PutLine(CString rightText, CString leftText, CDC *pDC, HDC hDC, bool indent, int height);
    CString toCurrency(float val);
};

Can someone explain why i cannot do this?

  • There is no CObject in your code. –  Nov 03 '17 at 14:20
  • 2
    Cannot reproduce, please make a [MCVE]. I just added `CPngImage pngImage[3];` to one of my classes and it compiles fine – Jabberwocky Nov 03 '17 at 14:24
  • Show at least the code fragment where you use `pngImage`. – Jabberwocky Nov 03 '17 at 14:26
  • @manni66 `CObject` is the grandparent of `CPngImage`. `afxwin.h` is a Microsoft include file. – Mark Ransom Nov 03 '17 at 14:33
  • 1
    Adding the prototypes and variables of your class isn't helping, they're irrelevant to the problem and just add unnecessary clutter. If there's more to the error message, *that* would help. – Mark Ransom Nov 03 '17 at 15:39
  • I included an entire listing of the header file. I get the same error when i try to add the other classes. I did try what you suggested and it also compiled fine. What possibly could be causing this error in my application? – BigEFrom84 Nov 03 '17 at 15:42
  • I couldn't compile this code in VS 2008, my installation doesn't have `` I don't remember if that has something to do with feature pack or if this header is jammed in to MFC. – Barmak Shemirani Nov 04 '17 at 06:03

1 Answers1

0

CObject is not designed with value semantics. Thus the copy constructor of CObject is marked as private. Thus declaring an array of CObject -- or an array of something derived from CObject like CPngImage -- is not the way to go.

Change your array to hold pointers, not whole objects (i.e. CPngImage* instead of CPngImage). See if that helps.

(Did you even take a look at the declaration of the class before asking this question?)

Joe
  • 5,394
  • 3
  • 23
  • 54
  • yes i did look at the declaration before asking, just didn't understand it. – BigEFrom84 Nov 04 '17 at 11:11
  • Yeah, sorry that was probably a little bit snarky of me to say that. Basically the reason that the constructor and assignment operator are private is to specifically prevent you from using the class in such a way that would cause you to possibly assign one instance to another. It's not made for that. This is a common C++ technique. It's the sort of thing that will become plain as you learn more. In short, anything derived from CObject should be accessed and stored by *pointer*, not by *value*. It's just the way it was designed. Do that and I think you'll have a lot more success. – Joe Nov 04 '17 at 19:21