1 year ago

#75126

test-img

Tiko7454

Linker cannot find overloaded class operator whith universal type operand

I wanted to overload + operator for my other class and I reached a linker error. I shortened my code to make it easy to review

num.h

#ifndef NUM_H
#define NUM_H

#include <iostream>
#include <fstream>

namespace ecail {
    class num {
        private:
            int a{};
        public:
            template<class T>
            int operator+(T x);
    };
}

#endif // NUM_H

num.cpp

#include "num.h"

template<class T>
int ecail::num::operator+(T x) {
    return a + x;
}

And my linker cannot find the overloaded operator

main.cxx

#include "num.h"


int main() {
    ecail::num x{};
    std::cout << (x + 2);
}

enter image description here

Here the class is static type (I mean it's not like vector its just num) whereas the operator + recieves as left operand num and as right operand - a number type (in this case), for instance int, double, long long etc. number overflow is not a problem because its just a prototype.

This question is considered as a duplicate but its not. the question which the system gave to me is about classes like vector (as mentioned above)

when I assemble them in one file everything works

c++

class

templates

operator-overloading

linker-errors

0 Answers

Your Answer

Accepted video resources