动态分配.不知道这些错误意味着什么

Dynamic Allocation. No idea what these errors mean

本文关键字:意味着 什么 错误 不知道 动态分配      更新时间:2023-10-16

这是一个我必须动态创建结构数组的项目。不知道这些错误意味着什么,也不知道我的代码出了什么问题。

根据到目前为止在这里给出的建议,我的大部分问题都得到了解决。以下是剩余错误的简短列表。

/tmp/ccdjbURO.o: In function `main':
assignment8.cpp:(.text+0x5a): undefined reference to `getData(menuItemType&, int&, std::basic_ifstream<char, std::char_traits<char> >&)'
assignment8.cpp:(.text+0x116): undefined reference to `showMenu(menuItemType, int)'
assignment8.cpp:(.text+0x1a5): undefined reference to `showMenu(menuItemType, int)'
assignment8.cpp:(.text+0x29f): undefined reference to `makeSelection(int&, int, int)'
assignment8.cpp:(.text+0x2eb): undefined reference to `printCheck(menuItemType, int, int)'
collect2: error: ld returned 1 exit status

以下是我所要求的函数原型和定义。与程序主体中任何函数调用的格式相比,原型和定义标题中的函数签名没有什么不同。

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    struct menuItemType
    {
       string menuItem;
       double menuPrice;
    };
    const double TAX = 0.05;
    const string FILE_NAME = "Ch9_Ex5Data.txt";
    int getData(menuItemType&, int&, ifstream);
    int makeSelection(int&, int, int);
    void showMenu(menuItemType, int);
    void printCheck(menuItemType, int, int);
    void getInt(int&);
    void getChar(char&);
 //********************************************************************************
//* getData
//********************************************************************************
int getData(menuItemType* &menuList, int& listSize, ifstream& inFile)
{
   inFile.open("Ch9_Ex5Data.txt");
   inFile  >>  listSize;
   if (inFile.fail())
      return -1;
   menuList = new menuItemType[listSize];
   for(int i = 0; i < listSize; i++)
   {
      getline(inFile, menuList[i].menuItem);
      inFile  >>  menuList[i].menuPrice;
      if (inFile.fail())
         return -1;
         break;
   }
                                                                         122,1         47%
   return 1;
}
//********************************************************************************
//* makeSelection
//********************************************************************************
int makeSelection(int* &orderList, int quantity, int index)
{
   if ((orderList[index] + quantity) < 0)
   {
      cout  <<  "Quantity selected makes total number ordered less than 0"
              <<   endl  <<  endl;
        return 1;
   }
   else
   {
      orderList[index] = orderList[index] + 1;
      return -1;
   }
}
//********************************************************************************
//* showMenu
//********************************************************************************
void showMenu(menuItemType *menuList, int listSize)
{
   cout  <<  fixed  <<  showpoint  <<  setprecision(2)  <<  endl  <<  endl
         << "------Today's Menu------"  <<  endl;
   for(int i = 0; i < listSize; i++)
   {
      cout  <<  left   <<  setw(18)  <<  menuList[i].menuItem   <<  "$ "
            <<  right  <<  setw(4)   <<  menuList[i].menuPrice  <<  endl;
   }
   cout  <<  "------------------------"
         <<  endl  <<  endl;
}
//********************************************************************************
//* printCheck
//********************************************************************************
void printCheck(menuItemType *menuList, int *orderList, int listSize)
{
   int taxDue  = 0;
   int amntDue = 0;
   cout  <<  fixed  <<  showpoint  <<  setprecision(2)  <<  endl  <<  endl
         <<  "------Your Reciept------"  <<  endl;
   for (int i = 0; i < listSize; i++)
   {
      if (orderList[i] > 0)
      {
         cout  <<  left   <<  setw(2)  <<  orderList[i]  <<  "  "
               <<  setw(15)  <<  menuList[i].menuItem
               <<  right  <<  setw(5)  <<  (orderList[i] * menuList[i].menuPrice)
               <<  endl;
         amntDue  +=  (orderList[i] * menuList[i].menuPrice);
      }
   }
                                                                         210,1         73%
 taxDue = amntDue * TAX;
   amntDue = amntDue * (1 + TAX);
   cout  <<  endl  <<  right   <<  setw(17)  <<  "Tax:    $ "
                   <<  setw(7) <<  taxDue
         <<  endl  <<  right   <<  setw(17)  <<  "Amount Due:    $ "
                   <<  setw(7) <<  amntDue
         <<  endl
         << "------------------------"  <<  endl  << endl;
}
                                                                     187,0-1       64%

错误看起来确实不友好,但问题很简单。

您在中通过ifstream

int getData(menuItemType* &menuList, int& listSize, ifstream inFile)

通过值,它隐含地试图复制它。

我认为复制istreamostream是不可能的。你必须通过引用传递它们。

此外,正如@PeterT在评论中所说,您没有试图调用的getline版本。两个默认版本都期望inputstring作为参数。

您的正向声明与定义不同。用这样的东西代替它们:

int getData(menuItemType*&, int&, ifstream&);
int makeSelection(int*&, int, int);
void showMenu(menuItemType*, int);
void printCheck(menuItemType*, int*, int);
void getInt(int&);
void getChar(char&);