Hey guys , i am having a problem with string arrays. Basically ,i declared my array in the header file. Here is my prog.

Main.cpp

using namespace std;

int main(){
string Test = "fat";
e te;
te.b(Test);
}


Test.h

class e
{
  public:
    void b(string)
     string a [ ];
     int f;

};


Array.cpp

void e::b (string t)
{
   e rt;
   rt.f ++;
   rt.a[rt.f] = t;
   cout << rt.a[rt.f] << endl
}

Okay guys , when i tried this using Netbeans on Windows running on MinGW. But when i tried it on Netbeans in Unix or just compiling using g++ using text editor , i kept on getting segmentation error. I hope for some help regarding this matter.

line 18 in Test.h does not declare an array, but only an uninitialized pointer to an array. Therefore line 30 in Array.cpp will crash because the array has no size. Instead of trying to use an array of std::strings, use a std::vector, e.g. std::vector<std::string> a; Then call vector's push_back() method to add a new string to the end of the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.