przeciążenie operatora w klasie wzorcowej macierzy

0

Witam, mam problem z wywołaniem funkcji dla operatorów pezeciążonych funkcji wzorcowej macierzy. Tak wyglada klasa macierzy

#ifndef MACIERZETEMPLATE_H
#define MACIERZETEMPLATE_H
#include "Wierszetemplate.h"
#include "Kolumnytemplate.h"
#include <iosfwd>

template <class T>
class macierzetemplate
{
public:
        template <class Q>
        friend class wierszetemplate;
        template <class Q>
        friend class kolumnytemplate;
        template <class Q>
        friend std::ostream& operator << (std::ostream&, const macierzetemplate<T>&);
        friend std::istream& operator >> (std::istream& in, const macierzetemplate<T>&);
        friend std::istream& operator >> (std::istream& in, const T&);
    macierzetemplate ();
    macierzetemplate (int, int);
    ~macierzetemplate();
    int M() const;
    int N() const;
    T& operator()(int, int) const;
    operator T**() const {return tab;};
    wierszetemplate<T>& operator () (int);
    kolumnytemplate<T>& operator [] (int);
    macierzetemplate<T>& operator = ( macierzetemplate<T>& );
    macierzetemplate<T>& operator + (const macierzetemplate<T>&, const macierzetemplate<T>&);
    macierzetemplate<T> transpozycja (macierzetemplate<T>&);

      int m;
      int n;
private:
      T** tab;

};
 
#include <iostream>
#include "macierzetemplate.h"



using namespace std;
template <class T>
ostream& operator << (ostream& wy, const macierzetemplate<T>& x)
{
    for(int i=0; i<x.m; ++i)
       {
       for (int j=0;j<x.n;j++)
           wy<<x(i,j)<<"|";
         wy<<endl;
        }

    return wy;
}

template <class T>
istream& operator >> (istream& in, const macierzetemplate<T>& x)
{
    for(int i=0; i<x.m; ++i)
       {
       for (int j=0;j<x.n;j++)
           in>>x(i,j);

        }

    return in;
}
template <class T>
istream& operator >> (istream& in, T& x)
{

         return  in>>x;


}

template <class T>
macierzetemplate<T>& macierzetemplate<T>::operator = ( macierzetemplate<T>& b)
{
    macierzetemplate<T> a(b.m,b.n);
        for (int i=0;i<a.m;i++)
           for (int j=0;j<a.n;j++)
              a[i][j]=b[i][j];
    return a;
}

template <class T>
macierzetemplate<T>& macierzetemplate<T>::operator + (const macierzetemplate<T>& a, const macierzetemplate<T>& b)
{
    if (a.m!= b.m || a.n!=b.m)
    {
        throw;
    }
    else
    {
        macierzetemplate<T> c(a.m,a.n);
        for (int i=0;i<c.m;i++)
           for (int j=0;j<c.n;j++)
              c(i,j)=a(i,j)+b(i,j);
    return c;

    }

}
 
#include <iostream>
#include "src/macierzetemplate.cpp"
#include "src/wierszetemplate.cpp"
#include "src/kolumnytemplate.cpp"
#include "wierszetemplate.operatory.cpp"
#include "kolumnytemplate.operatory.cpp"
#include "macierzetemplate.operatory.cpp"
#include "macierzetemplate.h"
using namespace std;

int main()
{
    macierzetemplate<float> m(6,6);
    float** tab;
    tab=m;
    cout<<m<<endl;
    macierzetemplate<float> z(6,6);
    cout<<z<<endl;
    macierzetemplate<float> a=m+z;
    return 0;
}

wyskakuje następujący błąd

 
||=== Build: Debug in macierzetemplate (compiler: GNU GCC Compiler) ===|
include\macierzetemplate.h|17|warning: friend declaration 'std::istream& operator>>(std::istream&, const macierzetemplate<T>&)' declares a non-template function [-Wnon-template-friend]|
include\macierzetemplate.h|17|note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) |
include\macierzetemplate.h|18|warning: friend declaration 'std::istream& operator>>(std::istream&, const T&)' declares a non-template function [-Wnon-template-friend]|
include\macierzetemplate.h|29|error: 'macierzetemplate<T>& macierzetemplate<T>::operator+(const macierzetemplate<T>&, const macierzetemplate<T>&)' must take either zero or one argument|
C:\Users\EVELINA\Downloads\2014.01.07\macierzetemplate.operatory.cpp|52|error: 'macierzetemplate<T>& macierzetemplate<T>::operator+(const macierzetemplate<T>&, const macierzetemplate<T>&)' must take either zero or one argument|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp||In function 'int main()':|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|19|error: no match for 'operator+' in 'm + z'|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|19|note: candidates are:|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_iterator.h|335|note: template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&)|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_iterator.h|335|note:   template argument deduction/substitution failed:|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|19|note:   'macierzetemplate<float>' is not derived from 'const std::reverse_iterator<_Iterator>'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2362|note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2362|note:   template argument deduction/substitution failed:|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|19|note:   'macierzetemplate<float>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.tcc|694|note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.tcc|694|note:   template argument deduction/substitution failed:|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|19|note:   mismatched types 'const _CharT*' and 'macierzetemplate<float>'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.tcc|710|note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(_CharT, const std::basic_string<_CharT, _Traits, _Alloc>&)|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.tcc|710|note:   template argument deduction/substitution failed:|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|19|note:   'macierzetemplate<float>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2399|note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2399|note:   template argument deduction/substitution failed:|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|19|note:   'macierzetemplate<float>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2415|note: template<class _CharT, class _Traits, class _Alloc> std::basic_string<_CharT, _Traits, _Alloc> std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|2415|note:   template argument deduction/substitution failed:|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|19|note:   'macierzetemplate<float>' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'|
C:\Users\EVELINA\Downloads\2014.01.07\main.cpp|25|error: 'b' was not declared in this scope|
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

Nie wiem co jest źle, bardz proszę o pomoc.

0

po pierwsze

template <class T>
macierzetemplate<T>& macierzetemplate<T>::operator + (const macierzetemplate<T>& a, const macierzetemplate<T>& b)
{
     macierzetemplate<T> c(a.m,a.n);
     return c;
}

wiesz czym to grozi nie?

edit:

http://en.cppreference.com/w/cpp/language/operators

tutaj znajdziesz jak fajnie przeładować operator+ i operator+=

0

nie bardzo rozumiem

0

problem w tym że tak wygląda to porzeciążenie w podobnej klasie u prowadzącego zajecia u którego mam zdobyc zaliczenie

 
const macierz& operator+(const macierz& m1, const macierz& m2)
{
 if((m1.m==m2.m)&&(m1.n==m2.n))
 {
  macierz *m(new macierz(m1.m,m1.n));
  for(int i=0;i<m1.m;++i)
  {
   for(int j=0;j<m1.n;++j)
   {
    (*m)(i,j)=m1(i,j)+m2(i,j);
   }
  }
  return *m;
 }
 else
 {
  throw;
 }
}

więc muszę raczej pobierać dwie macierze i razem z referencjami wszystko, wiem jak przeciążać operatory aby pobierały jeden argument i uruchamiały się na rzecz drugiego, tylko tutaj raczej tak nie mogę postąpić i muszę się skupić nad rozwianiem problemu który sprawia że nie działa niż przerobić całe przeciążenie

0

tylko dlaczego wciąż pokazuje mi ten błąd i jak się go moge pozbyć?:

include\macierzetemplate.h|29|error: 'macierzetemplate<T>& macierzetemplate<T>::operator+(const macierzetemplate<T>&, const macierzetemplate<T>&)' must take either zero or one argument|

0
mewe napisał(a):

tylko dlaczego wciąż pokazuje mi ten błąd i jak się go moge pozbyć?:

include\macierzetemplate.h|29|error: 'macierzetemplate<T>& macierzetemplate<T>::operator+(const macierzetemplate<T>&, const macierzetemplate<T>&)' must take either zero or one argument|

no bo Twój operator+ to member function, jeżeli chcesz, aby mógł przyjmować wiecej niż 1 parametr to wywal go z klasy lub daj mu friend

i dodaj const do parametru, który zwracasz lub wywal &

0

Dzięki wielkie, ruszyło do przodu, choć pewnie napotkam jeszcze wiele problemów

0

W różnych momentach pojawia się problem, mimo że kompiluje i nie wyskakują już żadne błędy, to jednak program nie otwiera się poprawnie. Czy ktoś umiałby coś powiedzieć na ten temat. Zdjęcie w załączniku

0

pominełam teraz w programie wszystkie obsługi błędu, lecz wciąż powraca teraz błąd std::bad_alloc

0

próbuje zdefiniować transpozycje macierzy


template <class T>
const macierzetemplate<T>& macierzetemplate<T>:: transpozycja ()
{
    macierzetemplate<T> tab(this-> M(),this->N(),0);
      for (int i=0;i<n;i++)
           for (int j=0;j<m;j++)
            tab(i,j)=(*this)(j,i);

}
 

i po wywo łaniu tego w main wyskakuje ten wyjatek

 
#include <iostream>
#include "src/macierzetemplate.cpp"
#include "src/wierszetemplate.cpp"
#include "src/kolumnytemplate.cpp"
#include "wierszetemplate.operatory.cpp"
#include "kolumnytemplate.operatory.cpp"
#include "macierzetemplate.operatory.cpp"
#include "macierzetemplate.h"
using namespace std;

int main()
{
    macierzetemplate<float> v=a.transpozycja();
    return 0;
}

klasa teraz wyglada tak

 
#ifndef MACIERZETEMPLATE_H
#define MACIERZETEMPLATE_H
#include "Wierszetemplate.h"
#include "Kolumnytemplate.h"
#include <iosfwd>

template <class T>
class macierzetemplate
{

public:
        template <class Q>
        friend class wierszetemplate;
        template <class Q>
        friend class kolumnytemplate;
        template <class Q>
        friend std::ostream& operator << (std::ostream&, const macierzetemplate<T>&);
        template <class Q>
        friend std::istream& operator >> (std::istream& in, const macierzetemplate<T>&);
        template <class Q>
        friend std::istream& operator >> (std::istream& in, const T&);
        template <class Q>
        friend  macierzetemplate<T>& operator + (const macierzetemplate<T>&, const macierzetemplate<T>&);
        template <class Q>
        friend  macierzetemplate<T>& operator - (const macierzetemplate<T>&, const macierzetemplate<T>&);
    const macierzetemplate<T>&  transpozycja();
    macierzetemplate ();
    macierzetemplate (int, int, int z=0);
    macierzetemplate (const macierzetemplate<T>&);
    ~macierzetemplate();
    int M() const;
    int N() const;
    operator T**() const {return tab;};
    T& operator()(int, int) const;
    wierszetemplate<T>& operator () (int);
    kolumnytemplate<T>& operator [] (int);
    const macierzetemplate<T>& operator = (const macierzetemplate<T>&);
    private:
      T** tab;
      int m;
      int n;

};
#endif // MACIERZETEMPLATE_H

i wszystkie pozostałe metody działają

0

kod wydaje się ok, tzn typowanie, bo to się kompiluje, debuguj, printuj sobie stany, ja nic tutaj nie dam rady wyczarować

i mówiłem Ci

friend macierzetemplate<T>& operator +

albo bez& albo const&

reszty implementacji nie widziałem

0

i tak wielkie dzięki

1 użytkowników online, w tym zalogowanych: 0, gości: 1