Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > перегрузка operator+


Автор: vinter 4.2.2007, 15:26
вообщем проблема, есть шаблон в нем обьявлен оператор
Код

template <class Type> class Matrix
{
....................

    friend Matrix<Type> operator+<>(const Type Variable,const Matrix<Type> SecObj);
...........
};

определение
Код

Matrix<Type> operator+(Type Variable, Matrix<Type> SecObj)
{
    Matrix<Type> Temp(SecObj.GetRows(), SecObj.GetColumns());
    for(int i = 0; i < SecObj.GetRows(); i++)
        for(int j = 0; j < SecObj.GetColumns(); j++)
            *(*(Temp.pMatrix) + i + j) += Variable
    return Temp;
}

ошибки
Код

d:\Programming\Open Projects\Matrix\Matrix.h(34) : error C2143: syntax error : missing ';' before '<'
        d:\Programming\Open Projects\Matrix\Matrix.h(38) : see reference to class template instantiation 'Matrix<Type>' being compiled
d:\Programming\Open Projects\Matrix\Matrix.h(34) : error C2460: 'operator`+'' : uses 'Matrix<Type>', which is being defined
d:\Programming\Open Projects\Matrix\Matrix.h(34) : error C2433: 'operator`+'' : 'friend' not permitted on data declarations
d:\Programming\Open Projects\Matrix\Matrix.h(34) : error C2238: unexpected token(s) preceding ';'
d:\Programming\Open Projects\Matrix\Matrix.h(277) : error C2904: '+' : name already used for a template in the current scope
        d:\Programming\Open Projects\Matrix\Matrix.h(34) : see declaration of 'operator`+''
d:\Programming\Open Projects\Matrix\Matrix.h(34) : error C2143: syntax error : missing ';' before '<'
        d:\Programming\Open Projects\Matrix\Main.cpp(6) : see reference to class template instantiation 'Matrix<Type>' being compiled
        with
        [
            Type=int
        ]
d:\Programming\Open Projects\Matrix\Matrix.h(34) : error C2460: 'operator`+'' : uses 'Matrix<Type>', which is being defined
        with
        [
            Type=int
        ]
d:\Programming\Open Projects\Matrix\Matrix.h(34) : error C2433: 'operator`+'' : 'friend' not permitted on data declarations
d:\Programming\Open Projects\Matrix\Matrix.h(34) : error C2238: unexpected token(s) preceding ';'
d:\Programming\Open Projects\Matrix\Main.cpp(21) : error C2677: binary '+' : no global operator found which takes type 'Matrix<Type>' (or there is no acceptable conversion)
        with
        [
            Type=int
        ]

при другом обьявлении
Код

template <class Type> class Matrix
{
....................

    friend Matrix<Type> operator+(const Type Variable,const Matrix<Type> SecObj);
...........
};

ошибки
Main.obj : error LNK2019: unresolved external symbol "class Matrix<int> __cdecl operator+(int,class Matrix<int>)" (??H@YA?AV?$Matrix@H@@HV0@@Z) referenced in function _main
Debug/Matrix.exe : fatal error LNK1120: 1 unresolved externals

IDE VS2003, подскажите в чем ошибка и есть ли она вооще или это compiler bag

Автор: Daevaorn 4.2.2007, 15:35
Цитата(vinter @  4.2.2007,  16:26 Найти цитируемый пост)
 подскажите в чем ошибка и есть ли она вооще или это compiler bag 

плохая мысль
Цитата(vinter @  4.2.2007,  16:26 Найти цитируемый пост)
 friend Matrix<Type> operator+(const Type Variable,const Matrix<Type> SecObj);

не верно
Код

template< typename U >
friend Matrix<U> operator+(const U& Variable,const Matrix<U>& SecObj);

Автор: vinter 4.2.2007, 15:42
спасибо

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)