5

我使用 VS2012 Express 用 C++ 编写了以下代码。

void ac_search(
    uint num_patterns, uint pattern_length, const char *patterns, 
    uint num_records, uint record_length, const char *records,
    int *matches, Node* trie) {

  // Irrelevant code omitted.
}    

vector<int> ac_benchmark_search(
    uint num_patterns, uint pattern_length,
    const char *patterns, uint num_records, uint record_length,
    const char *records, double &time) {

  // Prepare the container for the results
  vector<int> matches(num_records * num_patterns);
  Trie T;
  Node* trie = T.addWord(records, num_records, record_length);

  // error line
  ac_search(num_patterns, pattern_length, patterns, num_records,
            record_length, records, matches.data(), trie);    

  // Irrelevant code omitted.    
  return matches;
}

我在函数调用行收到错误identifier "ac_search" is undefined。我在这里有点困惑。因为该函数ac_search被声明为全局函数(不在任何容器内)。为什么我不能在这个地方打电话?我错过了什么吗?

更新

我尝试忽略不相关的代码,然后逐渐包含它,发现一切都很好,直到我包含ac_search我得到上述错误的外循环。这是函数的更新代码ac_search

void ac_cpu_string_search(uint num_patterns, uint pattern_length, const char *patterns, 
                       uint num_records, uint record_length, const char *records, int *matches, Node* trie)
{
    // Loop over all records
    //for (uint record_number = 0; record_number < num_records; ++record_number)
    //{
    //    // Loop over all patterns
        for (uint pattern_number = 0; pattern_number < num_patterns; ++pattern_number)
        {
            // Execute string search
            const char *ptr_record = &records[record_number * record_length];
            const char *ptr_match = std::strstr(ptr_record, &patterns[pattern_number * pattern_length]);

            // If pattern was found, then calculate offset, otherwise result is -1
            if (ptr_match)
            {
                matches[record_number * num_patterns + pattern_number] = static_cast<int>(std::distance(ptr_record, ptr_match));
            }
            else
            {
                matches[record_number * num_patterns + pattern_number] = -1;
            }
    //    }
    //}
}  

更新 2

我认为该错误与addWord属于该类的函数有关Trie。当我注释掉这个函数时,我没有再收到错误了。

Node* Trie::addWord(const char *records, uint num_records, uint record_length)
{

    // Loop over all records
    for (uint record_number = 0; record_number < num_records; ++record_number)
    {
        const char *ptr_record = &records[record_number * record_length];
        string s = ptr_record;
        Node* current = root;
        if ( s.length() == 0 )
        {
            current->setWordMarker(); // an empty word
            return;
        }

        for ( int i = 0; i < s.length(); i++ )
        {        
            Node* child = current->findChild(s[i]);
            if ( child != NULL )
            {
                current = child;
            }
                else
                {
                    Node* tmp = new Node();
                    tmp->setContent(s[i]);
                    current->appendChild(tmp);
                    current = tmp;
                }
                if ( i == s.length() - 1 )
                    current->setWordMarker();
        }
        return current;
    }  

void ac_search(
        uint num_patterns, uint pattern_length, const char *patterns, 
        uint num_records, uint record_length, const char *records,
        int *matches, Node* trie) {

      // Irrelevant code omitted.
    }    

    vector<int> ac_benchmark_search(
        uint num_patterns, uint pattern_length,
        const char *patterns, uint num_records, uint record_length,
        const char *records, double &time) {

      // Prepare the container for the results
      vector<int> matches(num_records * num_patterns);
      Trie T;
      Node* trie = T.addWord(records, num_records, record_length);

      // error line
      ac_search(num_patterns, pattern_length, patterns, num_records,
                record_length, records, matches.data(), trie);    

      // Irrelevant code omitted.    
      return matches;
    }
4

3 回答 3

8

从更新 2 和缩小问题范围后,我们可以很容易地发现函数末尾缺少一个大括号addWord。编译器永远不会显式识别这样的语法错误。相反,它将假定丢失的函数定义位于其他一些目标文件中。链接器会抱怨它,因此将直接归类为广泛的错误短语之一,即identifier is undefined. 合理地,因为使用当前语法,下一个函数定义(在这种情况下是ac_search)将包含在addWord范围内。因此,它不再是一个全局函数。这就是为什么编译器不会在外面看到这个函数addWord并会抛出此错误消息,指出没有这样的功能。关于编译器和链接器的一个很好的阐述可以在这篇文章中找到

于 2013-10-20T08:09:33.953 回答
1

您是否缺少函数声明?

void ac_search(uint num_patterns, uint pattern_length, const char *patterns, 
               uint num_records, uint record_length, const char *records, int *matches, Node* trie);

在执行 ac_benchmark_search 之前添加它。

于 2013-10-19T12:47:40.837 回答
-6

你可能也错过了using namespace std;

于 2014-12-29T02:06:33.900 回答