求解在編譯的最後跑出來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
求解謝謝大家~~