作者c2147369 (stitch)
看板C_and_CPP
标题[问题] 出现MAX was not declared in the scope
时间Tue Jul 12 13:34:39 2016
求解在编译的最後跑出来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://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1468301681.A.456.html
※ 编辑: c2147369 (120.117.156.8), 07/12/2016 13:42:16
1F:→ wawi2: 大哥 你的MAX放在main.cpp
07/12
W大,我是女生。我不懂你的意思QAQ
※ 编辑: c2147369 (120.117.156.8), 07/12/2016 14:15:19
2F:→ bibo9901: c++的全域 const 只有所在的.cpp才看得到 07/12 14:59
3F:→ Caesar08: 假小妹? 07/12 15:04
4F:→ Caesar08: 如果vector.h有extern的话就看的到 07/12 15:06
5F:→ ilms49898723: 现在好流行自称小妹或说是女的,有buff? 07/12 15:24
6F:推 TobyH4cker: 小妹我也不是故意的啦 07/14 12:06