zoukankan      html  css  js  c++  java
  • 一个关于C++ Inline关键字的引发的一个错误

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

    Linking
    cpp.obj : error LNK2001: unresolved external symbol 
    "public: int __thiscall aType::Descript(void)" (?Descript@aType@@QAEHXZ)
    Debug
    /cpp.exe : fatal error LNK1120: 1 unresolved externals

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

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

     1#include <iostream>
     2#include <aType.h>
     3using namespace std;
     4int 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:
     1#include "aType.h"
     2#include <iostream>
     3using namespace std;
     4aType::aType(int i,int j,int k)
     5{
     6    a[0]=i;
     7    a[1]=j;
     8    a[2]=k;
     9}

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

    17
    18aType 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
    27aType * aType::operator ->()
    28{
    29    return this;
    30}

    31aType 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}
     

    aType.h:
     1class aType
     2{
     3public:
     4    int a[3];
     5public :
     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}
    ;
    22inline int aType::Descript()
    23 {
    24         cout<<"test"<<endl;
    25    return 0;
    26 }

    27

    在这几个文件中,只要将
    inline int aType::Descript()
     
    {
             cout
    <<"test"<<endl;
        
    return 0;
     }
     放到aType.h中就可执行,放到aType.cpp中就不可以执行了但可以编译通过!.这个环境是VS6.0sp6
  • 相关阅读:
    android 仿微信聊天界面,以及语音录制功能
    程序员的鄙视链
    Spring 4 官方文档学习(十一)Web MVC 框架之URI Builder
    Spring 4 官方文档学习(十一)Web MVC 框架之Flash Attributes
    Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图
    JDK中的序列化和反序列化
    HTTP/1.1 学习
    HttpComponents 基础接口/类与HTTP message的对应关系
    Apache HttpComponents 学习
    HttpClient学习之 客户端HTTP编程入门
  • 原文地址:https://www.cnblogs.com/lee/p/358163.html
Copyright © 2011-2022 走看看