动态分配.不知道这些错误意味着什么
Dynamic Allocation. No idea what these errors mean
这是一个我必须动态创建结构数组的项目。不知道这些错误意味着什么,也不知道我的代码出了什么问题。
根据到目前为止在这里给出的建议,我的大部分问题都得到了解决。以下是剩余错误的简短列表。
/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)
通过值,它隐含地试图复制它。
我认为复制istream
和ostream
是不可能的。你必须通过引用传递它们。
此外,正如@PeterT在评论中所说,您没有试图调用的getline
版本。两个默认版本都期望input
和string
作为参数。
您的正向声明与定义不同。用这样的东西代替它们:
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&);
相关文章:
- 这行代码在C++类中意味着什么
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- @CPPFLAGS@在 Makefile.in 中意味着什么?
- 生成文件"relink"意味着什么?
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 在这种情况下,"typename..."意味着什么?
- "in-situ without memory allocation" 字符串的愚蠢实现意味着什么?
- 使用typedef有什么用,它意味着什么
- GCC 的 -Wpsabi 选项究竟有什么作用?压制它意味着什么?
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 调用堆栈显示 SIGBUS,这意味着什么
- C++标准在[basic.scope.hiding]中"same scope"到底意味着什么?
- 评估对象的创建意味着什么?
- 当我们在 C++ 中说"initialize the object"时,它实际上意味着什么?
- C++这种结构"InterceptionKeyStroke &kstroke = * (InterceptionKeyStroke *) &stroke"意味着什么?
- 该语法在C 中意味着什么
- 链接标志对静态库意味着什么
- 由mpglib输出的"hip: Can't rewind stream by 74 bits"到底意味着什么?
- 这在 C# 中意味着什么以及如何使用它