【问题标题】:how to call template function in class body?如何在类体中调用模板函数?
【发布时间】:2012-08-05 12:54:17
【问题描述】:

我写了followig函数,它在向量中搜索并找到指针在向量中的位置,如果找到成功,则返回迭代器:

template<class InputIterator>
InputIterator MainCore::findDeviceAccordingToIP ( const char * value )
{
    std::vector<Device *>::iterator first,last;
    first = this->devList->begin();
    last = this->devList->end();
    Device *temp;

    for ( ;first!=last; first++){
        temp = *first;

        if ( strcmp(temp->endpoint->IPAddress.c_str(),value) == 0)
        {
            return first;
            break;
        }
    }

    //return false;
}

cpp 文件中的上述代码我将以下代码放在 *.h 文件中的 MainCore 类中:

template<class InputIterator>    
InputIterator findDeviceAccordingToIP (const char *value );

现在当我调用另一个函数时,例如:

this->findDeviceAccordingToIP("192.168.2.11");

现在我有两个问题:

  1. 当我编译它时,我得到以下错误:

    error: no matching function for call to MainCore::findDeviceAccordingToIP(const char [13])

  2. 我如何获得 return T 和布尔值和迭代器?

【问题讨论】:

  • 看起来您将模板定义放在 .cpp 文件中。你不能那样做。在 .h 中声明并定义您的模板
  • 代码中不需要模板 - 您可能想要的返回类型是 std::vector&lt;Device*&gt;::iterator
  • 尝试使用 std::string 而不是 const char *。它更快、更高效。

标签: c++ templates stl vector iterator


【解决方案1】:

关于问题1,函数模板的模板参数对函数参数没有依赖,所以需要显式指定类型:

this->findDeviceAccordingToIP<SomeIteratorType>("192.168.2.11");

此外,模板代码应该在头文件中或在头文件中包含的文件中。它必须直接或间接地包含在客户端代码中。

请注意,您可以通过调用 std::find_if 和合适的仿函数来替换整个函数。

至于问题2,不清楚你的意思。

【讨论】:

    【解决方案2】:

    上面回答了第一个问题。

    第二个问题:

    您想要返回两种类型的值(迭代器和布尔值),这在 C++ 中是不可能的。你可以有一些可能性

    使迭代器成为引用参数并在此返回结果。使返回值返回true/false,表示搜索成功。

    返回last,以防您找不到该值。这可以很容易地像这样编码。

    template<class InputIterator> InputIterator MainCore::findDeviceAccordingToIP ( const char * value ) 
    {     
        std::vector<Device *>::iterator first,last;     
        first = this->devList->begin();     
        last = this->devList->end();     
        Device *temp;      
        for ( ;first!=last; first++)
        {         
            temp = *first;          
            if ( strcmp(temp->endpoint->IPAddress.c_str(),value) == 0)
            {  
                break;         
            }     
        }      
    
        return first; 
    } 
    

    【讨论】:

      【解决方案3】:

      我将其更改为以下函数,而不是迭代器,我将迭代器的内容作为Device * 传递:

      bool MainCore::findDeviceAccordingToIP (const std::string& _IPAddress, Device * devPtr )
      {
          std::vector<Device *>::iterator first,last;
          first = this->devList.begin();
          last = this->devList.end();
          Device *temp;
      
          for ( ;first!=last; first++){
              temp = *first;
      
              if ( temp->endpoint->IPAddress == _IPAddress )
              {
                  devPtr = *first;
                  return true;
              }
          }
      
          return false;
      }
      

      所以我得到布尔结果并通过 ref 得到向量内容的结果,它工作正常。

      【讨论】:

      • 这样你就不会在函数范围之外(在调用者中)修改devPtr;您需要通过引用传递指针:bool MainCore::findDeviceAccordingToIP (const std::string&amp; _IPAddress, Device* &amp;devPtr )
      • 但我需要将结果作为布尔值。
      • 确实,您得到的结果是布尔值,但由于方法声明中缺少 &amp; 符号,您没有在函数外部获得 devPtr 值。
      猜你喜欢
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 2014-09-23
      • 2012-10-24
      • 2020-09-10
      • 2012-03-06
      相关资源
      最近更新 更多