C++ question
following is a the definition of a function within a class "Tasklist" void addTask(char name[],char course[],char dueDate[]){ taskCount++; Task task(taskCount, name, course, dueDate); tasks[taskCount] = task; } where "tasks" is an array of object "Task" The problem this: at the end of the function, the members of the element at "tasks[taskCount]" like name, due date, etc, are indeed what was passed into this function, but after returning to this function's caller, all those values become garbage.
I figured it was something to do with passing by reference verses passing by value, but I'm not really passing the tasks array around; it is a member of the class that this function has access too, right?
Here is a summary of the class definition: class Task { private: int number; char* name; char* dueDate; char* course; char* saveText; public: Task(int num, char n[], char c[], char d[]){ number = num; name = new char[strlen(n) + 1]; dueDate = new char[strlen(d) + 1]; course = new char[strlen(c) + 1]; saveText = new char[strlen(n) + strlen(d) + strlen(c) + 3]; strcpy(name, n); strcpy(dueDate, d); strcpy(course, c); strcat(strcat(strcat(strcat(strcpy(saveText,n) ,";"),c),";"),d); }; ~Task(){ delete [] name; delete [] dueDate; delete [] course; delete [] saveText; }
figured it out.
Once rewrite your class and define the variables: int number; char* name; char* dueDate; char* course; char* saveText; as public! and test again!
Join our real-time social learning platform and learn together with your friends!