0

From my last edit, I managed to fix the two errors plaguing my program, but this error appeared and seems to persist.

error: operator<<(std::__1::basic_ostream<char, std::__1::char_traits >&, Poly const&)", referenced from: _main in MYPROGRAM-20b615.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation).

I have found the line in my code that is causing my program which is

friend ostream &operator<<(ostream &Out, const Poly &);

I don't know how to address it though.

CinCout
  • 9,486
  • 12
  • 49
  • 67
Shifty
  • 3
  • 3
  • 2
    Your operator does not appear to be part of a class.... – ChrisMM Dec 14 '21 at 04:18
  • This looks like one of those pesky copy/paste errors that all of us make. The difference is that people with more experience will see the error message, maybe even read the code, and say "oh silly me" while immediately fixing it. You're at the point where you see this and panic. To get from where you are now to where you want to be some day, consider taking time to read your error messages carefully, and practice looking at your code as if you're not the one who wrote it. It also helps to develop stuff in small chunks, and ensure between each chunk you have a program that compiles. – paddy Dec 14 '21 at 04:28
  • 1
    As a side-note, your function leaks memory by allocating a local dynamic array `prod`, never using it, and never storing or deleting the pointer. If your `Poly` class is using dynamic allocation in its constructor, you'd better make sure you're also following the [Rule of Three](https://stackoverflow.com/q/4172722/1553090). – paddy Dec 14 '21 at 04:32

1 Answers1

1

You are using the operator as if it's part of the Poly class, but from your code, it is not. It should look something like

template <typename T>
Poly<T> Poly<T>::operator*(const Poly<T> &rhs) const

Or course, this assumes that the operator is declared in the class definition.

Since a multiplication with operator* should not modify the object itself, it should be marked as a const function; also prevents accidentally changing the object.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
  • 1
    Worth mentioning that the function should really be const-qualified. – paddy Dec 14 '21 at 04:30
  • Since Poly is a template, it should be: `template Poly Poly::operator*(const Poly &rhs) const { ... }`. Note the "Poly::operator*" as opposed to "Poly::operator*" – Luis Guzman Dec 14 '21 at 04:52
  • @LuisGuzman, yes, you're right. – ChrisMM Dec 14 '21 at 05:15
  • I have added your suggestion which eliminated the two errors but a new has appeared that states "operator<<(std::__1::basic_ostream >&, Poly const&)", referenced from: _main in MYPROGRAM-20b615.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)." I am not really famiilar with this error. – Shifty Dec 15 '21 at 00:12
  • @Shifty, hard to tell from just that, but I'm assuming that you defined the function in a source file instead of header, which isn't allowed for templates. Would recommend asking a new question though, since it's a different problem than what the post was about. There are several topics on SO about that error though. – ChrisMM Dec 15 '21 at 00:43