函数未在作用域 (C++) 中声明

Function not declared in the scope (C++)

本文关键字:声明 C++ 作用域 函数      更新时间:2023-10-16

我一直在研究这个莫里斯-普拉特算法来将子字符串与文本匹配,但我在如何在实际函数中声明失败函数时遇到了麻烦,这样编译器就不会抱怨。我有大约 2 个小时来完成这个。所以请尽快帮助我:/

int KMPmatch(const string& text, const string& pattern)
{
  int n = text.size();
  int m = pattern.size();
  std::vector<int> fail = computeFailFunction(pattern);
  int i = 0;
  int j = 0;
  while (i < n)
  {
    if (pattern[j] == text[i])
    {
      if (j == m-1) return i-m+1;
      i++; j++;
    }
    else if (j > 0) j = fail[j-1];
    else i++;
  }
  return -1;
}
//KMPFailure function
std::vector<int> computeFailFunction(const string& pattern)
{
  std::vector <int> fail(pattern.size());
  fail[0] = 0;
  int m = pattern.size();
  int j = 0;
  int i = 1;
  while (i < m)
  {
    if (pattern[j] == pattern[i])
    {
      fail[i] = j+1;
      i++; j++;
    }
    else if (j > 0)
    {
      j = fail [j-1];
    }
    else
    {
      fail[i]= 0;
      i++;
    }
  }
  return fail;
}

std::vector<int> computeFailFunction(const string& pattern);放在int KMPmatch(const string& text, const string& pattern)前面。

或者将函数声明放入头文件中并包含在源文件中,这就是具有多个源文件的项目所做的。