1
votes

i have a problem with overloading the addition. this is an example i made to test overloading, but i get a compiler error: "iso c++ forbids declaration of operator+ with no type"

i hope you can help me

    #include <iostream>

    template <typename T>  
    class foo
    {
        public:
            foo();
            T value;
            void setValue(T svalue);
            friend foo<T> operator+(foo<T> const &foo1, foo<T> const &foo2);
    };

    template<typename T>
    foo<T> operator+(foo<T> const &foo1, foo<T> const &foo2)
    {
        return foo1.value + foo2.value;
    }

    template <typename T>
    foo<T>::foo()
    {
        value = 0;
    }

    template <typename T>
    void foo<T>::setValue(T svalue)
    {
        value = svalue;
    }

    int
    main()
    {
        foo<int> ten;
        ten.setValue(10);
        foo<int> five;
        five.setValue(5);

        return 0;
    }

//edit: sorry, i changed the classname from kamehameha to foo, some kamehameha's were left...

//edit2: i added the return type, now the compiler says: "warning: friend declaration 'foo operator+(foo, foo))' declares a non-template function

'foo foo::operator+(const foo&, const foo&)' must take either zero or one argument

4

4 Answers

1
votes

Friend functions of template classes don't implicitly create template functions... so you can't actually implement the created function except inline. Try this:

template <typename T>  
class foo
{
    public:
        foo();
        T value;
        void setValue(T svalue);
        friend foo<T> operator+(foo<T> const &foo1, foo<T> const &foo2)
        {
            foo result;
            result.setValue(foo1.value + foo2.value);
            return result;
        }
};

You could also forward declare the function, so the friend declaration isn't creating a new function, but referring to one already known:

template <typename T>  
class foo;

template<typename T>
foo<T> operator+(foo<T> const &foo1, foo<T> const &foo2);

template <typename T>  
class foo
{
    public:
        foo();
        T value;
        void setValue(T svalue);
        friend foo<T> operator+<T>(foo<T> const &foo1, foo<T> const &foo2);
        {
            foo result;
            result.setValue(foo1.value + foo2.value);
            return result;
        }
};

template<typename T>
foo<T> operator+(foo<T> const &foo1, foo<T> const &foo2);
{
    foo result;
    result.setValue(foo1.value + foo2.value);
    return result;
}
0
votes
foo<T>::operator+(foo<T> const &foo1, foo<T> const &foo2)

You didn't write a return type for your operator function.

0
votes

Your code

template<typename T>
foo<T>::operator+(foo<T> const &foo1, foo<T> const &foo2)
{
    return foo1.value + foo2.value;
}

defines operator+ with no return type and as a member function of foo<T>. What you want is

template<typename T>
foo<T> operator+(foo<T> const &foo1, foo<T> const &foo2)
{
    return foo1.value + foo2.value;
}

i.e. not as a member function and returning a foo<T>.

0
votes

Your + operator as a two parameter style, should be

template<typename T>
foo<T> operator+(foo<T> const &foo1, foo<T> const &foo2)
{
    return foo1.value + foo2.value;
}

I removed foo<T>:: from the beginning of operator+.