Tell me what's wrong ...

// students.h class NameStudents { public: void GetNameStudents() const; private: char *chNameStudents[3]; chNameStudents[0] = "Bob"; chNameStudents[1] = "Milan"; chNameStudents[2] = "Edvard"; }; // student.cpp #include <iostream> #include "students.h" using namespace std; void NameStudents::GetNameStudents() const const { for (int i = 0; i < 2; i++) cout << chNameStudents[i] << endl; } int main() { Stds Students; Stds.GetNameStudents(); } 

mistake:

In file included from /GCC/3.3.3/samp/STUDENTS/students.cpp:2: /GCC/3.3.3/samp/STUDENTS/students.h:7: error: ISO C++ forbids initialization of member chNameStudents' / GCC /3.3.3/samp/STUDENTS/students.h:7: error: making chNameStudents' static /GCC/3.3.3/samp/STUDENTS/students.h:7: error: invalid in-class initialization of static data member of non-integral type int [0] '/GCC/3.3.3/samp/STUDENTS/students.h:8: error: ISO C ++ forbids initialization of member chNameStudents' /GCC/3.3.3/samp/STUDENTS/students.h:8: error: making chNameStudents 'static /GCC/3.3.3/samp/STUDENTS/students.h:8: error: invalid in-class non-integral type int[1]' /GCC/3.3.3/samp/STUDENTS/students.h:9: error: ISO C++ forbids initialization of member chNameStudents '/GCC/3.3.3/samp/STUDENTS/students.h:9: error: making chNameStudents' static /GCC/3.3.3/samp/STUDENTS/students.h:9: error: invalid in-class initialization of static data member of non-integral type int [2] 'In file inclu ded from /GCC/3.3.3/samp/STUDENTS/students.cpp:2: /GCC/3.3.3/samp/STUDENTS/students.h:10set: warning: no newline at end of file /GCC/3.3 .3 / samp / STUDENTS / students.cpp: In function int main()': /GCC/3.3.3/samp/STUDENTS/students.cpp:13: error: Stds' undeclared (first use this function) / GCC / 3.3.3 / samp / STUDENTS / students.cpp: 13: error: (it appears in.) /GCC/3.3.3/samp/STUDENTS/students.cpp:13: error : parse error before ;' token /GCC/3.3.3/samp/STUDENTS/students.cpp:18:2: warning: no newline at end of file ;' token /GCC/3.3.3/samp/STUDENTS/students.cpp:18:2: warning: no newline at end of file

Tell me how to manipulate arrays defined in classes?

  • one
    Initialize in the constructor when creating an instance of the class. - alexlz
  • Why use char ** in pluses as a class field? Use STL facilities) - Andrey Buran

2 answers 2

 // students.h class NameStudents { public: NameStudents() { chNameStudents[0] = "Bob"; chNameStudents[1] = "Milan"; chNameStudents[2] = "Edvard"; } void GetNameStudents() const; private: char *chNameStudents[3]; }; // student.cpp #include <iostream> #include "students.h" using namespace std; void NameStudents::GetNameStudents() const { for (int i = 0; i < 2; i++) cout << chNameStudents[i] << endl; } int main() { NameStudents Students; Students.GetNameStudents(); } 
     // student.cpp #include <iostream> #include "students.h" using namespace std; void NameStudents::GetNameStudents() const { for(int i = 0; i < 2; i++) cout << this->chNameStudents[i] << endl; } int main() { NameStudents Students; Students.GetNameStudents(); } 
    • the answer is higher
    • 1. Not this.varname, but this-> varname, since this is a pointer 2. In this case, it is unnecessary - skegg
    • 1 really 2 I don’t think that it’s so difficult, it’s much harder to clean up the problems that may arise In this example, this is of course funny (I’m talking about problems), but you need to learn to do it from an early age))))) - VorobyevEvgeniy