首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重载模板中的socket.h方法

重载模板中的socket.h方法
EN

Stack Overflow用户
提问于 2020-11-28 18:46:56
回答 1查看 45关注 0票数 0

我正在尝试使用模板来创建一个泛型接口类型类。因此,我希望模板类中有一个"connect()“函数。但是,当我为套接字接口实现模板时,编译器认为我指的是父函数的connect()函数,而不是socket.h库的一部分connect()函数。如何指定如何在实现类中使用socket.h库连接()函数?请参阅下面的最小示例

代码语言:javascript
复制
#include <string>
#include <sys/socket.h>
#include <netdb.h>
template <class Input, class Output> class Parent {
public:
  Parent() : _isConnected(false) {}
  bool connect() {
    return _isConnected || (_isConnected = childConnect());
  }
private:
  bool _isConnected;
  virtual bool childConnect() = 0;
};
class Implementation : public Parent<std::string, std::string> {
  bool childConnect() {
    int sockfd;
    struct addrinfo *p;
    connect(sockfd, p->ai_addr, p->ai_addrlen);
    return true;
  }
};
int main() {
  Implementation ii;
  return 0;
}

和编译器错误

代码语言:javascript
复制
g++ main.cc
main.cc: In member function ‘virtual bool Implementation::childConnect()’:
main.cc:22:46: error: no matching function for call to ‘Implementation::connect(int&, sockaddr*&, socklen_t&)’
     connect(sockfd, p->ai_addr, p->ai_addrlen);
                                              ^
main.cc:11:8: note: candidate: bool Parent<Input, Output>::connect() [with Input = std::__cxx11::basic_string<char>; Output = std::__cxx11::basic_string<char>]
   bool connect() {
        ^~~~~~~
main.cc:11:8: note:   candidate expects 0 arguments, 3 provided
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-28 18:50:29

您可能完全有资格打电话:

代码语言:javascript
复制
::connect(sockfd, p->ai_addr, p->ai_addrlen);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65053295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档