Want a friend function in the namespace, but not the whole class

this is the next question to a friend function in the namespace

If I want testFunc to be in the TestNamespace namespace, but I don't want TestClass to also be in TestNamespace, how can I declare them?

1.This does not work ("expected unqualified identifier before namespace" on line 2)

class TestClass {
namespace TestNamespace {
    friend void testFunc(TestClass &myObj);
};
}

namespace TestNamespace {
    void testFunc(TestClass &myObj);
}

2. But he complains that "TestNamespace :: testFunc (...) should be declared inside" TestNamespace "when I do this -

class TestClass {
friend void TestNamespace::testFunc(TestClass &myObj);

}

namespace TestNamespace {
    void testFunc(TestClass &myObj);
}

3. The above code is fine if I don't have โ€œTestNamespace ::โ€ in the friend function declaration in line 2, but I'm not sure if he knows that TestNamespace :: testFunc is a friend.

, . . !

+3
2

:

class TestClass;

namespace TestNamespace {
    void testFunc(TestClass &myObj);
}

class TestClass {
    friend void TestNamespace::testFunc(TestClass &myObj);
};

, "leda" , , , , , .

+2

:

class TestClass; // forward declaration

namespace TestNamespace {  // namespace body
    void testFunc(TestClass &myObj);  // passing by reference, ok!
}

class TestClass {
    friend void TestNamespace::testFunc(TestClass &myObj); // scoping with namespace
};

, ( ).

+1

All Articles