Identifier is undefined

209,763

Solution 1

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

Solution 2

Are you missing a function declaration?

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

Add it just before your implementation of ac_benchmark_search.

Share:
209,763
Hawk
Author by

Hawk

Live To Learn and Earn

Updated on July 09, 2022

Comments

  • Hawk
    Hawk almost 2 years

    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;
        }
    
  • Levente Kurusa
    Levente Kurusa over 10 years
    This shouldn't matter if the function is implemented before the other is implemented.
  • user1764961
    user1764961 over 10 years
    "if" is the important part.
  • Levente Kurusa
    Levente Kurusa over 10 years
    Looking at his code, it is ac_benchmark_search is implemented before ac_search, unless he posted it the wrong way.
  • user1764961
    user1764961 over 10 years
    Maybe he pasted here just these two functions (in the right order).
  • Hawk
    Hawk over 10 years
    @user1764961 I posted the same order as my code implementation
  • user1764961
    user1764961 over 10 years
    @hawk, no namespaces?
  • Hawk
    Hawk over 10 years
    @user1764961 I've used namespace using namespace concurrency; using std::string; using std::cout; using std::endl; using std::vector;