1

我不明白为什么 XCode 在使用时会遇到链接问题

    string create_base_uri(string host, int port, string dbname){
      std::ostringstream ostr; //output string stream
      ostr << port; //use the string stream just like cout,
      string port_string = ostr.str();
      return "http://" + host + ":" + port_string + "/" + dbname;
}

架构 x86_64 的未定义符号:“CouchServer::create_base_uri(std::__1::basic_string, std::__1::allocator >, int, std::__1::basic_string, std::__1::allocator >)”,引用自:couchServer::get_document_by_id(std::__1::basic_string, std::__1::allocator >) in couch_server.o ld:符号未找到架构 x86_64 clang:错误:链接器命令失败,退出代码1(使用 -v 查看调用)

有人可以帮我吗?

4

1 回答 1

1

您需要定义相对于类的成员函数:

string CouchServer::create_base_uri(string host, int port, string dbname) {
    //..
}

相反,您正在定义一个自由函数:

string create_base_uri(string host, int port, string dbname) {
    //..
}

有趣的是,它可以编译,因为它不引用该类的任何其他成员。将其设为免费功能可能会更好!如果它当前是私有成员,则可以将其放在匿名命名空间中。如果它在其他地方有用,你可以把它变成一个实用函数。

于 2013-04-14T19:24:54.767 回答