Trouble using an array in a class (C++) -


i've looked through various forum posts here , other sites have not seen referring similar problem. problem i'm having is: studentinfo array not run besides when array elements 6 or below. i want able have array size of 23, code returns:

bash: line 12: 51068 segmentation fault      $file.o $args 

the code i've provided below simplified version of actual code. have use array in program (no vectors may want suggest) because part of assignment. still little new c++ explanation answers awesome. help!

#include <iostream> #include <string>  using namespace std;  class studentgrades {     private:     string studentinfo[23];      public:     void setstudentinfo(string info) {         (int = 0; < 23; ++i) {             studentinfo[i] = info;         }     }      string getstudentinfo() {         (int = 0; < 23; ++i) {             cout << studentinfo[i] << " ";         }     } };  int main() {      studentgrades student1;      student1.setstudentinfo("bob");      student1.getstudentinfo(); } 

the code has undefined behavior because member function getstudentinfo returns nothing though declared non-void return type.

declare like

void  getstudentinfo() const {     (int = 0; < 23; ++i) {         cout << studentinfo[i] << " ";     } } 

also better not use magic numbers. can add static constant data member in class this

class studentgrades {     private:     static const size_t n = 23;     string studentinfo[n];     .... 

and use everywhere needed. example

void  getstudentinfo() const {     (size_t = 0; < n; ++i) {         cout << studentinfo[i] << " ";     } } 

Comments

Popular posts from this blog

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

laravel - Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id (View: F:\project\resources\views\admin\carousels\index.blade.php) -