如何将数组与指针一起使用?
How to use array with pointer?
我是编程新手,试图创建一个包含一系列记录的数组,然后让程序接受输入并最终打印出列表的内容。
我在addRecord()
函数中记录某些变量的值时遇到问题,因为以下代码的输出如下:
constructor called
-1:-1 Eat lunch
-1:-1 Watch TV
destructor called
为什么我错过了完全L1.addRecord(5, 30, "Make dinner");
的调用,为什么时间没有通过(它们在构造函数中设置为-1(?
谢谢
#include <iostream>
#include <string>
using namespace std;
class Record {
private:
int hour;
int minute;
string todo;
public:
Record()
{
hour = -1;
minute = -1;
todo = "N/A";
}
void setData(int hour, int minute, const char* td);
void setData(Record& e);
void printRecord();
};
class List
{
private:
Record* recordArr;
int maxRecords;
int actualRecordCount;
public:
List(int maxRecords);
List(List& s) {
actualRecordCount = s.actualRecordCount;
maxRecords = s.maxRecords;
recordArr = new Record[maxRecords];
for (int i = 0; i < actualRecordCount; i++)
{
recordArr[i].setData(s.recordArr[i]);
}
std::cout << "copy constructor called." << std::endl;
}
~List();
bool addRecord(int hour, int minute, const char* todo);
void printList();
};
///////////////////////////
void Record::setData(int hour, int minute, const char* td)
{
hour = hour;
minute = minute;
todo = td;
}
void Record::setData(Record& e)
{
hour = e.hour;
minute = e.minute;
todo = e.todo;
}
void Record::printRecord()
{
std::cout << hour << ":" << minute << " " << todo << std::endl;
}
List::List(int maxRecords)
: maxRecords(maxRecords)
{
actualRecordCount = 0;
recordArr = new Record[maxRecords];
std::cout << "constructor called" << std::endl;
}
List::~List()
{
std::cout << "ndestructor called";
delete[] recordArr;
}
bool List::addRecord(int hour, int minute, const char* todo)
{
Record newRecord; // create new Record
newRecord.setData(hour, minute, todo); //assign values
if (actualRecordCount >= maxRecords) // array full
{
return false;
}
else
{
recordArr[actualRecordCount] = newRecord; // put new Record into the array of Entry
actualRecordCount++; // increment Entry count
return true;
}
}
void List::printList()
{
for (int i = 0; i < actualRecordCount; i++)
{
recordArr[i].printRecord();
cout << endl;
i++;
}
}
int main() {
List L1(20);
L1.addRecord(2, 30, "Eat lunch");
L1.addRecord(5, 30, "Make dinner");
L1.addRecord(7, 30, "Watch TV");
L1.printList();
}
void Record::setData(int hour, int minute, const char* td)
{
hour = hour;
"小时"是此setData()
方法的参数名称。 因此,hour=hour;
将此参数设置为自身。这绝对没有任何作用。接下来的其他两个作业也是如此。
您在此处的明显意图是初始化恰好具有相同名称的成员的类。当不同的事物在C++中具有相同的名称时,会有一组复杂的规则来选择名称所代表的"事物"。这些规则在=
运算符的左侧和右侧是相同的,因此这两个hour
最终都引用了同一个对象:此类方法的参数。
您可以简单地将参数重命名为该方法:
void Record::setData(int hourArg, int minuteArg, const char* tdArg)
{
hour = hourArg;
等等。或者,如果您希望保持参数名称相同,请使事情更加明确:
this->hour=hour;
另一个错误在这里
void List::printList()
{
for (int i = 0; i < actualRecordCount; i++)
{
recordArr[i].printRecord();
cout << endl;
i++;
}
}
你有两次i++
。
一个问题是,具有三个参数的Record::setData
函数实际上并没有设置类对象的hour
和minute
字段!这是由于您使用与类成员同名的参数引起的,这(恕我直言(是糟糕的编码风格。因此,在下面的代码中,前两个赋值只是将参数值替换为它们自己:
void Record::setData(int hour, int minute, const char* td)
{
hour = hour; // This and the following line are self-assignments to the arguments
minute = minute; // given and, as such, are effectively doing nothing!
todo = td; // This, however, is OK, because there is no ambiguity.
}
若要解决此问题,请添加对目标的显式this->
引用:
void Record::setData(int hour, int minute, const char* td)
{
this->hour = hour;
this->minute = minute;
todo = td;
}
或者(在我看来,要好得多(给前两个论点一个非歧义的名称:
void Record::setData(int in_hour, int in_minute, const char* td)
{
hour = in_hour;
minute = in_minute;
todo = td;
}
相关文章:
- 在将 new 与指针一起使用时,创建数组的指定长度
- 尝试将指针与结构一起使用时出错
- 为什么我们将 [ ] 与指向数组的指针一起使用?
- 如何将数组与指针一起使用?
- 将函数指针和对象放在一起
- 如何将 string.erase() 与指针一起使用?
- 将智能指针与容器一起使用的原因
- 为什么指针和c++中的函数放在一起
- 为什么当我尝试将priority_queue与参数一起使用作为指向结构的指针时会弹出错误
- __unaligned说明符何时与指针一起使用?
- 如何定义与将 Lambda 与捕获作为回调一起使用兼容的函数指针
- 是否可以将引用类型别名与指针运算符一起使用来声明对指针的引用?
- 为什么在将多态行为与指向接口的指针一起使用时没有调用析构函数?
- 自动关键字与智能指针一起
- 当直接分配时,为什么此功能指针分配起作用,而不是与有条件的操作员一起使用
- shared_ptr ::重置仅与原始指针一起使用
- 将 boost::function 与指向派生类的共享指针的参数一起使用
- 为什么在 sizeof() 函数中与 * 运算符一起使用和不使用 * 运算符时,指向结构变量的指针大小会有所不同?
- 为什么动态演员只能与参考和指针一起使用
- 是否可以将SDL2与智能指针一起使用