1

When I run my program, I get the Segmentation Fault error. I believe it is coming from how I am using the array "string *words" which is privately declared in the class definition. I use it here in the .cpp file anyone know what I need to change? heres the function I think the problem is in:

Dictionary::Dictionary(string filename){

    ifstream inF;

    inF.open(filename.c_str());

    if (inF.fail()){
      cerr << "Error opening file" <<endl;
      exit(1);
    }

    inF >> numwords;
    numwords = 3000;
    words = new string(words[numwords]);


    for(int i=0; i <= numwords - 1; i++){
      inF >> words[i];
    }
    inF.close();
  }
2
  • words = new string(words[numwords]) - what is that line supposed to do in your code? What is the purpose of words[numwords] on the right-hand side, inside the braces? If you are the author, then I'm sure you should be able to explain why it is written that way. Commented Apr 19, 2011 at 0:25
  • What are you implying Andrey? It may just be a simple mistake. Commented Apr 19, 2011 at 1:04

2 Answers 2

9

The line:

words = new string(words[numwords]);

should actually be:

words = new string[numwords];
Sign up to request clarification or add additional context in comments.

Comments

4

You are going to need to learn how to use a debugger. The exact procedure depends on what system you're using. But if you run your code in a debugger, the debugger will stop on the exact line where the problem was detected. As you can imagine, this is very helpful for debugging.

Note that the line where the problem was detected and the line where the problem started may be quite different.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.