0

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.