类链接器错误代码
Class Linker Error Code
我正在编写这个基本程序,但卡住了。 我是C++新手。我的程序必须计算各种违法行为的罚款。它将通过以下方式实现此目的:
-
用户将输入实际速度,速度限制,是否犯罪发生在工作区/住宅区//是否,以及法院费用
-
显示票证的总金额
-
提示用户是否要输入另一个工单或退出
以下是预期输出的示例:
超速
罚款:超过限速每英里 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
。
因此,不仅在声明函数时,它应该是限定的,而且在定义它时也是如此。
,请使用作用域解析运算符::
指示它属于该类。
- JNI 不满意链接错误: 动态链接库 (DLL) 初始化例程失败
- CMake / 错误时链接 libfreeimage / libtiff.
- Cmake 3.8.0在makefiles中生成错误的链接命令
- 在将C全局变量与C 代码链接起来的麻烦
- 链接器错误与链接器无关
- OpenCV2 代码链接错误
- 从 C 代码链接错误调用C++函数(使用 gcc 进行链接)
- DCC致命错误F2588链接器错误代码:1($ 00000001)
- cusparse功能的多个定义错误在链接可执行文件中的cuda文件时
- C 错误:0x00934ABB(链接列表,地址簿)的未手动异常
- 错误:链接静态库时出现";"
- 将 Rcpp 与 C 代码链接,以实现自适应城域拒绝采样
- 链接器错误:静态链接提升序列化库
- 链接器错误尝试链接libFlurryAds时出现重复符号
- 分析共享库是否存在重复代码链接
- 将错误从链接时移动到编译时
- 将静态 C 库与C++代码链接时出现"undefined reference to"错误
- visual Can用VC10(sp1)编译的C++库可以通过用VC11编译的代码链接
- 在windows上链接Python到我的c++代码-链接错误
- C++代码链接中的错误:警告 C4190:类型指定了 C 链接,但返回与 C 不兼容的 UDT