简单C++中的错误,但无法找出原因

An error in simple C++, but can't figure out why

本文关键字:C++ 错误 简单      更新时间:2023-10-16

我有这样的代码1> test.obj:错误LNK2019:"void __cdecl iceCreamDivision(int,double)"(?iceCreamDivision@@YAXHN@Z)외부 기호(참조 위치: _主要的함수)에서 확인하지 못했습니다.1> D:\download\C161\Debug\C161.exe:致命错误LNK1120:1개의 확인할 수 없는 외부 참조입니다.很抱歉,这是韩语,但它说有一个外部符号,错误在第一个void方法上。这段代码直接来自教科书,我想知道为什么它不起作用。

#include <iostream>
using namespace std;
void iceCreamDivision(int number, double totalWeight);
int main()
{
    int number;
    double totalWeight;
    cout << "Enter the number of customers: ";
    cin >> number;
    cout << "Enter weight of ice cream to divide (in ounces): ";
    cin >> totalWeight;
    iceCreamDivision(number, totalWeight);
    return 0;
}
void iceCreamDivison(int number, double totalWeight)
{
    double portion;
    if (number == 0)
    {
        cout << "Cannot divide among zero customers.n";
        return;
    }
    portion = totalWeight/number;
    cout << "Each one receives "
        << portion << " ounces of ice cream." << endl;
}

我认为您的代码中存在拼写错误

你申报了

void iceCreamDivision(int number, double totalWeight);

但是你定义了

void iceCreamDivison(int number, double totalWeight)

请注意DivisionDivison之间的差异。请参阅学习C++也可以帮助您学习英语。

您声明了iceCreamDivision,但使用拼写错误的名称iceCreamDivison定义了一个不同的函数。将缺少的i添加到定义中。