简体   繁体   中英

Error with const member in class

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM