为什么下一个代码不能编译?

时间:2016-11-30 15:36:10

标签: c++ gio glibmm

如何正确地重写下一个来源?它是GLib驱动的IRC-bot的一部分。编译器崩溃并出现下一个错误:

src/irc.cpp:20:9: error: cannot call member function ‘Glib::ListHandle<Glib::RefPtr<Gio::InetAddress> > Gio::Resolver::lookup_by_name(const Glib::ustring&, const Glib::RefPtr<Gio::Cancellable>&)’ without object
     ),

来源:

#include "includes.hpp" // Just including all the files

Glib::RefPtr<Gio::Socket> ircSock; // Our socket

void ircInit() { // Init-function
  try {
    ircSock = Gio::Socket::create( // Creating socket
      Gio::SocketFamily::SOCKET_FAMILY_IPV4,
      Gio::SocketType::SOCKET_TYPE_STREAM,
      Gio::SocketProtocol::SOCKET_PROTOCOL_TCP
    );

    ircSock->connect( // Problematic code
      Gio::InetSocketAddress::create(
        Gio::Resolver::lookup_by_name(
          "irc.freenode.net", // For-example
          Gio::Cancellable::create()
        ),
        6667
      ),
      Gio::Cancellable::create()
    );
  } catch(const Glib::Error& e) {
    std::cerr << "IRC: Error: " << e.what() << std::endl; // Error-reporting
  }
}

1 个答案:

答案 0 :(得分:3)

根据documentation,这是成员函数,因此,您需要构造对象来调用它,而您尝试将其称为静态函数。

正确的修复方法是创建Gio::Resolver对象,并在创建的对象上调用此方法。