Learning to work with structures. I want to implement adding an element using the AddNewElement() function; But the program does not compile. Although if this piece of code from the function is transferred to the initialization itself ( Init() ), it works fine. Apparently the problem with the end . But how to deal with it, I can not figure it out.

 #include <iostream> using namespace std; struct Student { char* name; int age; }; struct List { Student student; List* next; }; void Print(List *begin) { List* print = begin; while(print) { cout <<print->student.name<<"->"<<print->student.age<<endl; print = print->next; } cout<<"NULL"<<endl; } void AddNewElement(char* NewName, int NewAge) { end ->next = new List; end = end->next; end -> student.name = NewName; end -> student.age = NewAge; end ->next = NULL; } void Init(List **begin) { *begin = new List; (*begin) -> student.name = "Andrew"; (*begin) -> student.age = 20; (*begin) -> next = NULL; List* end = *begin; AddNewElement("Petr", 19); } int main() { List *begin = NULL; Init(&begin); Print(begin); return 0; } 
  • The error produces the type overloaded function with no contextual type information. - V. Rotenberh
  • There was already a lot of such questions. You can read the answers, for example, but this one is avp

2 answers 2

The simplest thing is to change your code to:

 #include <iostream> using namespace std; struct Student { char* name; int age; }; struct List { Student student; List* next; }; void Print(List *begin) { List* print = begin; while(print) { cout <<print->student.name<<"->"<<print->student.age<<endl; print = print->next; } cout<<"NULL"<<endl; } void AddNewElement(List *end, char* NewName, int NewAge) { end ->next = new List; end = end->next; end -> student.name = NewName; end -> student.age = NewAge; end ->next = NULL; } List *Init(List **begin) { *begin = new List; (*begin) -> student.name = "Andrew"; (*begin) -> student.age = 20; (*begin) -> next = NULL; return *begin; } int main() { List *begin = NULL; List *end = Init(&begin); AddNewElement(end, "Petr", 19); Print(begin); return 0; } 

A working example and its conclusion:

 Andrew->20 Petr->19 NULL 

UPD (to comment 1)

As Harry noted, the AddNewElement function uses the end variable, a pointer to the end of the list. But the problem is that this variable is not in the scope of the AddNewElement function. There are two options, either to declare it globally, or pass it as a parameter. The latter is more for 'feng shui' Well, another problem arises: end is defined as local in the Init function. But we after all do initialization of the list once? Why don't initialization functions return all necessary data for further work with the list? That's actually what she does.

UPD (to comment 2)

Change

 void AddNewElement(List *end, char* NewName, int NewAge) 

on

 List *AddNewElement(List *end, char* NewName, int NewAge) 

and at the end of the function add

  return end; 

well and adding elements to do so

 end = AddNewElement(end, "Petr", 19); 

That's what to avoid all these "returns", it is better to do in classes. then end it will be possible to make a class variable and work with it quietly from the methods.

  • And it is possible in more detail, why did we remake the initialization, in the function with the return value? - V. Rotenberh
  • It still does not work as it should. When adding one more element, it does not grow to the end, but overwrites the second one. - V. Rotenberh
  • @V. Rotenberh, see additions to the answer. - Embedder
  • Everything works as it should. Thank. I will study further. - V. Rotenberh
 void AddNewElement(char* NewName, int NewAge) { end ->next = new List; end = end->next; end -> student.name = NewName; end -> student.age = NewAge; end ->next = NULL; } 

You have no end variable declared at all here! How can the compiler know what you want from it? :)

See - you pass the function name and age. All student is. But IN WHAT LIST to make it? you don't say anything about it ...

  • Thank. I will work with an example that answered @Embedder - V. Rotenberh