Synopsis

先来看两个错误

  1. 报错

    'insertList' was not declared in this scope

  2. 修改没有加引用型&的参数

Detail

  1. 第一个错误,显然是这个函数没有被定义。但我已经创建了呀!!这又是为什么呢?经过回忆,是因为在函数A调用函数B的时候,把函数A放在函数B的前面。又因为,程序是从上到下执行的,所以才会报错成未定义。

  2. 这个在修改参数中,经常用到。而且这是一个很常见又很严重的错误。因为参数的传递的将参数值复制过去另外一个块中。一开始学c的时候,用到了地址的传递(也就是传指针)。现在c++中修改参数,就不必再传指针了,不过要记得要改成引用型啊。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
using namespace std;
#define MAXSIZE 100
typedef int ElemType;
typedef struct SqList
{ ElemType data[MAXSIZE];
int length;
}SqList;

int insertList(SqList *sq,int i,ElemType x)
{ int j;
if(i<0||i>sq-> length -1)
{ printf("\n The value of %d is wrong!\n", i);
return 0;
}
if(sq-> length >= MAXSIZE)
{ printf("\n overflow!");
return 0;
}
for(j=sq->length-1;j>=i;j--)
sq->data[j+1]=sq->data[j];
sq->data[i]=x;
sq->length++;
return 1;
}

int deleteList(SqList *sq,int i)
{ if(i<0 || i>sq-> length)
{ printf("\n the position is wrong!\n");
return 0;
}
for(i;i<sq->length;i++)
sq->data[i-1]=sq->data[i];
sq->length--;
return 1;
}

void createList(SqList &L)
{
ElemType e;
int n;
cout << "Please enter the length of the list: ";
cin >> n;
cout << "Enter " << n << " numbers: ";
for(int i=0; i < n; i++){
cin>>e;
L.data[i]=e;
}
L.length=n;
}

void insert(SqList &L)
{
int i, j;
cout << "Please enter the index that needs to insert(start from 0): ";
cin >> i;
cout << "Please enter the data that needs to insert: ";
cin >> j;
insertList(&L, i, j);
}

void delet(SqList& L)
{
int i;
cout << "Please enter the index that needs to delet(start from 0): ";
cin >> i;
deleteList(&L, i);
}

void print(SqList &L)
{
int i;
cout << "list: ";
for(i=0; i< L.length;i++)
{
cout << L.data[i] << " ";
}
cout << endl;
}


int main()
{
SqList L;
ElemType i,j;

createList(L);
print(L);

insert(L);
print(L);

delet(L);
print(L);

system("pause>0");
}