Friend error in C++ with Xcode

Post: #1
Ok I'm getting this compile error in Xcode with gcc 4.0
error: 'std::string CPG_ERROR_NS::CPG_Exceptions::m_What' is protected
cpg_exceptions-test.cpp:31: error: within this context

heres the relevant code:
class CPG_Exceptions
friend class CPG_Exceptions_Test;

static CPG_ErrorID m_ErrorID;

  explicit CPG_Exceptions();
  explicit CPG_Exceptions(CPG_SInt32 what_reason);
  explicit CPG_Exceptions(const CPG_String& what_reason);
  virtual ~CPG_Exceptions();
  CPG_SInt32 what();
  CPG_String swhat();

    CPG_Exceptions& ParseVariables(CPG_String inStr,...);

CPG_String m_What;
CPG_SInt32 m_NWhat;

virtual CPG_SInt32 GetErrorID() { return m_ErrorID.GetErrorID(); }
virtual CPG_String GetErrorString() { return m_ErrorID.GetErrorString(); }


    CPG_ERROR_NS::CPG_Exceptions exTest1;
    CPG_ERROR_NS::CPG_Exceptions exTest2(5);
    CPG_ERROR_NS::CPG_Exceptions exTest3(std::string("This is a test"));
    CPPUNIT_ASSERT("" == exTest1.m_What);//<-- this is the line that goes with the error.
    CPPUNIT_ASSERT_EQUAL(0, exTest1.m_NWhat);
    CPPUNIT_ASSERT("" == exTest2.m_What);
    CPPUNIT_ASSERT_EQUAL((CPG_SInt32)5, exTest2.m_NWhat);
    CPPUNIT_ASSERT("This is a test" == exTest3.m_What);
    CPPUNIT_ASSERT_EQUAL((CPG_SInt32)0, exTest3.m_NWhat);
    CPPUNIT_ASSERT("Generic Exception Was Thrown" == exTest1.m_ErrorID.GetErrorString());
    CPG_SInt32 errorID = CPG_ERROR_NS::CPG_ErrorIdTable::GetErrorID(CPG_String("CPG_Exceptions"));
    CPPUNIT_ASSERT_EQUAL(errorID, exTest1.m_ErrorID.GetErrorID());

I've been googleing around for answer and have seen some problems with friend and read through a thread here on Inheritance and Visibilty but have yet to find a solution to the problem.
I would expect that since the class CPG_Exceptions_Test is made a friend it should have access to the protected variable a least in VS it does but then again VS is really leniant at times.

The only reason I'm using friend is so that the test cases have access to protected and private variables to make sure things get ititialized right and to bypass functions before they have been tested. The friend declarations are only compiled in when unit testing.

Blah if i can beat this error, 90% of the current build errors will disappear.

Thanks in advance.
Quote this message in a reply
Posts: 834
Joined: 2002.09
Post: #2
That looks all right. The only thing I can think is missing is the actual definition of UNIT_TESTING__. Are you dead sure that that file includes a header that defines UNIT_TESTING__? You could assert it by adding:
#ifndef UNIT_TESTING__
#error Ka-boom!
to that header.

If it turns out to be undefined, I'd recommend adding UNIT_TESTING__=1 (without the spaces) to the Preprocessor Macros in Project Settings. Also, don't forget to define it for all the build configurations you are using.
Quote this message in a reply
Post: #3
Yep it's there I went ahead and tried your method to make sure it was defined and it is.

Maybe I'll go visit the gcc options for the target and see i can't find something.

One thing i had to do to get it to compile was add class to the friend declaration, yet another item VS is leniant on.
Quote this message in a reply
Posts: 834
Joined: 2002.09
Post: #4
Come to think of it, I think I've only ever seen friend declarations in the public block. Your declaration will be protected. Although it doesn't make sense to have different access modes for friends, it might be worth a try?
Quote this message in a reply
Post: #5
Tried that and it still gives the errors. Tried turning on the option of treating nonconformat code as a warning but it didn't help.

Wonder if it's some bug. Guess I should go install Xcode 2.2.1
Quote this message in a reply
Posts: 834
Joined: 2002.09
Post: #6
I'd like to take a look at this. Could you pack up your XCode project and mail it to me? ivan att rusted dott se.
Quote this message in a reply
Post: #7
I have figured out the cause and it's not obvious from the sample code because the namespaces are missing.

The problem is that the exception class is contained within CPG_ERROR_NS while the testing code is in the global namespace. The compiler is thinking that the friend class is in the same name space. If wrap a namespace around the testing class and forward declare the class with the same namespace and use the namespace in the friend declaration then it compiles no problem.

This begs the question which I should know but don't, how to specify a class is not in a namespace. i would assume it's ::<classname> but i think i've tried that before and it didn't work.

I only came to the realization when I was browsing some google groups posts and saw someone mention something about namespaces and then I realized this could be the problem which as it turns out is the problem.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  ChangeInputMode error in Xcode 3.1 programming in C Achithyn 1 4,183 May 18, 2009 06:30 PM
Last Post: Achithyn
  Xcode error? bronxbomber92 2 3,772 Nov 2, 2006 07:48 PM
Last Post: bronxbomber92