c++ - Invalid use of non-static data member -
i want create file in can add persons personlist including counter makes sure capacity of personlist not exceeded. commented functions make clear task(s) should perform.
during compilation following error:
invalid use of non-static data member 'personlist::m_capacity'
unfortunately cannot figure out what's wrong in code:
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 new personlist-object // capacity elements , size = 0 set_size(0); } void personlist::add(person p){ // pre: size < capacity // post: if pre (is true) p has been stored @ // first empty location. size 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; }
c++ not support variable length arrays (vlas), have here:
person m_data[m_capacity];
for work, m_capacity
must compile time constant.
besides that, have syntax error in default constructor, :
required indicate initialization list:
personlist::personlist() : m_capacity(10), m_empty(person()) { .... } // ^ here!
Comments
Post a Comment