#include <string> question

It is my understanding that we add #include string to the top of the program when the data type std::string is involved. However, something has me confused. In the answer key for the FreeForm project “The Object of Your Affection”, the data type std::string is involved in profile.hpp but there is no #include string at the top.

Here is the link to the lesson

https://www.codecademy.com/courses/learn-c-plus-plus/projects/cpp-dating-profile

Here is the answer key for the lesson

Here is the code for those who do not want to click the links

app.cpp

#include <iostream>
#include "profile.hpp"

int main() {

  Profile sam("Sam Drakkila", 30, "New York", "USA", "he/him");
  sam.add_hobby("listening to audiobooks and podcasts");
  sam.add_hobby("playing rec sports like bowling and kickball");
  sam.add_hobby("writing a speculative fiction novel");
  sam.add_hobby("reading advice columns");
  std::cout << sam.view_profile();

}

profile.hpp

#include <vector>

class Profile {
private:
  std::string name;
  int age;
  std::string city;
  std::string country;
  std::string pronouns;
  std::vector<std::string> hobbies;
  
public:
  Profile(std::string new_name, int new_age, std::string new_city, std::string new_country, std::string new_pronouns = "they/them");
  std::string view_profile();
  void add_hobby(std::string new_hobby);

};

profile.cpp

#include <iostream>

#include "profile.hpp"

Profile::Profile(std::string new_name, int new_age, std::string new_city, std::string new_country, std::string new_pronouns)
  : name(new_name), age(new_age), city(new_city), country(new_country), pronouns(new_pronouns) {

  if (new_age >= 18) {
    age = new_age;
  } else {
    age = 0;
  }

}

std::string Profile::view_profile() {

  std::string bio = "Name: " + name;
  bio += "\nAge: " + std::to_string(age);
  bio += "\nPronouns: " + pronouns;
  std::string hobby_string = "Hobbies:\n";

  for (std::string hobby : hobbies) {

    hobby_string += " - " + hobby + "\n";

  }

  return bio + "\n" + hobby_string;

}

void Profile::add_hobby(std::string new_hobby) {

  hobbies.push_back(new_hobby);

}

My question is, shouldn’t profile.hpp need #include string at the top? I have run the code and it runs perfectly fine without it. Why is this the case? When is #include string absolutely necessary?

1 Like

It’s been a while since I’ve done anything with C++, but I have a suspicion that what’s happening is simply your compiler being kind by recognising that std::string is part of the C++ standard library and pulling the appropriate header file in for you automatically in the background.

I don’t think it’s referenced in any of the other headers you’re using (<iostream> and <vector>, from the standard library) so can’t see any other way of it finding its way in to your program.

You shouldn’t rely on the compiler to “notice” omissions from the std library and correct them, though - so yes, I’m fairly sure the solution ought to include #include <string> somewhere as you suggest.

(The last time I used C++ was in '09, so apologies in advance for any mistakes!)

2 Likes

I believe @thepitycoder is correct, and this thread on stackoverflow seems to corroborate: https://stackoverflow.com/questions/16506095/do-i-have-to-use-include-string-beside-iostream

1 Like