[問題] 呼叫class函數與int

看板C_and_CPP作者 (電資雙雄)時間9年前 (2015/03/05 15:46), 編輯推噓2(206)
留言8則, 2人參與, 最新討論串1/1
開發平台(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

03/05 16:23, , 1F
把 class APP 拉到 SetVR 上面
03/05 16:23, 1F

03/05 21:31, , 2F
樓上大大已解 補充下 函數上面補上 class APP; 也可以
03/05 21:31, 2F

03/05 21:33, , 3F
因為C++編譯器一定要先看到定義或宣告某類別存在 才准用
03/05 21:33, 3F

03/05 21:35, , 4F
不過還有2個問題 ... 等等補完
03/05 21:35, 4F

03/05 21:39, , 5F
二樓我說的是錯的 當我沒說過 XD
03/05 21:39, 5F

03/05 21:40, , 6F
剩下兩個問題 1.C++物件呼叫函數要有實例化物件才可用
03/05 21:40, 6F

03/05 21:41, , 7F
2.在Class外面實作Member一定要在函數名前頭加上Class名
03/05 21:41, 7F

03/05 21:42, , 8F
void APP::dec(int x,int y) {...} 才對
03/05 21:42, 8F
文章代碼(AID): #1K-0dGo4 (C_and_CPP)