2013-05-04 105 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()) 
相关问题