2013-05-04 106 views
-1

的我要創造,我可以加人到PersonList包括一個計數器,它可以確保PersonList的容量不超過一個文件。我評論了所有的功能,以明確他們應該執行什麼任務。無效使用非靜態數據成員

在編譯過程中,我得到以下錯誤:

invalid use of non-static data member 'PersonList::m_Capacity' 

不幸的是我無法弄清楚什麼是錯的代碼:

的main.cpp

#include <iostream> 
#include "Person.h" 
#include "PersonList.h" 

int main() 
{ 

    Person p1 = Person("John", 21); 
    Person p2 = Person("Elham", 19); 
    PersonList p_list = PersonList(); 

    p_list.add(p1); 
    p_list.add(p2); 

    std::cout << p_list.get_Size() << std::endl; 
    Person p = p_list.get(0); 
    std::cout << p.get_Name() << " " << p.get_Age() << std::endl; 

    return 0; 
} 

Person.h

#ifndef PERSON_H 
#define PERSON_H 

class Person 
{ 
    public: 
     Person(); 
     Person(std::string Name, int Age); 

     void set_Name(std::string name); 
     void set_Age(int age); 
     std::string get_Name(); 
     int get_Age(); 

    private: 
     std::string m_Name; 
     int m_Age; 
}; 

#endif // PERSON_H 

Person.cpp

#include <string> 
#include "Person.h" 

Person::Person(){} 

Person::Person(std::string Name, int Age){ 
// pre: 0 <= Age 
// post: m_Name, if pre Name (is true) Age = m_Age, 
//  otherwise age = -1 

    set_Name(Name); 

    if (0 <= Age){ 

     set_Age(Age); 
    } 

    else{ 

     set_Age(-1); 
    } 

} 

void Person::set_Name(std::string Name){ 
// post: m_Name = Name 

    m_Name = Name; 
} 

void Person::set_Age(int Age){ 
// pre: 0 <= Age 
// post: if pre Age (is true) = m_Age, otherwise m_Age = -1; 

    if (0 <= Age){ 

     m_Age = Age; 
    } 

    else set_Age(-1); 
} 

std::string Person::get_Name(){ 
// post: returns name 

    return m_Name; 
} 

int Person::get_Age(){ 
// post: returns age 

    return m_Age; 
} 

PersonList.h

#ifndef PERSONLIST_H 
#define PERSONLIST_H 

class PersonList 
{ 
    public: 
     PersonList(); 

     void add(Person p); 
     void set_Size(int Size); 
     int get(int index); 
     int get_Size(); 

    private: 
     const int m_Capacity; 
     const Person m_Empty; 
     Person m_Data[m_Capacity]; 
     int m_Size; 

}; 

#endif // PERSONLIST_H 

PersonList.cpp

#include "Person.h" 
#include "PersonList.h" 

PersonList::PersonList() 
m_Capacity(10), m_Empty(Person()) 
{ 
// post: has created a new PersonList-object with 
//  CAPACITY elements and size = 0 

    set_Size(0); 
} 

void PersonList::add(Person p){ 
// pre: size < CAPACITY 
// post: if pre (is true) p has been stored at the 
//  first empty location. size was incremented. 

    if (m_Size < m_Capacity){ 

     m_Data[m_Size] = p; 
     set_Size(m_Size+1); 
    } 

} 

void set_Size(int Size){ 

    m_Size = Size; 
} 

int PersonList::get(int index){ 
// pre: 0 <= index && index < size 
// post: if pre returns data[index] otherwise returns EMPTY 

    if (0 <= index && index < m_Size){ 

     return m_Data[index]; 
    } 

    else{ 

     return m_EMPTY; 
    } 
} 

int PersonList::get_Size(){ 
// post returns size; 

    return m_Size; 

} 

回答

5

C++不支持可變長度的數組(VLA),這是你必須在這裏:

Person m_Data[m_Capacity]; 

對於這個工作,m_Capacity必須是一個編譯時間常量。

除此之外,你有一個語法錯誤在默認的構造函數,在需要:以指示初始化列表:

PersonList::PersonList() : m_Capacity(10), m_Empty(Person()) { .... } 
//      ^Here! 
+0

m_Capacity已經是一個常數。 – Zasz 2013-05-04 16:58:06

+0

@Zasz每個PersonList實例都是不變的,但它不是一個編譯時間常量。 – juanchopanza 2013-05-04 16:59:04

+0

@juanchopanza你能告訴我編譯時間常量究竟是什麼,爲什麼我的代碼中的const int不是這樣一個常量? – Pietair 2013-05-04 18:14:29

0

構造函數和初始化列表必須由冒號分隔。像這樣

PersonList::PersonList() : m_Capacity(10), m_Empty(Person()) 
相關問題