I have a class named Student with its Name and Address classes.
#ifndef ADDRESS_H #define ADDRESS_H //This is address class #include <string> class Address{ public: Address(std::string street, std::string city, std::string state, std::string zip) : street(street), city(city),state(state),zip(zip) {} std::string street,city,state,zip; std::string aString; aString=street+city+state+zip; //private: }; #endif and the Name class is
#ifndef NAME_H #define NAME_H #include <iostream> #include <string> class Name { friend std::ostream &operator <<(std::ostream &os, const Name &name) { if(name.middle!="") os << name.last << ", "<<name.middle<<" ," << name.first; else os<< name.last <<", "<<name.first; return os; } public: Name(std::string last, std::string middle, std::string first) : last(last), first(first),middle(middle) {} private: std::string last, first, middle; }; #endif And the Student class is like:
#ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> #include "name.h" #include "Address.h" class Person { friend std::ostream &operator <<(std::ostream &os, const Person &person); public: Person(const Name &name, int age, const Address &address); Address address; std::string adr=address.aString; //private: Name name; int age; }; #endif Finally, to call them.
#include <iostream> #include "student.h" #include <string> using namespace std; Person::Person(const Name &name, int age, const Address &address) : name(name), age(age),address(address) {} ostream &operator <<(ostream &os, const Person &person) { os << person.name << " " << person.age<<person.adr; return os; } #include <iostream> #include "student.h" using namespace std; int main() { Person p(Name("Doe","","Jane"), 21, Address("Park Ave","New York","NY","10002")); Person p2(Name("Bane","IHateM","Jane"), 21, Address("Bay parkway","Brooklyn","NY","11223")); cout << p<<endl; cout<< p2<<endl; return 0; } However, there are some errors during compilation. (1) The following line is wrong based on complier, how to fix it please?
std::string adr=address.aString; (2) In my address class, the compiler said that "string does not name a type Error", but following this Why am I getting string does not name a type Error? can't fix the problem, why is that?
Addressclass twice, and missedNamestd::string adr=address.aString;) to do? When do you expect it to be executed?