【问题标题】:Why next code does not compile?为什么下一个代码无法编译?
【发布时间】:2025-11-27 22:10:01
【问题描述】:

我如何正确地重写下一个来源?它是 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
  }
}

【问题讨论】:

  • 因为它是一个成员函数。你不能像静态函数一样调用它。

标签: c++ gio glibmm


【解决方案1】:

根据documentation,这是成员函数,所以,你需要构造对象来调用它,而你试图像静态函数一样调用它。

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

【讨论】:

  • src/irc.cpp:19:18: error: request for member ‘lookup_by_name’ in ‘resolver’, which is of non-class type ‘Gio::Resolver()’(对不起,我是 C++ 新手。当前版本:here