1

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?

4
  • 1
    You posted your Address class twice, and missed Name Commented Oct 13, 2015 at 2:43
  • Thank for reminding.I have fixed it. Commented Oct 13, 2015 at 2:50
  • 2
    It appears that you continued to write code after introducing this error; never add to code that doesn't work. It also appears that you are attempting a complicated exercise before you have mastered the basics. What do you expect the assignment (std::string adr=address.aString;) to do? When do you expect it to be executed? Commented Oct 13, 2015 at 2:51
  • 2
    @immibis - he changed the file, like I suggested in the first comment, but not the problem. It was a valid edit Commented Oct 13, 2015 at 3:21

1 Answer 1

2

Simple Solution

The simplest solution is to move your aString=street+city+state+zip; inside the Address constructor.

Do the same for your adr = ... statement (you'll still need a 'declaration' for std::string adr; within your class header).

To understand why what you wrote won't work, consider this:

When you write (within a class declaration, like in your header)

class myClass { int a = 5; }; 

you assign a default value to the int a that you have declared - this is both declaration and (default) assignment.

When you write

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

you're trying to give a default assignment to aString, but this is invalid code.

You could do this using

std::string aString = ...;

but not

std::string aString; aString = ...; 

because the last line is a 'statement' - something to be executed.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.