类中的const成员出错

时间:2015-10-13 02:35:22

标签: c++ const

我有一个名为Student的班级,其NameAddress班级。

#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

并且Name类是

#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

Student类就像:

#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

最后,打电话给他们。

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

但是,编译期间会出现一些错误。 (1)基于编译器,以下行是错误的,请问如何解决?

std::string adr=address.aString;

(2)在我的address类中,编译器说&#34;字符串没有命名类型错误&#34;,但遵循此Why am I getting string does not name a type Error?无法解决问题那是为什么?

1 个答案:

答案 0 :(得分:2)

简单解决方案

最简单的解决方案是将aString=street+city+state+zip;移到Address构造函数中。

对您的adr = ...声明执行相同的操作(您的类标题中仍需要std::string adr;的“声明”。

要理解为什么你写的东西不起作用,请考虑这个:

当你写(在一个类声明中,如在你的标题中)

class myClass
{
    int a = 5;
};

为您声明的int a分配一个默认值 - 这是声明和(默认)赋值。

写作时

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

您正尝试向aString提供默认作业,但这是无效代码。

您可以使用

执行此操作

std::string aString = ...;

但不是

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

因为最后一行是'陈述' - 要执行的东西。