将数组中的元素设置为 0 或 NULL
Set element in array to 0 or NULL
我创建了一个根据用户输入调整大小的数组。
int spotInArray = 0;
Bankcard* a = NULL;
int n;
cout << "Enter number of cards" << std::endl;
cin >> n;
a = new Bankcard[n];
这是我在交换机中的代码,供用户选择要删除的卡。
int choice;
cout << "Enter Number of card you would like to delete: " << endl;
cin >> choice;
for (int i = n; i < spotInArray; i++)
{
a[n] = a[n + 1];
a[choice - 1] = 0;
}
我在 a[选择 - 1] = 0 时收到此错误;
智能感知:没有运算符"="与这些操作数匹配
这是完整的代码。
int main()
{
//Bankcard bankcard1("Blue Card", 1, .05, 3000.00, 430.32, 200.35, 124.00);
int spotInArray = 0;
Bankcard* a = NULL; // Pointer to int, initialize to nothing.
int n; // Size needed for array
cout << "Enter number of cards" << std::endl;
cin >> n; // Read in the size
a = new Bankcard[n]; // Allocate n ints and save ptr in a.
//for (int i=0; i<n; i++) {
//a[i] = bankcard1;
//bankcard1.show();
//}
int choice;
showMenu();
cin >> choice;
while (choice != 6)
{
switch(choice)
{
case 1 : {
string productName;
int cardNum;
double interestRate;
double maxLimit;
double outstandingBalance;
double purchaseAmount;
double paymentAmount;
cout << "Enter Card Name(No spaces, no special characters)" << std::endl;
cin >> productName;
cout << "Enter Number of Card" << std::endl;
cin >> cardNum;
cout << "Enter interest Rate" << std::endl;
cin >> interestRate;
cout << "Enter Max Limit" << std::endl;
cin >> maxLimit;
cout << "Enter Outstanding Balance" << std::endl;
cin >> outstandingBalance;
cout << "Enter Purchase Amount" << std::endl;
cin >> purchaseAmount;
cout << "Enter Payment Amount" << std::endl;
cin >> paymentAmount;
Bankcard bankcard1(productName, cardNum, interestRate, maxLimit, outstandingBalance, purchaseAmount, paymentAmount);
a[spotInArray] = bankcard1;
spotInArray++;
break;
}
case 2 : update();
break;
case 3 : {
int choice;
cout << "Enter Number of card you would like to delete: " << endl;
cin >> choice;
for (int i = 0; i < n; i++)
{
a[n] = a[n + 1];
a[choice - 1] = 0;
}
}
deleteCard();
break;
case 4 : overLoad();
break;
case 5 : {
for ( int i = 0; i < spotInArray; i++)
{
cout << a[i];
}
}
break;
case 6 : exit();
break;
}
showMenu();
cout << endl;
cin >> choice;
};
std::cin.get();
std::cin.get();
return 0;
}
除非有其他代码你没有显示,否则目前这个 for 循环甚至不应该运行:
for (int i = n; i < spotInArray; i++)
{
a[n] = a[n + 1];
a[choice - 1] = 0;
}
因为spotInArray
是0
的,i
从n
开始(你也在递增i
)。
你确定它在那条线上失败了吗?
代码中几乎没有不正确的内容。首先,a[n]
无效。大小n
数组的有效索引为 0 到 n-1。避免自己处理记忆。在这种情况下,请改用std::list
(如果频繁删除)。
std::list<Bankcard> bCards(n);
// Take input to for the cardToRemove.
bCards.erase( bCards.begin() + cardToRemove );
相关文章:
- 为什么在排序链表上的这种合并实现总是将两个列表都设置为 NULL,而只有一个应该设置一个列表?
- 如果我在相应 char 数组的声明中为其提供额外的元素,是否会自动设置 NULL?
- C++ 由于类析构函数中的指针设置为 NULL 而导致的内存泄漏
- 删除对象与将指针设置为 null
- 在单一实例类中将成员指针设置为 null 的正确方法是什么
- STD :: Sort将数组指针设置为NULL
- 设置 NULL 会导致滞后,[MAX_STRING_LENGTH] = {'\0'};
- 一旦对象设置为 null,用于声明对象的堆大小的变量就设置为 null?
- 将结构元素地址设置为C 中的null(单链接列表)
- 移动语义是否只是一个浅层副本并将其他人的指针设置为 null?
- 删除指针并将指针设置为null之间有什么区别
- 为什么我们需要在move构造函数中将右值引用设置为null
- 将数组中的元素设置为 0 或 NULL
- SQLBindParam:设置 Null 值失败 (ODBC)
- 如果指针设置为 NULL,则对它的任何引用都不会或通过它也是 NULL
- WCHAR_T设置为c 中的null
- 当我要删除它时,指针甚至无法设置为null
- 将对象的数组设置为c 中的null
- 在此初始值设定项方法上将整个数组设置为 NULL 是否C++标准?
- C++如果没有任何像 C# 那样传递的内容,请将函数参数设置为 null