类链接器错误代码

Class Linker Error Code

本文关键字:错误代码 链接      更新时间:2023-10-16

我正在编写这个基本程序,但卡住了。 我是C++新手。我的程序必须计算各种违法行为的罚款。它将通过以下方式实现此目的:

  1. 用户将输入实际速度,速度限制,是否犯罪发生在工作区/住宅区//是否,以及法院费用

  2. 显示票证的总金额

  3. 提示用户是否要输入另一个工单或退出

以下是预期输出的示例:

超速
罚款:超过限速每英里 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 

这是怎么回事? 我完全被难住了。

当你像这样定义函数时:

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

我认为您需要删除以下中的"const":

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

class FineCalculator中,你声明了getFine方法const,但你定义了它而不定义const。要修复链接错误,您需要将声明和定义都设置为常量或非常量。

是的,需要删除常量。将 getfine 声明为常量的动机是什么,因为无论如何您都在修改对象?

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

此外,定义作用域时不会解析作用域

,请使用作用域解析运算符::指示它属于该类。