作者ComputerGod (電資雙雄)
看板C_and_CPP
標題[問題] 呼叫class函數與int
時間Thu Mar 5 15:46:21 2015
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
NO
問題(Question):
#include <cstdlib>
#include <iostream>
using namespace std;
int VDR = 3000;
void SetVR()
{
VDR =VDR -100;
APP->dec(5,4);
}
class APP
{
public:
void dec(int x, int y);
};
void dec(int x,int y)
{
int d = x-y;
cout << d << endl;
}
int main()
{
SetVR();
cout << VDR <<endl;
system("pause");
return 0;
}
預期的正確結果(Expected Output):
2900
1
錯誤結果(Wrong Output):
APP was not declared in this scope
不曉得如何處理?
我已經是使用public為何外部抓不到
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.117.164.19
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1425541584.A.C84.html
推 LPH66: 把 class APP 拉到 SetVR 上面 03/05 16:23
→ NilPtr: 樓上大大已解 補充下 函數上面補上 class APP; 也可以 03/05 21:31
→ NilPtr: 因為C++編譯器一定要先看到定義或宣告某類別存在 才准用 03/05 21:33
推 NilPtr: 不過還有2個問題 ... 等等補完 03/05 21:35
→ NilPtr: 二樓我說的是錯的 當我沒說過 XD 03/05 21:39
→ NilPtr: 剩下兩個問題 1.C++物件呼叫函數要有實例化物件才可用 03/05 21:40
→ NilPtr: 2.在Class外面實作Member一定要在函數名前頭加上Class名 03/05 21:41
→ NilPtr: void APP::dec(int x,int y) {...} 才對 03/05 21:42