2014-04-24 61 views
0

这是一个项目,我不得不动态地创建一个结构数组。不知道这些错误是什么意思或我的代码有什么问题。动态分配。不知道这些错误意味着什么

根据迄今为止给出的建议,我的大部分问题都已解决。以下是剩余错误的简短列表。

/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% 
+0

你不能复制ifstream的,而且也没有所谓的'函数getline(字符串)'只有一个'函数getline(istream的和,串)功能'因为你自然需要告诉程序:像这样的东西代替它们它应该从那里获得该线。 – PeterT

回答

0

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

您的ifstream

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

的值,它隐含试图把它拷贝过去。

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

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

+0

大部分错误都是由此产生的。然后我意识到我公然忘记包含一个istream变量作为getline的参数。现在我剩下一小堆错误,但我不知道这些意思是什么。这些错误讨论了函数的未定义引用,但不提供任何行号。这些可能表明什么?下面显示的错误。 – user3569354

+0

/tmp/ccdjbURO.o:函数'main': assignment8.cpp :(.text + 0x5a):对getData(menuItemType&,int&,std :: basic_ifstream > )' assignment8.cpp :(。text + 0x116):对'showMenu(menuItemType,int)'的未定义引用' assignment8.cpp :(.text + 0x1a5):对'showMenu(menuItemType,int)'的未定义引用' assignment8 .cpp :(.text + 0x29f):对'makeSelection(int&,int,int)'的未定义引用' assignment8.cpp :(.text + 0x2eb):'printCheck(menuItemType,int,int)'的未定义引用collect2 :错误:ld返回1退出状态 – user3569354

+0

这些是链接器错误。你没有链接对象与这些功能。你需要为它们提供定义。然而,这是一个不同的问题。 – luk32

0

您的转发声明与定义不同。

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&); 
相关问题