一个关于C++ Inline关键字的引发的一个错误

 

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

None.gif Linkingdot.gif
None.gifcpp.obj : error LNK2001: unresolved external symbol 
" public: int __thiscall aType::Descript(void) "  ( ? Descript@aType@@QAEHXZ)
None.gifDebug
/ cpp.exe : fatal error LNK1120:  1  unresolved externals

 这看了好长时间没有明白怎么回事,无意中.cpp中的内联函数的定义放到.h中,再编绎执行竟然成功了.到现在还没有弄清楚为什么在连接时没有找到这个内联函数,只是知道这样用内联函数.现在将程序给贴出来,大家看一下为什么要放在.h文件中.

我的程序分为三部分:主程序(cpp.cpp),类定义文件(aType.cpp),类头文件(aType.h)
cpp.cpp:

 1 None.gif #include  < iostream >
 2 None.gif#include  < aType.h >
 3 None.gif using   namespace  std;
 4 None.gif int  main( int  argc,  char   *  argv[])
 5 ExpandedBlockStart.gifContractedBlock.gif {
 6InBlock.gif    aType b(1,2,3);    
 7InBlock.gif    cout<<b.Descript();    //测试内联函数
 8InBlock.gif    cout<<b[1]<<endl;  //测试重载[]运算符
 9InBlock.gif    try
10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
11InBlock.gif        b[4]=13;
12ExpandedSubBlockEnd.gif    }

13InBlock.gif    catch(char * a)
14ExpandedSubBlockStart.gifContractedSubBlock.gif    {
15InBlock.gif         cout<<a<<endl;
16ExpandedSubBlockEnd.gif    }

17InBlock.gif    d=b+c;     //测试重载+运算符
18InBlock.gif    for(int i=0;i<3;i++)
19InBlock.gif       cout<<d[i]<<endl;
20InBlock.gif        cout<<d->a[1]<<endl;
21InBlock.gif    d=b,c;  //测试重载,运算符
22InBlock.gif    cout<<d->a[1]<<endl;
23InBlock.gif    return 0;
24ExpandedBlockEnd.gif}

25 None.gif
26 None.gif
aType.cpp:
 1 None.gif #include  " aType.h "
 2 None.gif#include  < iostream >
 3 None.gif using   namespace  std;
 4 None.gifaType::aType( int  i, int  j, int  k)
 5 ExpandedBlockStart.gifContractedBlock.gif {
 6InBlock.gif    a[0]=i;
 7InBlock.gif    a[1]=j;
 8InBlock.gif    a[2]=k;
 9ExpandedBlockEnd.gif}

10 None.gif int   & aType:: operator []( int  i)
11 ExpandedBlockStart.gifContractedBlock.gif {
12InBlock.gif    if(i<0||i>2)
13InBlock.gif       throw " Boundary error";
14InBlock.gif    cout<<"overload []";
15InBlock.gif    return a[i];
16ExpandedBlockEnd.gif}

17 None.gif
18 None.gifaType aType:: operator   + (aType oA)
19 ExpandedBlockStart.gifContractedBlock.gif {
20InBlock.gif    aType temp;
21InBlock.gif    temp.a[0]=a[0]+oA.a[0];
22InBlock.gif    temp.a[1]=a[1]+oA.a[1];
23InBlock.gif    temp.a[2]=a[2]+oA.a[2];
24InBlock.gif    return temp;
25ExpandedBlockEnd.gif}

26 None.gif
27 None.gifaType  *  aType:: operator   -> ()
28 ExpandedBlockStart.gifContractedBlock.gif {
29InBlock.gif    return this;
30ExpandedBlockEnd.gif}

31 None.gifaType aType:: operator  ,(aType oA)
32 ExpandedBlockStart.gifContractedBlock.gif {
33InBlock.gif    return oA;
34ExpandedBlockEnd.gif}

35 None.gif  int  aType::myMax()
36 ExpandedBlockStart.gifContractedBlock.gif {
37InBlock.gif    int temp,i;
38InBlock.gif    temp=a[0];
39InBlock.gif    for(i=0;i<3;i++)
40InBlock.gif        if(temp<a[i])
41InBlock.gif            temp=a[i];
42InBlock.gif    return temp;
43ExpandedBlockEnd.gif}
 

aType.h:
 1 None.gif class  aType
 2 ExpandedBlockStart.gifContractedBlock.gif {
 3InBlock.gifpublic:
 4InBlock.gif    int a[3];
 5InBlock.gifpublic :
 6InBlock.gif    aType(int i,int j,int k);
 7InBlock.gif    ~aType()
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 9InBlock.gif        a[0]=0;
10InBlock.gif        a[1]=0;
11InBlock.gif        a[2]=0;
12ExpandedSubBlockEnd.gif    }

13InBlock.gif    aType()
14ExpandedSubBlockStart.gifContractedSubBlock.gif    {}
15InBlock.gif    int &operator[](int i);
16InBlock.gif    aType operator+(aType oA);
17InBlock.gif    aType *operator ->();
18InBlock.gif    aType operator ,(aType oA);
19InBlock.gif    int myMax();
20InBlock.gif    int Descript();
21ExpandedBlockEnd.gif}
;
22 None.gifinline  int  aType::Descript()
23 ExpandedBlockStart.gifContractedBlock.gif  {
24InBlock.gif         cout<<"test"<<endl;
25InBlock.gif    return 0;
26ExpandedBlockEnd.gif }

27 None.gif

在这几个文件中,只要将
None.gif inline  int  aType::Descript()
ExpandedBlockStart.gifContractedBlock.gif 
{
InBlock.gif         cout
<<"test"<<endl;
InBlock.gif    
return 0;
ExpandedBlockEnd.gif }
 放到aType.h中就可执行,放到aType.cpp中就不可以执行了但可以编译通过!.这个环境是VS6.0sp6

转载于:https://www.cnblogs.com/lee/archive/2006/03/24/358163.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值