看板 C_and_CPP 關於我們 聯絡資訊
求解在編譯的最後跑出來MAX was not declared in the scope。 main.cpp #include <iostream> #include <iomanip> #include "vector.h" using namespace std; const int MAX = 100; int main() { int a1[] = {0,1,2,3,4,5,6,7,8,9}; int s1=sizeof(a1)/sizeof(int); vector v1(a1,s1); v1.dump(); vector v2(v1); v2.set(13,88); v2.dump(); vector v3(v1); v3.resize(6); v3.dump(); vector v4(v1); v4.resize(15); v4.dump(); system("pause"); return 0; } vector.h class vector { private: int dat[MAX]; int size; public: vector(); vector(int d[], int s); vector(const vector & v) ; void set(int index, int v) ; int get(int index) ; void resize(int s) ; void dump() ; }; vector.cpp #include <iostream> #include <iomanip> #include "vector.h" using namespace std; vector::vector() { size=0; } vector::vector(int d[], int s) { size=(s<=MAX)?s:MAX; for(int i=0; i<size; i++) dat[i]=d[i]; } vector::vector(const vector & v) { size = v.size; for(int i=0; i<size; i++) dat[i]=v.dat[i]; } void vector::set(int index, int v) { if (index<size) dat[index]=v; else if (index<MAX) { while (size<index) dat[size++]=0; dat[size++]=v; } else cout<<"ERROR: index is out of range!"<<endl; } int vector::get(int index) { if (index<size) return dat[index]; else cout<<"ERROR: index is out of range!"<<endl; return 0; } void vector::resize(int s) { if (s<size) size=s; else while (size<=s) dat[size++]=0; } void vector::dump() { for(int i=0; i<size; i++) cout<<setw(5)<<dat[i]; cout<<endl; } http://i.imgur.com/6pyUaqM.jpg 求解謝謝大家~~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.216.1.228 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1468301681.A.456.html ※ 編輯: c2147369 (120.117.156.8), 07/12/2016 13:42:16
wawi2: 大哥 你的MAX放在main.cpp
07/12 W大,我是女生。我不懂你的意思QAQ ※ 編輯: c2147369 (120.117.156.8), 07/12/2016 14:15:19
bibo9901: c++的全域 const 只有所在的.cpp才看得到 07/12 14:59
Caesar08: 假小妹? 07/12 15:04
Caesar08: 如果vector.h有extern的話就看的到 07/12 15:06
ilms49898723: 現在好流行自稱小妹或說是女的,有buff? 07/12 15:24
TobyH4cker: 小妹我也不是故意的啦 07/14 12:06