我是C++新手,一直試圖讓我的頭在C++中的類。最近我嘗試了這個程序,而不是返回一個整數9,它返回一些垃圾值。是否有人可以幫助我走出C++的朋友函數不起作用
#include <iostream>
#include <cstring>
#include <math.h>
using namespace std;
class abc;
class xyz
{
int a;
public:
friend int add(xyz, abc);
friend void setval(xyz, int, abc, int);
};
class abc
{
int b;
public:
friend int add(xyz, abc);
friend void setval(xyz, int, abc, int);
};
int add(xyz V1, abc V2)
{ return (V1.a + V2.b);}
void setval(xyz v1, int v11, abc v2, int v22)
{v1.a = v11; v2.b = v22; }
int main()
{
xyz A;
abc B;
setval(A, 4, B, 5);
cout<<add(A, B)<<endl;
return(0);
}
有點痛苦的閱讀代碼..爲什麼類名是'xyz'..''abc' ... –
@AnnieKim我只是嘗試了我學習的概念,所以只是一個隨機代碼沒有具體原因。 –
@Sankalp謝謝你解決問題的答案,我會通過引用進一步探討傳遞值。 –