标识符未定义

时间:2013-10-19 12:28:10

标签: c++ visual-c++ visual-studio-2012

我使用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;
    }

3 个答案:

答案 0 :(得分:3)

从更新2开始,在缩小问题范围后,我们可以很容易地发现函数addWord末尾缺少一个大括号。编译器永远不会明确地识别出这样的语法错误。相反,它将假设缺少的函数定义位于某个其他目标文件中。链接器会抱怨它,因此直接将其归类为identifier is undefined的广泛错误短语之一。合理地,因为使用当前语法,下一个函数定义(在本例中为ac_search)将包含在addWord范围内。因此,它不再是一个全球性的功能。这就是为什么编译器不会在addWord之外看到此函数的原因,并且会抛出此错误消息,指出没有这样的函数。关于编译器和链接器的详细说明可以在article

中找到

答案 1 :(得分:0)

你错过了一个函数声明吗?

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之前添加它。

答案 2 :(得分:-4)

您可能也会遗漏using namespace std;