C++ 分段错误,共享库

C++ 分段错误,共享库,c++,C++,当我试图运行我的程序时,我遇到了分段错误。有人能帮我找出我做错了什么吗 使用以下命令进行编译: g++ sms_out.cpp -o sms_out g++ -c -fPIC SMSDispatch.cpp g++ -shared SMSDispatch.o -o libSMSDispatch.so 它应该是一个共享库和动态链接。当我试图运行sms_时,我遇到了分段错误 //sms_out.cpp #include <iostream> #include&l

当我试图运行我的程序时,我遇到了分段错误。有人能帮我找出我做错了什么吗

使用以下命令进行编译:

    g++ sms_out.cpp -o sms_out
    g++ -c -fPIC SMSDispatch.cpp
    g++ -shared SMSDispatch.o -o libSMSDispatch.so
它应该是一个共享库和动态链接。当我试图运行sms_时,我遇到了分段错误

//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”
使用名称空间std;
字符串sms=“”;
无效发送短信(字符串短信)
{
SMSDispatch*sPtr=0;
sPtr->sendSMS(sms);
}
int main(int argc,char*argv[])
{
if(argv[1])
{
字符串输入=argv[1];
字符串测试=“--author”;
如果(输入==测试)
{

cout取消引用
NULL
指针将导致分段错误:

void sendSMS(string sms)
{
  SMSDispatch* sPtr=0;
  sPtr->sendSMS(sms);
}
我看不出使用动态分配对象的原因,因此建议更改为:

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

取消引用
NULL
指针将导致分段错误:

void sendSMS(string sms)
{
  SMSDispatch* sPtr=0;
  sPtr->sendSMS(sms);
}
我看不出使用动态分配对象的原因,因此建议更改为:

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

在sms_out.cpp中的
sendSMS
函数中,声明一个指针并将其初始化为null pointer(
0
)。下一行尝试通过该指针访问对象并调用成员函数。由于指针为null(这意味着它没有指向有效的对象),此操作将失败,并出现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=新SMSDispatch;
在第一种情况下,你也可以这样做

SMSDispatch dp;
dp.sendSMS(sms);
在secons情况下,在不再需要对象后,需要调用
delete sptr;

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

你要么需要

  • 将SMSDispatch.cpp添加到g++调用中,以直接绑定代码或
  • 从SMSDispatch.cpp构建共享库后,通过将
    -lSMSDispatch
    添加到g++选项,重新链接共享库

在sms_out.cpp中的
sendSMS
函数中,您声明一个指针并将其初始化为null pointer(
0
)。下一行尝试通过该指针访问对象并调用成员函数。由于指针为null(这意味着它没有指向有效的对象),此操作将失败,并出现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=新SMSDispatch;
在第一种情况下,你也可以这样做

SMSDispatch dp;
dp.sendSMS(sms);
在secons情况下,在不再需要对象后,需要调用
delete sptr;

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

你要么需要

  • 将SMSDispatch.cpp添加到g++调用中,以直接绑定代码或
  • 从SMSDispatch.cpp构建共享库后,通过将
    -lSMSDispatch
    添加到g++选项,重新链接共享库

调试器告诉您SEGFULT发生的确切位置是什么?原始代码转储不是一种有效的提问方式。学习使用调试器(阅读教程、买书、上课);然后缩小问题范围并提出特定问题。我看不到您链接到
libSMSDisplay。因此
。如果需要,请替换
(argv[1])
使用
if(argc>1)
。如果
argc,调试器会告诉您segfault发生的确切位置吗?原始代码转储不是一种有效的提问方式。学习使用调试器(阅读教程、买书、上课);然后缩小您的问题范围并提出一个具体问题。我看不到您链接到
libSMSDisplay.so
。请将
if(argv[1])
替换为
if(argc>1)
。如果像SMSDispatch*sPtr=&SMSDispatch;??
&SMSDispatch
这样的argc无效,因为
SMSDispatch
是一种类型。您需要该类型的实例。根据需要,您可以执行(1)
SMSDispatch dp;sPtr=&dp;
或(2)
sPtr=new SMSDispatch;
(1),您也可以执行
SMSDispatch dp;dp.sendSMS(sms);
,以防(2)您不再需要该对象后需要调用
delete sptr;
。啊,好的,谢谢,但我尝试编写SMSDispatch dp;SMSDispatch*sptr=&dp;sptr->sendSMS(sms);但随后我遇到了一个错误:未定义对“vtable for SMSDispatch”的引用,这是因为您未链接到生成的libSMSDispatch。因此,请按如下方式编译主程序:
g++sms_out.cpp-o sms_out-lSMSDispatch
(请参阅其他选项)让我知道这是否有效,我会更新我的答案,但我刚刚编译成功像SMSDispatch*sPtr=&SMSDispatch;??
&SMSDispatch
这样的“找不到-lSMSDispatch”无效,因为
SMSDispatch
是一种类型。您需要该类型的实例。根据需要,您可以执行以下操作:(1)
SMSDispatch dp;sPtr=&dp;
或(2)
sPtr=新的SMSDispatch;
(1),您也可以执行
SMSDispatch dp;dp.sendSMS(sms);
,以防(2)您不再需要该对象后需要调用
delete sptr;
。啊,好的,谢谢,但我尝试编写SMSDispatch dp;SMSDispatch*sptr=&dp;sptr->sendSMS(sms);但随后我遇到了一个错误:未定义对“vtable for SMSDispatch”的引用,这是因为您未链接到生成的libSMSDispatch。因此,请按如下方式编译主程序:
g++sms_out.cpp-o sms_out-lSMSDispatch
(请参阅其他选项)让我知道这是否有效,然后我更新我的