【问题标题】:Dynamic Allocation. No idea what these errors mean动态分配。不知道这些错误是什么意思
【发布时间】:2014-04-24 15:04:31
【问题描述】:

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

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

/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 并且没有称为 getline(string) 的函数只有一个带有 getline(istream&amp;,string&amp;) 的函数,因为您自然需要告诉程序应该从哪里获取该行。

标签: c++ stl copy-constructor dynamic-allocation


【解决方案1】:

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

你在

中传递了ifstream

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

按值,隐式尝试复制它。

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

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

【讨论】:

  • 这清除了大部分错误。然后我意识到我公然忘记包含一个 istream 变量作为 getline 的参数。现在我留下了一个简短的错误列表,但我也不知道这些是什么意思。这些错误涉及对函数的未定义引用,但没有给出任何行号。这些可能表明什么?错误如下所示。
  • /tmp/ccdjbURO.o: 在函数 main': assignment8.cpp:(.text+0x5a): undefined reference to getData(menuItemType&, int&, std::basic_ifstream >&)' assignment8.cpp:(. text+0x116): 未定义引用showMenu(menuItemType, int)' assignment8.cpp:(.text+0x1a5): undefined reference to showMenu(menuItemType, int)' assignment8.cpp:(.text+0x29f): 未定义引用makeSelection(int&amp;, int, int)' assignment8.cpp:(.text+0x2eb): undefined reference to printCheck(menuItemType, int, int)' collect2: error: ld 返回1 个退出状态
  • 这些是链接器错误。您没有将对象与这些功能联系起来。您需要为它们提供定义。但这是另一回事。
  • 我已经更新了我最初的帖子以显示当前的错误,并且我已经按照要求包含了我的函数原型和定义。谢谢你到目前为止的帮助。我觉得我快到了。
  • @user3569354 我告诉过你这是一个不同的问题,现在你只是让更多的读者感到困惑。
【解决方案2】:

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

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&);

【讨论】:

    猜你喜欢
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多