类链接器错误代码

时间:2013-09-08 11:31:20

标签: c++ linker

我正在写这个基本程序而且卡住了。我是C ++的新手。我的计划必须 针对各种违法行为罚款。 它将通过以下方式实现这一目标:

  1. 用户将输入实际速度,速度限制 在工作区/居住区/是否发生违法行为,以及法庭费用

  2. 显示故障单的总金额

  3. 提示用户是否要输入其他故障单或退出

  4. 这是预期输出的一个例子:

      

    超速
      罚款:超过限速加上每英里5美元加上法庭费用

         

    超速公路工作区
      罚款:超过限速每英里6美元加上法庭费用

         

    在住宅区加速
      罚款:超过限速每英里7美元加上200美元加上法庭费用

    以下是代码:

    #include <iostream>
    
    class FineCalculator
    {
    public:
        ~FineCalculator() {}
        FineCalculator(int courtFees); 
        int getFine(int zone, int speedLimit, int actualSpeed) const;
    
    private:
      int courtFees;
      int balance;
    };
    
    
    FineCalculator::FineCalculator(int courtFees)
    {
        //return courtFees;
    }
    
    int getFine(int zone, int speedLimit, int actualSpeed) 
    {
        //define IF you are speeding or not
    
        if (actualSpeed > speedLimit)
        {
    
            /* define speeding zones
               1. Regular
               2. Highway
               3. Residential
             */
    
            if (zone==1)
            {
                int balanceCounter=actualSpeed-speedLimit;
    
            //balance=courtfees+(loopcounter*5)
            //balance
            }
    
        }
    
        return 0;
    }
    
    int main()
    {
        int courtFee=0;
        int inputFee=0;
        int accumulator=0;
        int programLooper=1; 
        int speedLimitz=0;
        int vechicleSpeed=0;
    
        std::cout<<"Please enter the court fee $";
        std::cin >>courtFee;
    
        FineCalculator fine1(courtFee);
    
        while (programLooper !=0)
        {
            //1 for regular, 2 for highway, 3 for residential
            //loop selection of offenses
            std::cout<<"Please make numerical  ticket selection for where the offense occured: \n";
            std::cout<<"1. Regular \n";
            std::cout<<"2. Highway \n";
            std::cout<<"3. Residential \n";
            std::cin >>programLooper;
    
            std::cout<<"\n \n \n";
            std::cout<<"Please Enter the speed limit \n";
            std::cin >>speedLimitz;
    
            std::cout<<"\n \n \n";
            std::cout<<"Please Enter the vechile speed  \n";
            std::cin >>vechicleSpeed;
    
            fine1.getFine(programLooper,speedLimitz,vechicleSpeed);
        }
    
        if (programLooper==0)
        {
            //end program loop
            return (0);
        }
    }
    

    对于FineCalculator::FineCalculator(int courtFees)方法的初学者,我不知道在返回时输入什么,因为返回后我输入的任何内容都会出错。

    同样在fine1.getFine(programLooper,speedLimitz,vechicleSpeed);我收到错误说:

    Error   2   error LNK1120: 1 unresolved externals   
    
    Error   1   error LNK2019: unresolved external symbol "public: int __thiscall FineCalculator::getFine(int,int,int)const " (?getFine@FineCalculator@@QBEHHHH@Z) referenced in function _main 
    

    这里发生了什么?我完全被难过了。

5 个答案:

答案 0 :(得分:0)

我认为您需要删除“const”:

int getFine(int zone, int speedLimit, int actualSpeed) const;

答案 1 :(得分:0)

class FineCalculator中,您声明了getFine方法const,但是您在没有const的情况下定义了它。要修复链接错误,您需要将声明和定义都设置为const或非const。

答案 2 :(得分:0)

当你定义这样的函数时:

int getFine(int zone, int speedLimit, int actualSpeed)
{ ... }

你定义的是一个普通的函数,而不是你的类的一部分 - 这是合法的,但不是你想要的。

要定义您在班级中声明的getFine,您需要:

int FineCalculator::getFine(int zone, int speedLimit, int actualSpeed) const
{ ... }

FineCalculator::是关键。你还需要最终的const来匹配你的声明方式。)

此外,对于您的第一个问题,您无法从FineCalculator::FineCalculator返回任何内容。但是你不需要 - 它的工作是构造一个FineCalculator对象然后你可以使用它,例如通过调用它上面的getFine

答案 3 :(得分:0)

编译器和链接器都需要知道成员函数是否为const 因此,不仅在声明函数时它应该是合格的,而且在定义它时。

此外,定义范围时未解析范围,请使用范围解析运算符::来表明它属于该类。

答案 4 :(得分:0)

是的,需要删除const。 将getfine声明为const的动机是什么,因为你无论如何修改对象?

相关问题