最近在学习C++,看到内联函数,就上机编了一个小程序,我本来以为可简单的语法,可是竟然在我编绎成功后,执行的时候出现了下面的错误:




这看了好长时间没有明白怎么回事,无意中.cpp中的内联函数的定义放到.h中,再编绎执行竟然成功了.到现在还没有弄清楚为什么在连接时没有找到这个内联函数,只是知道这样用内联函数.现在将程序给贴出来,大家看一下为什么要放在.h文件中.
我的程序分为三部分:主程序(cpp.cpp),类定义文件(aType.cpp),类头文件(aType.h)
cpp.cpp:
1
#include <iostream>
2
#include <aType.h>
3
using namespace std;
4
int main(int argc, char * argv[])
5
{
6
aType b(1,2,3);
7
cout<<b.Descript(); //测试内联函数
8
cout<<b[1]<<endl; //测试重载[]运算符
9
try
10
{
11
b[4]=13;
12
}
13
catch(char * a)
14
{
15
cout<<a<<endl;
16
}
17
d=b+c; //测试重载+运算符
18
for(int i=0;i<3;i++)
19
cout<<d[i]<<endl;
20
cout<<d->a[1]<<endl;
21
d=b,c; //测试重载,运算符
22
cout<<d->a[1]<<endl;
23
return 0;
24
}
25
26
aType.cpp:
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

1
#include "aType.h"
2
#include <iostream>
3
using namespace std;
4
aType::aType(int i,int j,int k)
5
{
6
a[0]=i;
7
a[1]=j;
8
a[2]=k;
9
}
10
int &aType::operator[](int i)
11
{
12
if(i<0||i>2)
13
throw " Boundary error";
14
cout<<"overload []";
15
return a[i];
16
}
17
18
aType aType::operator +(aType oA)
19
{
20
aType temp;
21
temp.a[0]=a[0]+oA.a[0];
22
temp.a[1]=a[1]+oA.a[1];
23
temp.a[2]=a[2]+oA.a[2];
24
return temp;
25
}
26
27
aType * aType::operator ->()
28
{
29
return this;
30
}
31
aType aType::operator ,(aType oA)
32
{
33
return oA;
34
}
35
int aType::myMax()
36
{
37
int temp,i;
38
temp=a[0];
39
for(i=0;i<3;i++)
40
if(temp<a[i])
41
temp=a[i];
42
return temp;
43
}

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

aType.h:
1
class aType
2
{
3
public:
4
int a[3];
5
public :
6
aType(int i,int j,int k);
7
~aType()
8
{
9
a[0]=0;
10
a[1]=0;
11
a[2]=0;
12
}
13
aType()
14
{}
15
int &operator[](int i);
16
aType operator+(aType oA);
17
aType *operator ->();
18
aType operator ,(aType oA);
19
int myMax();
20
int Descript();
21
};
22
inline int aType::Descript()
23
{
24
cout<<"test"<<endl;
25
return 0;
26
}
27

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

在这几个文件中,只要将




