When I was learning CPP to do exercises, I met error, and asked the boss to solve the problem.

I am using cPP primer to learn the class. When I use vs code to do exercises after the book, I encounter a mistake and don"t know how to solve it.

this is the related code, and the title requires "add the correct constructor to the Person class"

-sharpinclude<iostream>
-sharpinclude<string>
using namespace std;
class Person
{
    private:
      string strName;
      string strAddress;
    public:
      Person() = default;
      Person(const string &name,const string &add)
      {
          strName = name;
          strAddress = add;
      }
      Person(std::istream &is) { is >> *this; }

    public:
      string getName() const { return strName; }
      string getAddress() const { return strAddress; }
      std::istream &read(std::istream &is, Person &per)
      {
          is >> per.strName >> per.strAddress;
          return is;
      }
      std::ostream &print(std::ostream &os, const Person &per)
      {
          os << per.getName() << per.getAddress ();
          return os;
      }
};

error occurs here at run time



The

code is entered according to the answers to the after-class exercises. I don"t know how to solve the error. I want to ask the boss to solve it. Thank you very much.

Jul.06,2021

you must have lost a little bit. You haven't defined the operation istream on the left, Person on the right, and in the middle.


cPP primer?


@ 7856 is right. First of all, there is no original answer book, it is written by the translator himself, and there are some mistakes, so sometimes you can't believe all the answers.

if you take a closer look at the example of Sales_data in Primer, there is no definition of the operator is > > * this . If you want to write in this way, you have to provide the definition of . And the second public keyword in the answer is obviously superfluous.

it should be written like this:

-sharppragma once
-sharpinclude <iostream>
-sharpinclude <string>
class Person{

friend std::istream &read(std::istream &is, Person &per);
public:
    Person() = default;
    Person(const std::string &name, const std::string &add)
    {
        strName = name;
        strAddress = add;
    }
    Person(std::istream &is) { read(is, *this); }

    std::string getName() const { return strName; }
    std::string getAddress() const { return strAddress; }

private:
    std::string strName;
    std::string strAddress;

};

std::istream &read(std::istream &is, Person &per)
{
    is >> per.strName >> per.strAddress;
    return is;
}
std::ostream &print(std::ostream &os, const Person &per)
{
    os << per.getName() << per.getAddress();
    return os;
}

  • read and print are not member functions, they are just functions related to this class, so don't put them inside class, just put them outside
  • .
  • in fact, you must put this class in a header file. Do not use using namespace std in the header file. There is no using namespace std; . When you write function parameters, you should also write as std::string .
Menu