• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

C++ pointers, Struct, and Vectors  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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: 61766
193
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!