0

I have an issue with linker errors attempting to overload << for cout, which I seemed to be going back and forth between Stackoverflow and related websites, and adjusting the code based on the other threads, with no success. Please note that I am very new to overloading, and just did many of the Math operator overloading in this program, which also took a lot of back and forward, but I think is now working...

Anyway, I was attempting to place the definition inside the class in the header file as follows:

std::ostream& operator<<(std::ostream&, const Vector2&);

And the definition in the CPP file:

    std::ostream& operator<<(std::ostream& os, const Vector2& VInput)
{
    std::string TempString = "{ " + std::to_string(VInput.x) + ", " + std::to_string(VInput.y) + " }";
    os << TempString;
    return os;
}

However I was getting errors about not being able to find the definition. I then read to move the declaration out of the class, so the definition as it is above, is now in the header file, but not encapsulated inside the class brackets.

This stopped the undefined error, but now I get a LNK2019 error telling me that there is an unresolved external.

I then found another person saying to put the definition with the the declaration as one, but when I do that I get a LNK2005 error telling me the function has been defined multiple times, I believed as the Include for this header is in several locations in the project.

I am guessing I am doing something very simple, or else what I am trying to do is just bad, but could someone let me know based on the code above and where I am placing it what could be causing these errors?

  • 1
    You need to make the function a `friend`, otherwise it will be a member function. – Some programmer dude Nov 01 '16 at 05:32
  • By the way, you don't need the temporary string, just output directly to `os` and return its result. Like `return os << "{ " << VInput.x << ...` – Some programmer dude Nov 01 '16 at 05:34
  • The history of your trials and tribulations don't make for a useful question. Please post a [MCVE](http://stackoverflow.com/help/mcve) showing just one case but including exactly what you did, and copy and paste the error messages that were caused by the code you posted. The code you posted in this question is OK, you must have made a mistake in code that you didn't post, or you omitted important context. See [this thread](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) for help with "undefined reference" errors. – M.M Nov 01 '16 at 05:56
  • Also see [this thread](http://stackoverflow.com/questions/4421706/operator-overloading) for correct examples of operator overloading. – M.M Nov 01 '16 at 05:57
  • Some programmer dude - Currently, the declaration is not inside the class so friend does not work. I put it back into the class and added friend, which intellisense was happy with, but building the project had the same error I currently have, LNK2019 unresolved external. M.M - the discussion as to what I did was exactly what you were asking for. I posted my one example, the exact code I was using and where I was using it, along with the errors I was getting so I am not sure why that is a bad question. Also, that reference link gave me no information whatsoever on identifying what the issue is. – Vaengence Nov 01 '16 at 08:32
  • Nobody here has a crystal ball. You made a mistake somewhere , but the pieces of program you posted are OK. This is why it is recommended to post a *complete* example. Then someone will be able to point out your error. There are too many possibilities to list them all here. Luckily someone has listed them all already, in the first link I provided. You either need to go through the list more carefully, or post a complete example. Also, you have not posted the error message. You posted a code plus a text description of the error. The exact error message may contain clues you don't realize. – M.M Nov 01 '16 at 08:58

0 Answers0