2012-04-01 105 views
1

当我尝试运行我的程序时,出现分段错误。有人能帮我找出我做错了什么吗?分段错误,共享库

与此编译:

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

它应该是一个共享库和动态链接。当我尝试运行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() 
{ 

} 
+5

您的调试器告诉您关于段错误发生的具体位置? – Mat 2012-04-01 11:24:50

+2

原始代码转储不是提问的高效方式。学习使用调试器(阅读教程,购买书籍,参加课程);然后缩小您的问题并提出具体问题。 – 2012-04-01 11:26:59

+1

我没有看到你用'libSMSDisplay.so'链接。 – trojanfoe 2012-04-01 11:28:16

回答

3

在你sendSMS功能sms_out.cpp声明一个指针并对其进行初始化到零位(0)。下一行尝试通过该指针访问对象并调用成员函数。由于指针为空(这意味着它不指向一个有效的对象),该操作wilth一个seg.fault

void sendSMS(string sms) 
{ 
    SMSDispatch* sPtr=0; // this sets pointer to null 
         // assign the address of a valid `SMSDispatch` object instead 
    sPtr->sendSMS(sms); 
} 

未能修复它,福斯特你需要一个类型的实例。

根据你的需要,你可以做

  • SMSDispatch dp; sPtr=&dp;
  • sptr=new SMSDispatch;

在第一种情况下,你也可以同样做

SMSDispatch dp; 
dp.sendSMS(sms); 

在secons在你不需要后,你需要拨打delete sptr;再一次编辑对象。

此外,请注意,为了编译该程序,编译器将需要SMSDispatch::sendSMS函数的定义。通过包含SMSDispatch.h标头,您只提供声明

你要么需要

  • 加入-lSMSDispatch添加SMSDispatch.cpp为代码的直接包装或
  • 链接的G ++ invokation agains共享库G ++从建立共享库后选择SMSDispatch。cpp
+0

就像SMSDispatch * sPtr =&SMSDispatch; ?? – Veronic 2012-04-01 11:56:25

+0

由于'SMSDispatch'是一种类型,因此'&SMSDispatch'无效。你需要一个这种类型的实例。根据你的需要你可以做(​​1)'SMSDispatch dp; sPtr = &dp;'或(2)'sptr = new SMSDispatch;'在情况下(1),你可以做'SMSDispatch dp; dp.sendSMS(sms);',如果(2)你不需要这个对象,你需要调用'delete sptr;'。 – Attila 2012-04-01 13:22:08

+0

阿好,谢谢,但我试图写SMSDispatch DP; SMSDispatch * sPtr = &dp; sPtr-> sendSMS(sms);但后来我得到了一个错误:未定义的引用'vtable for SMSDispatch' – Veronic 2012-04-01 13:46:16

9

解引用一个指针NULL将导致一个segme ntation故障:

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

我看不到一个理由使用动态分配的对象,因此建议改为:

void sendSMS(string sms) 
{ 
    SMSDispatch sPtr; 
    sPtr.sendSMS(sms); 
}