N
N
Nikita Gusakov2014-04-12 22:29:20
C++ / C#
Nikita Gusakov, 2014-04-12 22:29:20

Why doesn't operator overloading work?

For educational purposes, I am writing a class on sys / socket, to work with sockets, in fact.
https://github.com/nkt/cpp-socket/blob/master/src/... is the method.
https://github.com/nkt/cpp-socket/blob/master/chat... is its usage.

Xcode issues

Undefined symbols for architecture x86_64:
  "Socket& operator<<<char const [16]>(Socket&, char const (&) [16])", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)


Here's an example that works just fine:
#include <iostream>
#include <string>

class test
{
public:
  void send(std::string str);
  template <class T>
    friend test &operator <<(test &tst, T &message);
};

void test::send(std::string str)
{
  std::cout << str;
}

template <class T>
test &operator <<(test &tst, T &message)
{
  tst.send(std::string(message));
  return tst;
}

int main()
{
  test t;

  t << "hello world!\n";
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bogolt, 2014-04-12
@hell0w0rd

Template classes and methods cannot be defined in cpp files. Move all the boilerplate code to a header file and it will work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question