【问题标题】:C++ reading string segmentation faultC++ 读取字符串分段错误
【发布时间】:2015-02-19 05:47:58
【问题描述】:

我正在编写一个程序,它将输入一个字母消息,并使用该类构建莫尔斯电码字符串,然后输出该字符串。我已经编写了所有方法并且程序编译得很好。但是,当我输入临时字符串并按回车时。我得到一个分段错误。我似乎找不到问题所在。如果有人能看到问题所在,我将非常感谢您的帮助。谢谢。

#include <string>
#include <vector>
#include <fstream>

using namespace std;

class Code
{
public:
  Code();  // Default constructor - loads and uses morse code
  string decode(vector< string> message);  // decodes a message 
  string encode(vector<char> message); // encodes a message

private:
  vector<string> codewords; // this is a codeword vector parallel to A-Z
  vector<char> alpha; // this is the vector for A-Z
  vector<char>  alphacode(); // function builds the vector alpha - A B C etc.
  vector<string>  morsecode(); // function builds the vector codewords containing morse code
  char decode(string c); //returns the character for the codeword c.
  string encode(char c);
};

Code::Code() {
    alpha = alphacode();
    codewords = morsecode();
}

string Code::decode(vector< string> message) {
    string temp;
    for (int i=0; i < message.size(); i++) {
        temp += decode(message[i]);
    }
    return temp;
}


string Code::encode(vector<char> message)
{
    string temp;
    for (int i=0; i<message.size(); i++)
    {
        temp+=encode(message[i]);
    }
    return temp;
}

vector<string> Code::morsecode()
{ // This function returns a vector containing the morse code
 vector<string> temp(28);
 temp[0] =".-";
 temp[1] ="-...";
 temp[2] ="-.-.";
 temp[3] ="-..";
 temp[4] =".";
 temp[5] ="..-.";
 temp[6] ="--.";
 temp[7] ="....";
 temp[8] ="..";
 temp[9] =".---";
 temp[10] ="-.-";
 temp[11] =".-..";
 temp[12] ="--";
 temp[13] ="-.";
 temp[14] ="---";
 temp[15] =".--.";
 temp[16] ="--.--";
 temp[17] =".-.";
 temp[18] ="...";
 temp[19] ="-";
 temp[20] ="..-";
 temp[21] ="...-";
 temp[22] =".--";
 temp[23] ="-..-";
 temp[24] ="-.--";
 temp[25] ="--..";
 temp[26] =".......";
 temp[27] ="x";
 return temp;
}

vector<char> Code::alphacode()
{// This returns a vector containing the alphabet a-z and " "
 vector<char> temp;
 for (char c='A'; c<='Z'; c++)
      temp.push_back(c);
 temp.push_back(' ');
 temp.push_back('.');
 return temp;
}

char Code::decode(string c)
{
    for (int i = 0; i < alpha.size(); i++) {
        if(c == codewords[i]) {
            return alpha[i];
        }    
    }
}

string Code::encode(char c)
{
    for (int i=0;i<codewords.size();i++)
    {
        if (c==alpha[i])                
        {
            return codewords[i];                    
        }
    }
}
int main()
{

 vector<char> message;   
    string temp; 

    getline(cin, temp);  
        for (int i=0; i <temp.length(); i++)
        {
            message.push_back(temp[i]);
        }

    Code C;
    cout << C.encode(message) << endl;
}

【问题讨论】:

    标签: c++ string oop vector segmentation-fault


    【解决方案1】:

    您的 alphacodes 和 morsecodes 仅适用于大写字母,因此此函数返回 null 导致出现问题。

    string Code::encode(char c)
    {
        for (int i=0;i<codewords.size();i++)
        {
            if (c==alpha[i])                
            {
                return codewords[i];                    
            }
        }
    }
    

    您对c==alpha[i] 的检查应该忽略大小写,或者您的字母代码也应该有小字母代码。您的摩尔斯电码也应该有小字母的代码,并且您将 A-Z 映射到 0-28 的检查应该容纳小字母。

    请记住,小写字母与大写字母的 ASCII 码不同。

    【讨论】:

      【解决方案2】:

      下面的函数末尾没有return语句。

      string Code::encode(char c)
      {
          for (int i=0;i<codewords.size();i++)
          {
              if (c==alpha[i])                
              {
                  return codewords[i];                    
              }
          }
      
          // What should happen if execution gets to this line?
      }
      

      如果您的代码有可能到达函数的末尾,您将遇到未定义的行为。这可能是您问题的根源。

      【讨论】:

        猜你喜欢
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 2017-09-07
        相关资源
        最近更新 更多