Segmentation fault, shared library

Posted by user1306184 on Stack Overflow See other posts from Stack Overflow or by user1306184
Published on 2012-04-01T11:23:52Z Indexed on 2012/04/01 11:30 UTC
Read the original article Hit count: 197

Filed under:

I get Segmentation Fault when I try to run my program. Can someone please help me find out what Im doing wrong?

Compiling with this:

    g++ sms_out.cpp -o sms_out
    g++ -c -fPIC SMSDispatch.cpp
    g++ -shared SMSDispatch.o -o libSMSDispatch.so

It should be a shared library and dynamic linking. I get Segmentation Fault when I try to run sms_out.

//sms_out.cpp

#include <iostream>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<string>
#include "SMSDispatch.h"
using namespace std;

string sms = "";

void sendSMS(string sms)
{
  SMSDispatch* sPtr=0;
  sPtr->sendSMS(sms);
}

int main(int argc, char *argv[])
{
  if(argv[1])
  {
    string input = argv[1];
    string test = "--author";


 if(input == test)
    {
      cout << "s149113" << endl;
      return 0;
    }
  }

  string line = "";
  string file = "sms_out.txt";
  ifstream myfile(file.c_str());


while(getline(myfile, line))
{
      string idnr, landcode, number, error;
      istringstream linestream(line);

      unsigned short errorcode;

      //Split the sentence
      getline(linestream, idnr, '\t');
      getline(linestream, landcode, ':');
      getline(linestream, number, '\t');
      getline(linestream, error);

  if(idnr == "") break;

  //Make string to int
    try
  {
    errorcode = atoi(error.c_str() );
  }
  catch (exception &)
  {
  }
  //Put together landcode and tlfnumber
  string nr = landcode + number;

  string txt = "Thank you for your vote!";
  if(errorcode == 100) txt = "Invalid question, please try again";
  else if(errorcode == 110) txt = "Sorry, only one vote pr. number";
  else if(errorcode == 200) txt = "Invalid alternative, please try again";
  else if(errorcode == 300) txt = "Missing a statement after other, please try again";
  else if(errorcode == 999) txt = "An error occurred, please try again";

  sms += "{\"ID\":" + idnr + ",\"nr\":" + nr + ",\"txt\":" + "\"" + txt + "\"" + "}\n";
  }
  cout << sms << endl;
  sendSMS(sms);

}

//SMSDispatch.h

#include <string>
#ifndef SMSDISPATCH_H
#define SMSDISPATCH_H
using namespace std;

class SMSDispatch{
  public:
  virtual void sendSMS(string json);
  };
#endif

//SMSDispatch.cpp

 #include <iostream>
 #include <fstream>
 #include "SMSDispatch.h"
 using namespace std;

/*virtual*/void SMSDispatch::sendSMS(string json)
{
  ofstream myfile;
  myfile.open ("sms_out.log");
  myfile << json;
  myfile.close();
}

int main()
{

}

© Stack Overflow or respective owner

Related posts about c++