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

Threaded View

  1. #13
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: how should I arrange classes in files?

    I do use #pragma once at the beginning of every header file.

    I've changed my code in a few places. Now I've gotten to a situation like this:

    file1.h:
    Code:
    namespace one
    {
    	class class1
    	{
    		int x;
    
    		friend class class2;
    	};
    }
    file2:
    Code:
    #include "file1.h"
    
    class class2
    {
    public:
    	int y;
    	class class2()
    	{
    		one::class1 obj;
    		obj.x = 3;
    	}
    };
    main.cpp:
    Code:
    #include "file2.h"
    ....
    //in the main function:
    class2 obj;
    And I get these errors in file2:
    Code:
    error C2248: 'one::class1::x' : cannot access private member declared in class 'one::class1'
    IntelliSense: member "one::class1::x" (declared at line 6 of "d:\my projects\testslearn\testslearn\file1.h") is inaccessible
    Why does the friendship not work? well... I assume it expects the class to belong in the same namespace. But, isn't there a way to do it without putting class2 in a namespace?

    it seems that friend class ::class2; does not work, even if class2 is global.
    It says:
    Code:
    error C2039: 'class2' : is not a member of '`global namespace''
    error C2248: 'one::class1::x' : cannot access private member declared in class 'one::class1'
    Last edited by Feoggou; March 7th, 2011 at 02:53 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured