C ++对象作为类成员

时间:2017-07-24 02:21:42

标签: c++ class

从地址创建对象并尝试引用它无法正常工作

   Student::Student(string studentInfo_c){ // student constructor
       stringstream ss(studentInfo_c);

       getline(ss, lastName, ',');
       getline(ss, firstName, ',');
       getline(ss, address1, ',');
       getline(ss, address2, ',');
       getline(ss, city, ',');
       getline(ss, state, ',');
       getline(ss, zipCode, ',');


       Address sAddrs(address1, address2, city, state, zipCode);



  }

      ostream& operator<<(ostream& os, const Student& s){  os << s.lastName << ", " << s.firstName << " " << s.aAddrs;
      return os; // first place that sAddrs oject is referenced
  }

类原型:

class Student {

  private:

    string line;

    string lastName;
    string firstName;
    string address1;
    string address2;
    string city;
    string state;
    string zipCode;
public:
    //Student() : Address aAddrs   this didnt work...
    Student(string studentInfo_c);
    string get_firstName();
    string get_lastName();
    void set_address(string address1_f, string address2_f, string city_f, string state_f, string zipCode_f);

    friend ostream& operator<<(ostream& os, const Student& s);

    ~Student();

}

错误: 在函数'std :: ostream&amp; operator&lt;&lt;(std :: ostream&amp;,const Student&amp;)':| C:\ Users \ Chris \ Documents \ Summer 2017 Semesters \ HeapOStudents \ student.cpp | 67 | error:'const class Student'没有名为'aAddrs'的成员|

C:\ Users \ Chris \ Documents \ Summer 2017 Semesters \ df \ student.cpp | 73 | error:'aAddrs'未在此范围内声明|

|| ===构建失败:6个错误,0个警告(0分钟,0秒(秒))=== |

P.S。 我知道这与其他问题相似,但它们似乎都没有对我有用,它们稍微先进一点。

感谢,

1 个答案:

答案 0 :(得分:0)

根据评论中的建议,我收集了一份MCVE来说明如何做到这一点:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class Address {
  private:
    string address1;
    string address2;
    string city;
    string state;
    string zipCode;
  public:
    // default constructor (leaving contents empty)
    Address() { }
    // constructor.
    Address(
      const string &address1, const string &address2,
      const string &city, const string &state,
      const string &zipCode):
      address1(address1), address2(address2),
      city(city), state(state), zipCode(zipCode)
    { }

    friend ostream& operator<<(ostream& os, const Address &a);
};

ostream& operator<<(ostream& os, const Address &a)
{
  return os
    << "  Address 1: " << a.address1 << endl
    << "  Address 2: " << a.address2 << endl
    << "  City     : " << a.city << endl
    << "  State    : " << a.state << endl
    << "  Zip Code : " << a.zipCode << endl;
}

class Student {
  private:
    string lastName;
    string firstName;
    Address sAddrs;

  public:
    // constructor.
    Student(const string &studentInfo_c);

    friend ostream& operator<<(ostream& os, const Student &s);
};

Student::Student(const string &studentInfo_c)
  // all members are default constructed (leaving them empty)
{
  stringstream ss(studentInfo_c);
  getline(ss, lastName, ',');
  getline(ss, firstName, ',');
  string address1, address2, city, state, zipCode;
  getline(ss, address1, ',');
  getline(ss, address2, ',');
  getline(ss, city, ',');
  getline(ss, state, ',');
  getline(ss, zipCode, ',');
  sAddrs = Address(address1, address2, city, state, zipCode);
}

ostream& operator<<(ostream &os, const Student &s)
{
  return os
    << "Student " << s.lastName << ", " << s.firstName << endl
    << "Address: " << endl
    << s.sAddrs << endl;
}

int main()
{
  string sample("Doe,John,1 Anyway,,Anytown,Anystate,12345,");
  Student s(sample);
  cout << s;
  return 0;
}

使用g ++测试:

$ g++ -std=c++11 -o test test.cc

$ ./test
Student Doe, John
Address: 
  Address 1: 1 Anyway
  Address 2: 
  City     : Anytown
  State    : Anystate
  Zip Code : 12345


$

注意:

  1. Address提供了两个构造函数:默认构造函数和第二个构造函数。

  2. Student::Student使用默认构造函数构造所有成员。 (因此,Address必须提供一个。)

  3. Student::Student的正文中,创建了Address的临时实例并将其分配给Student::sAddrs。这是有效的,因为赋值运算符(Address& Address::operator=(const Address&))是由编译器生成的。 (这不是最有效的代码,而是源代码工作量最少的代码。)