Nick
Nick

Reputation: 19684

XCODE: Linking issue when using string streams <sstream>

I don't understand why XCode is running into a linking issue when using

    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;
}

Undefined symbols for architecture x86_64: "CouchServer::create_base_uri(std::__1::basic_string, std::__1::allocator >, int, std::__1::basic_string, std::__1::allocator >)", referenced from: CouchServer::get_document_by_id(std::__1::basic_string, std::__1::allocator >) in couch_server.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can someone help me out?

Upvotes: 1

Views: 670

Answers (1)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24163

You need to define the member function relative to the class:

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

Instead, you're defining a free function:

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

Interestingly, it compiles as it doesn't refer to any other members of the class. It may be better to actually make it a free function! If it is currently a private member you can put it in an anonymous namespace instead. If it's useful in other places, you could make it into a utility function.

Upvotes: 1

Related Questions