Tuesday, May 17, 2005, 1:39:34 PM, you wrote:
Hi. You've forgot a semicolon after last closing bracket of namespace
declaration.
FH> Dear C++ users,
FH> I have problem with friend functions and namespaces.
FH> Assume we have class "MyClass" defined in namespace "MyNameSpace":
FH> namespace MyNameSpace
FH> {
FH> class MyClass
FH> {
FH> private:
FH> int data;
FH> public:
FH> int func(){}
FH> friend int main();
FH> };
FH>
}; // ';' should be placed here to define that class MyClass belongs
// to namespace MyNameSpace
In the implementation file of MyClass's members you shouldn't place a
semicolon. e.g.
namespace MyNameSpace {
int MyClass::func()
{
//...
}
}
If you whant to declare some friend function in the class declaration
then you should place function prototype before the class declaration. e.g.
int main();
namespace MyNameSpace
{
class MyClass
{
friend int main();
...
And at last... what's for do you need an access for private members
in main() function? It's hard to imagine a real reason for doing this.
FH> using namespace name;
FH> int main()
FH> {
FH> C obj;
FH> obj.data;
FH> return 0;
FH> }
FH> I want to allow "main()" to access private members so I made it friend
FH> for the class. Since "main()" does not belong to "MyNameSpace" the
FH> compiler can not make it a friend for the class. It trys to find a
FH> "main()" function which belongs to the namespace not a "Main()" function
FH> outside.
FH> Now I want to k now if there is solution for this problem.
FH> Thanks,
--
Best regards,
Denis
|