dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: compilation error C2248 using CStringArray

  1. #1
    Join Date
    Aug 2005
    Posts
    54

    compilation error C2248 using CStringArray

    I have 2 classes say A and B.

    A.h

    private:
    CStringArray m_cStringArrayFilePaths;

    public:
    CStringArray& GetStringArrayFilePaths();

    A.cpp

    CStringArray& A::GetStringArrayFilePaths()
    {
    return m_cStringArrayFilePaths;
    }

    B.h

    A m_cA;

    B.cpp

    B::MyFunc()
    {
    CStringArray& l_cStringArrayPaths = m_cA.GetStringArrayFilePaths();
    //Using variable l_cStringArrayPaths in this function.
    }

    On compiling file B.cpp i am getting the following error:

    error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
    c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afx.h(551) : see declaration of 'CObject::CObject'
    c:\program files\microsoft visual studio 9.0\vc\atlmfc\include\afx.h(522) : see declaration of 'CObject'
    This diagnostic occurred in the compiler generated function 'CStringArray::CStringArray(const CStringArray &)'

    Can anyone please help?

  2. #2
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: compilation error C2248 using CStringArray

    Are you sure you are not missing '&' in:
    Code:
    CStringArray& A::GetStringArrayFilePaths()
    From the errors shown, it seems you are copy-constructing the CStringArray; although it not in the source code you've shown.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  3. #3
    Join Date
    Aug 2005
    Posts
    54

    Re: compilation error C2248 using CStringArray

    yes. I am also confused with that. This error should have come if i would have returned CStringArray object by value and not by reference.

    anyways i have changed ny code to use std::vector<CString> instead of CStringArray, but still i would like to know what caused this error?

    thanks for help.

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: compilation error C2248 using CStringArray

    anyways i have changed ny code to use std::vector<CString> instead of CStringArray, but still i would like to know what caused this error?
    Posting the actual code would help us helping you!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: compilation error C2248 using CStringArray

    If the object containing the CStringArray is passed by value you will get this error since the generated copy constuctor in your class will try to invoke the copy constructor of CStringArray.

    Code:
    class X
    {
       CStringArray arr;
    };
    
    void foo( X x)   
    {
    }
    
    
    // ....
    X x;
    foo( x);   // this will make a call to X's copy constructor which in turn will make a call to CStringArray's copy constructor
    To work around this problem you need to define the copy constructor for your class;

    Code:
    class X
    {
       CStringArray arr;
    
    public:
         X() {}
    
         X( const X& other) 
         { 
              arr.Copy( other.arr);
         }
    };
    Har Har

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)