C ++:数组作为函数形式参数获取错误

时间:2016-12-04 03:14:39

标签: c++ arrays visual-studio function user-defined-functions

我在大学读计算机科学课。我正在完成一项即将到期的作业,但我遇到了一个问题而且我已经厌倦了等待我的老师回复我的电子邮件。我们使用Visual Studio 2015学习使用C ++编写代码,并通过用户定义的函数,数组和结构。我们没有对课程或任何事情做过任何事情,我在错误中发现的所有信息似乎都涉及到这些,因此对我没有任何帮助。

每当我运行程序时,我都会遇到以下错误:

“LNK2019未解析的外部符号”void __cdecl takeOrder(int,int,struct menuItemType * const)“(?takeOrder @@ YAXHHQAUmenuItemType @@@ Z)在函数_main中引用

LNK1120 1未解决的外部“

这两个错误都被称为“第1行”。

Visual Studio网站上的文档有点夸张,而且我不了解那里的所有信息,但我认为它与我的变量声明有关。这是唯一有意义的事情。

我对数组没有很好的理解,但我认为我正确使用它们。我有双倍,三倍,四倍检查教科书和我的笔记和我的拼写,我不知道我错过了什么。下面是完整的代码(保存一些让它更长的东西)。

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstring>

using namespace std;

struct menuItemType
{
    string itemName;
    double itemPrice;
};

void read(menuItemType menuList[], int&);
void showMenu(menuItemType menuList[], int&);
void takeOrder(int, int, menuItemType menulist[]);

int main()
{
    int counter = 0;
    menuItemType menuList[10];
    int order[10] = { 0,0,0,0,0,0,0,0,0,0 };
    double totalBill = 0;
    double tax = 0;

    cout << fixed << setprecision(2) << showpoint;
    cout << "Welcome to Hold-ye-Overs, a delightful place to eat with food\nmade from one hundred percent actual food!\n";

    read(menuList, counter);
    showMenu(menuList, counter);
    takeOrder(order[10], counter, menuList);

    return 0;
}

void takeOrder(int order[], int counter, menuItemType menuList[])
{
    int amount_of_item;
    int choice;
    bool flag = true;
    char orderMore;

    while (flag == true)
    {
        cout << "Please enter the number for the item you would like to have." << endl;
        cin >> choice;
        cout << "Please enter the number of " << menuList[choice - 1].itemName << "s you would like to order." << endl;
        cin >> amount_of_item;
        order[choice - 1] = order[choice - 1] + amount_of_item;
        cout << "Would you like to order more items? y/n" << endl;
        cin >> orderMore;
        if (orderMore == 'y' || orderMore == 'Y')
            flag = true;
        else
            flag = false;
    }

}
void read(menuItemType menuList[], int& counter)
{
    ifstream in;
    in.open("menu.txt");
    char temp = char();
    int i = 0;

    while (!in.eof())
    {
        getline(in, menuList[i].itemName);
        in >> menuList[i].itemPrice;
        //cout << menuList[i].itemName << " " << menuList[i].itemPrice << endl;
        in.get(temp);//to pick up endl after the price
        ++i;
        counter++;
    }
    in.close();
}

void showMenu(menuItemType menuList[], int& counter)
{
    cout << fixed << setprecision(2) << showpoint;
    int i = 0;
    for (i = 0; i < counter; i++)
    { cout << left << setw(2) << i + 1 << " :" << left << setw(20) << menuList[i].itemName;
    cout << right << setw(1) << "$" << left << setw(7) << menuList[i].itemPrice << endl;
    }

}

1 个答案:

答案 0 :(得分:0)

如果你想将数组传递给函数takeOrder,我可以看到你需要纠正的两个地方。

  1. 函数的定义需要数组,因此声明需要更改为void takeOrder(int[], int, menuItemType menulist[]);
  2. 将main函数的用法改为takeOrder(order, counter, menuList);,因为你的函数接受的数组不是int(order [10]传递整数)
  3. 如果要将函数的整数更改定义传递给takeOrder(int order, int counter, menuItemType menuList[])