Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifier is undefined

I wrote the following code in C++ using VS2012 Express.

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;
}

I get the error identifier "ac_search" is undefined at the function invoking line. I am a bit confused here. because the function ac_search is declared as a global (not inside any container). Why can't I call it at this place? Am I missing something?

Update

I tried ignore irrelevant code and then included it gradually and found that everything is fine until I include the outer loop of ac_search I get the aforementioned error. here is updated code of the function 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;
            }
    //    }
    //}
}  

Update 2

I think the error has something to do with the function addWord which belongs to the class Trie. When I commented out this function, I did not get the error anymore.

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;
    }
like image 360
Hawk Avatar asked Oct 20 '22 22:10

Hawk


1 Answers

From the update 2 and after narrowing down the problem scope, we can easily find that there is a brace missing at the end of the function addWord. The compiler will never explicitly identify such a syntax error. instead, it will assume that the missing function definition located in some other object file. The linker will complain about it and hence directly will be categorized under one of the broad the error phrases which is identifier is undefined. Reasonably, because with the current syntax the next function definition (in this case is ac_search) will be included under the addWord scope. Hence, it is not a global function anymore. And that is why compiler will not see this function outside addWord and will throw this error message stating that there is no such a function. A very good elaboration about the compiler and the linker can be found in this article

like image 55
Hawk Avatar answered Oct 27 '22 11:10

Hawk