friend function problem ...
-
What is the wrong ? :confused: #include #include #include class MyMath { public: friend int Sum(int v1, int v2); }; int Sum(int v1, int v2) { return v1 + v2; } void main() { MyMath obj; cout << "Result: " << obj.Sum(1, 3); _getch(); } My month article: Game programming by DirectX by Lan Mader. Please visit in: www.geocities.com/hadi_rezaie/index.html Hadi Rezaie
-
What is the wrong ? :confused: #include #include #include class MyMath { public: friend int Sum(int v1, int v2); }; int Sum(int v1, int v2) { return v1 + v2; } void main() { MyMath obj; cout << "Result: " << obj.Sum(1, 3); _getch(); } My month article: Game programming by DirectX by Lan Mader. Please visit in: www.geocities.com/hadi_rezaie/index.html Hadi Rezaie
Your MyMath class's Sum function is public to other classes so there is not need to declare it as friend. And you can declare the method as friend if you are writing a class template(generating new classes depending upon what type of parameters you pass to the function). In your code I do not see a reason for declaring it as a friend or also it not possible to declare it as a friend. Einstein's Assistant