简体   繁体   中英

C++: Array as Function Formal Parameter Gets me Errors

I'm in a computer science class in college. I'm working on an assignment that isn't due soon, but I'm running into a problem and I'm tired of waiting on my teacher to respond to my emails. We use Visual Studio 2015 to learn to code with C++, and have gotten through user-defined functions, arrays and structs. We haven't done anything with classes or anything, and all the information I've found on the errors I'm getting seem to involve these and are thus no help to me.

Whenever I run my program I get the following errors:

"LNK2019 unresolved external symbol "void __cdecl takeOrder(int,int,struct menuItemType * const)" (?takeOrder@@YAXHHQAUmenuItemType@@@Z) referenced in function _main

LNK1120 1 unresolved externals"

both errors are said to be in "line 1".

The documents on the Visual Studio site are a bit too vocab heavy and I'm not understanding all the information there, but I think it has something to do with my declaration of variables. That's the only thing that makes sense.

I don't have a great understanding of arrays, but I think I'm using them right. I've double, triple, quadruple checked the textbook and my notes and my spelling, and I don't know what I'm missing. Below is the code in its entirety (save some of the // stuff that just makes it longer).

#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;
    }

}

I can see two places you need to correct things if you want to pass array to function takeOrder.

  1. definition of function takes array so declaration need to change to void takeOrder(int[], int, menuItemType menulist[]);
  2. change usage in main function to takeOrder(order, counter, menuList); as your function is accepting array not int (order[10] passes integer)

If you want to pass integer change definition of function to takeOrder(int order, int counter, menuItemType menuList[])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM