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.

3
  • 2
    Your operator does not appear to be part of a class.... Commented Dec 14, 2021 at 4: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. Commented Dec 14, 2021 at 4: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. Commented Dec 14, 2021 at 4:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Worth mentioning that the function should really be const-qualified.
Since Poly is a template, it should be: template <typename T> Poly<T> Poly<T>::operator*(const Poly<T> &rhs) const { ... }. Note the "Poly<T>::operator*" as opposed to "Poly::operator*"
@LuisGuzman, yes, you're right.
I have added your suggestion which eliminated the two errors but a new has appeared that states "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Poly<int> 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, 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.