0

I'm trying to overload operators for class that are buried in namespace's, but i have plenty of CE.

//main.cpp
#include "ss.h"
#include "ss.cpp"

nn::ss a(2);
nn::ss b(3);

int main(){
    //nn::ss c=a+b;
    return 0;
}
//ss.h
namespace nn{
    class ss;
}
nn::ss operator+(const nn::ss &a, const nn::ss &b);
namespace nn{
    class ss{
        private:
        int v;
        public:
        ss(int v);
        friend ss ::operator+(const ss &a, const ss &b);
    };
}
 
//ss.cpp
using namespace nn;
ss::ss(int v) : v(v){}
ss operator+(const ss &a, const ss &b){
    return ss(a.v+b.v);
}

I've tried using several stack questions answers but that's all i have figured out yet. None of questions I've found contained overloading operators like + or =, the method I tried using here worked for << operators.

Ramzel
  • 38
  • 4
  • 2
    If by "CE" you mean compiler errors, then please copy-paste them (as text) into the question itself. And add comments in the code on the lines where you get them. – Some programmer dude May 07 '22 at 22:01
  • *"i have plenty of CE"* -- Go one at a time. Focus on the first, since the others might be artifacts of the first (assuming "CE" means "compilation errors"). Note that the error message for the first error might extend over several lines. Messages that start with "Note:" are usually part of the error that precedes them. – JaMiT May 07 '22 at 22:02
  • 1
    As a possible *guess* about the problem, I suggest you learn about [*Argument Dependent Lookup (ADL)*](https://en.cppreference.com/w/cpp/language/adl). You don't need the global declaration of `operator+`, only the one declared as `friend`. I also suggest, since it's so simple, that you defined (implement) it inline in the header file where you now only declare it. – Some programmer dude May 07 '22 at 22:04
  • 4
    Why do you `#include "ss.cpp"` ? – paolo May 07 '22 at 22:04

0 Answers0