• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

C++ pointers, Struct, and Vectors

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an assignment to work on that I don't understand. Would appreciate any assistance to get me headed in the right direction. The instructions for this are:

Define a structure Student with a name and a vector<Course*> of courses.
• Define a structure Course with a name and a vector<Student*> of enrolled students.
• Define a function void print_student(Student* s) that print the name of a student and the names of all courses that the student takes.
• Define a function void print_course(Course* c) the prints the name of a course and the names of all students in that course.
• Define a function void enroll(Student* s, Course* c) that enrolls the given student in the given course, updating both vectors.
Note: because the Student structure refers to Course, and Course refers to Student, you'll need to use a structure declaration (aka—a forward reference) to allow you to write the definitions in the header file.

This is what I have in my header file:




In my .cpp file this is what I have:



I'm using Visual Studio 2013 and I can't get anything to compile. I'm not even sure what to do, any help would be appreciated. Thank you.
 
Ranch Hand
Posts: 69
2
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, you will need to include the standard library headers for the artifacts that you're using in your header file. Otherwise the compiler will puke when it comes across a name that it can't find. You will also either have to (not recommended) put a 'using' declaration in your header or (recommended) explicitly qualify the artifacts that you're using from standard headers.





Typically, a C++ interface / implementation pair are used in this fashion:





The #include directive is essentially a glorified copy and paste. When #including a file, all that you are doing is telling the compiler to paste whatever is in the file in the spot that you've #included it. So in this case, you're pasting your header file at the top of your implementation file, then feeding the compiler the resulting 'translation unit' as it's called.

Another issue that I can see off-hand is in your printing functions, you're printing out the pointers that are provided.



This then of course makes memory management a bit tricky. There is a better way using *Modern C++* as illustrated below:



I'm sorry that your instructor is still teaching outdated C++. Bear with it until you pass the course, then explore the wonders of "modern" C++.

* Not quite sure why code tags aren't working.
 
Marshal
Posts: 80093
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luke Leber wrote: . . .

* Not quite sure why code tags aren't working.

Because you wrote [code=C++11] and we don't have a C++ option?? They worked when I got rid of =C++11 and re‑enabled BB code.

That's a bl**d* good post, BTW.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Subject: C++ pointers, Struct, and Vectors

Interesting code.

Something is missing. How do I print out:

- Student course information

-Course student information
 
Yeah, but is it art? What do you think tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic