【发布时间】:2019-05-17 07:18:40
【问题描述】:
我有一个 ToDo 类,它具有描述、日期和优先级作为私有成员变量。
我正在尝试获取已填充的 ToDo 并将其添加到 ToDo 的全局数组中。
我已经使用 this->description、this->date 和 this->priority 设置了对象的所有成员变量,但是当我尝试使用以下命令添加对象时 - TODO_GLOBAL_ARRAY[CURRENT_LOC_OF_ARRAY] = this; - 我收到一条错误消息,指出“没有可行的重载 '='”。
我还尝试实例化 ToDo 对象的一个实例并将其传递给数组,但这仍然使原始对象没有数据并且无法正确打印出来。
//ToDo Header
#include <string>
using std::string;
#ifndef TODOLIST
#define TODOLIST
class ToDoList{
private:
string description;
string date;
int priority;
public:
bool addToList(ToDoList todoItem);
bool addToList(string desc, string date, int priority);
bool getNextItem(ToDoList &toDoItem);
bool getNextItem(string &desc, string &date, int &priority);
bool getByPriority(ToDoList *results, int priority);
bool getByPriority(ToDoList *results, int priority, int &resultSize);
void printToDo();
void printToDo(ToDoList aToDo);
void printToDoList(ToDoList *aToDoList);
void printToDoList(ToDoList *aToDoList, int size);
ToDoList();
ToDoList(string desc, string date, int priority);
// TODO: implement method to get ToDo from usr input
};
#endif
extern ToDoList usr_TODO_list[];
extern const int MAX_ITEMS_TODO;
extern int SIZE_OF_USR_LIST;
extern int NEXT_INDEX;
// From ToDo.cpp
bool ToDoList::addToList(string desc, string date, int priority){
if (SIZE_OF_USR_LIST == MAX_ITEMS_TODO) {
return false;
}
else{
ToDoList aToDo;
this->description = desc;
this->date = date;
this->priority = priority;
usr_TODO_list[SIZE_OF_USR_LIST] = this;
SIZE_OF_USR_LIST++;
return true;
}
}
// From main.cpp
using namespace std;
ToDoList usr_TODO_list[100];
int const MAX_ITEMS_TODO (100);
int SIZE_OF_USR_LIST = 0;
int NEXT_INDEX = 0;
int main()
{ // etc...
预期:使用 'this' 将对象传递到数组中
实际:没有可行的重载 '=' 错误
【问题讨论】:
-
这里的一些命名有点混乱,
ToDoList实际上是一个数组元素而不是一个列表。我会尝试重构您的代码以使其更清晰。无论如何,如果你想添加一个副本,你应该使用usr_TODO_list[SIZE_OF_USR_LIST] = *this;,这样你就不会试图分配一个指针。如果您实际上是在尝试存储指针,那么您可能希望将您的数组声明为ToDoList *usr_TODO_list[100];。 -
这个架构很奇怪,但是你的 else 子句应该可以简化为
usr_TODO_list[SIZE_OF_USR_LIST++] = ToDoList(desc, date, priority); return true;并注意aToDo在你的实现中是没有意义的。不知道你想要什么。无论如何,您的代码似乎厚颜无耻地将 ToDo list 与 ToDo item 混淆。前者应该包含后者的实例,并且就我所见,后者只需要一个描述、日期和优先级的简单对象。 -
@George 谢谢!!!我忘了这是一个指针。我使用了尊重运算符,它起作用了。您可以将其发布为回复,以便我将其标记为正确答案吗?谢谢!