0

making object from Address and trying to refer to it is not working

   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 prototypes:

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();

}

error: In function 'std::ostream& operator<<(std::ostream&, const Student&)':| C:\Users\Chris\Documents\Summer 2017 Semesters\HeapOStudents\student.cpp|67|error: 'const class Student' has no member named 'aAddrs'|

C:\Users\Chris\Documents\Summer 2017 Semesters\df\student.cpp|73|error: 'aAddrs' was not declared in this scope|

||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

p.s. I know this is similar to other questions but none of them have seemed to work for me, they are slightly more advanced.

thanks,

thisissparzo
  • 37
  • 1
  • 8
  • 2
    Show actual code, and actual error messages; don't describe them in prose. Your example only remotely resembles C++, and doesn't make much sense. – Igor Tandetnik Jul 24 '17 at 02:31
  • Pseduocode won't help solve the problem. Post actual code. – CinCout Jul 24 '17 at 02:33
  • `Address sAddrs(address1, address2, city, state, zipCode);` if `sAddrs` is a class member of `Student` then you may need to use initializer list – duong_dajgja Jul 24 '17 at 03:05
  • 4
    Declaring a variable named `sAddrs` in the `Student` constructor does *not* give `Student` a data member named `sAddrs`. – cdhowie Jul 24 '17 at 03:05
  • 1
    ***|error: 'const class Student' has no member named 'aAddrs'|*** The compiler is correct. `sAddrs` needs to be a member of the Student class. It won't help to be a local variable in the constructor. – drescherjm Jul 24 '17 at 03:08
  • Should it be declared as a private member then? – thisissparzo Jul 24 '17 at 03:16
  • Yes make it a member. Although then your problem is you can't initialize it in the constructor's initializer list with parameters to it that you calculate in your constructor's body. Unless `Address` has some other way of setting the parameters. – drescherjm Jul 24 '17 at 03:59

1 Answers1

0

According to recommendations in comments, I assembled an MCVE to show how it could be done:

#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;
}

Tested with 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


$

Notes:

  1. Address provides two constructors: a default constructor and a second one for initialization.

  2. Student::Student constructs all members with default constructors. (Therefore, Address has to provide one.)

  3. In the body of Student::Student, a temporary instance of Address is created and assigned to Student::sAddrs. This works because the assignment operator (Address& Address::operator=(const Address&)) is generated by the compiler. (This is not the most efficient code but the one with least source code effort.)

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • Ahh, I see. I don't need to initialize all of the members in Student. Thanks for the help @Scheff. How can I assign you credit? – thisissparzo Jul 24 '17 at 14:21
  • @thisissparzo Members are always constructed with every constructor of the resp. class - either explicitly or implicitly. (I.e. if you don't do it in your code, the compiler will do it for you). Implict construction does work only for members with default constructor. (Otherwise, you get a compiler error.) Btw. [SO: Help Center > Asking](https://stackoverflow.com/help/someone-answers) explains how to "assign credit". – Scheff's Cat Jul 24 '17 at 14:30
  • @thisissparzo I used "construct" instead of "initialize" because there is a difference for [POD](http://en.cppreference.com/w/cpp/concept/PODType) ("plain old data"). In older standards, implicit construction of POD members was leaving them blank (i.e. uninitialized). In the past, I got "ugly" bugs when I forgot to initialize e.g. a `bool` member. Since then, I explicitly initialize POD members always. This topic is described in [SO: Default, value and zero initialization mess](https://stackoverflow.com/questions/29765961/default-value-and-zero-initialization-mess) and linked pages. – Scheff's Cat Jul 24 '17 at 14:44