2017-07-24 136 views
0

從地址製作對象,並試圖指它不工作C++對象爲類成員

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

}

錯誤: 在函數「STD :: ostream &運營商< <(std :: ostream &,const學生&)':| 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學期\ df \ student.cpp | 73 |錯誤:'aAddrs'未在此範圍內聲明|

|| ===構建失敗:6個錯誤,0個警告(0分鐘,0秒)===

p.s. 我知道這與其他問題類似,但他們都沒有爲我工作,他們稍微更先進。

感謝,

+2

顯示實際的代碼和實際的錯誤消息;不要用散文形容他們​​。你的例子只是遠程類似於C++,並沒有多大意義。 –

+0

Pseduocode不會幫助解決問題。張貼實際的代碼。 – CinCout

+0

'sAddrs(address1,address2,city,state,zipCode);'如果'sAddrs'是'Student'的類成員,那麼您可能需要使用初始化列表 –

回答

0

根據意見建議,我組建了一個MCVE顯示它如何能做到:

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

測試與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 


$ 

注:

  1. Address提供了兩個構造函數:默認構造函數和第二個用於初始化的構造函數。

  2. Student::Student構造具有默認構造函數的所有成員。 (因此,具有Address以提供一個。)

  3. Student::Student體內,被創建並分配給Student::sAddrsAddress一個臨時的實例。這是有效的,因爲賦值運算符(Address& Address::operator=(const Address&))由編譯器生成。 (這不是最有效的代碼,而是最少使用源代碼的代碼。)

+0

啊,我明白了。我不需要初始化學生中的所有成員。感謝@Scheff的幫助。我怎樣才能讓你信用? – thisissparzo

+0

@thisissparzo成員總是由每個構造函數構造而成。類 - 顯式或隱式。 (也就是說,如果你沒有在你的代碼中做,編譯器會爲你做)。 Implict構造只適用於具有默認構造函數的成員。 (否則,你會得到一個編譯器錯誤。)順便說一句。 [SO:幫助中心>問](https://stackoverflow.com/help/someone-answers)解釋如何「分配信用」。 – Scheff

+0

@thisissparzo我使用「構造」而不是「初始化」,因爲[POD](http://en.cppreference.com/w/cpp/concept/PODType)(「普通舊數據」)存在差異。在較老的標準中,POD成員的隱含結構將其留空(即未初始化)。過去,當我忘記初始化時,我得到了「醜陋」的錯誤,例如一個'bool'成員。從那以後,我總是明確地初始化POD成員。本主題在[SO:默認,值和零初始化混亂](https://stackoverflow.com/questions/29765961/default-value-and-zero-initialization-mess)和鏈接頁面中進行了介紹。 – Scheff