C++ - friend operator – C / C++ – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

C++ - friend operator – C / C++ – Fórum – Programujte.comC++ - friend operator – C / C++ – Fórum – Programujte.com

 

jupiik
~ Anonymní uživatel
9 příspěvků
19. 11. 2011   #1
-
0
-

ahoj mam mensi problem

template <typename T, typename K>
class Matrix;

template <typename T, typename K>
Matrix<T,K>& operator*(const K left, Matrix<T,K>& right)


template <typename T, typename K>
class Matrix
  {
      public:
         friend Matrix<T,K>& operator*(const K left, Matrix<T,K>& right) 
};


template <typename T, typename K>
Matrix<T,K>& operator*(const K left, Matrix<T,K>& rigt)
  {
      ...;
      return right;
  }

napise mi to pri prekladani (debian 6.01, amd64, gcc ver. 4.4.5)

"g++ -Wall -std=c++0x Matrix.cpp"

In file included from ../XMatrix.hpp:7,
                 from XMatrix_test.cpp:1:
../../MathGeometry/Matrix/Matrix.hpp:70: warning: friend declaration ‘MathGeometry::Matrix<T, K>& MathGeometry::operator*(K, MathGeometry::Matrix<T, K>&)’ declares a non-template function
../../MathGeometry/Matrix/Matrix.hpp:70: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
../../MathGeometry/Matrix/Matrix.hpp:71: warning: friend declaration ‘MathGeometry::Matrix<T, K>& MathGeometry::operator+(K, MathGeometry::Matrix<T, K>&)’ declares a non-template function

kdyz to upravim na 

template <typename T, typename K>
class Matrix;

template <typename T, typename K>
Matrix<T,K>& operator*(const K left, Matrix<T,K>& right)


template <typename T, typename K>
class Matrix
  {
      public:
         friend Matrix<T,K>& (operator*<>)(const K left, Matrix<T,K>& right) 
};


template <typename T, typename K>
Matrix<T,K>& operator*(const K left, Matrix<T,K>& rigt)
  {
      ...;
      return right;
  }

tak to pro zmenu napise 

hGeometry/Matrix/Matrix.hpp:70: error: expected ‘)’ before ‘<’ token
../../MathGeometry/Matrix/Matrix.hpp:70: error: expected ‘;’ before ‘<’ token
../../MathGeometry/Matrix/Matrix.hpp:71: error: expected ‘)’ before ‘<’ token
../../MathGeometry/Matrix/Matrix.hpp:71: error: expected ‘;’ before ‘<’ token

Nevite nahodou nekdo co stim ?? Jsem z toho celi nestastny ...

Nahlásit jako SPAM
IP: 85.92.56.–
vitamin+8
Grafoman
19. 11. 2011   #2
-
0
-

#1 jupiik
 Funkcia operator* je template, cize musis specifikovat podla akych typov sa ma generovat. V tvojom pripade podla typov <T, K>

template <typename T, typename K>
class Matrix
  {
      public:
         friend Matrix<T,K>& operator*<T, K>(const K left, Matrix<T,K>& right);
};
Nahlásit jako SPAM
IP: 95.105.128.–
obfuscate: "The cruel god Malloc will strike you down. "
ZMeson: "That's the C god. C++ has a new god. "
jupiik
~ Anonymní uživatel
9 příspěvků
19. 11. 2011   #3
-
0
-

#2 vitamin
to jsem taky zkousel .. bohuzel nefungovalo ...

ale uz jsem na to prisel ....

#ifndef OPENGL_MATHGEOMETRY_MATRIX
#define OPENGL_MATHGEOMETRY_MATRIX

#include <iostream>


using namespace std;


namespace MathGeometry
{


typedef unsigned short int ushorti;

template <typename T = float, typename K = T>
class Matrix;

template <typename T, typename K>
class VirtualMatrix;    

template <typename T, typename K>
ostream& operator<<(ostream&, const Matrix<T,K>&);

template <typename T, typename K>
Matrix<T,K> operator*(const K& left, const Matrix<T,K>& right);


template <typename T, typename K>
T cukni()
  {
      cout << "cukl\n";

      return 2;
  }


template <typename T, typename K>
class Matrix
    {
        public:
            virtual ~Matrix();
            Matrix(const Matrix<T,K>& copy);
            Matrix(Matrix<T,K>&& move);
            Matrix() = delete;
            Matrix(const Matrix<T,K>* const copy);
            Matrix(unsigned short int m, unsigned short int n);
            Matrix(unsigned short int m, unsigned short int n, const T* const matrix);
            Matrix(unsigned short int m, unsigned short int n, const T** matrix);

            T&                  GetMatrixNumber(unsigned short int _m, unsigned short int _n);
            unsigned short int  Getn()   const   { return n; }
            unsigned short int  Getm()   const   { return m; }
            void                kxM(K value);
            void                kdM(K value); // d - dividing
            void                mxM(const T* const matrix);
            void                Mxm(const T* const matrix);
            void                MT();
            void                LoadIdentityMatrix();
            void                LoadZeroMatrix();
            void                LoadMatrix(const T* const matrix);

            friend ostream&     (operator<<<>)(ostream& left, const Matrix<T,K>& right);
            friend Matrix<T,K>  (operator*<>)(const K& left, const Matrix<T,K> &right);
            inline T*           operator[](unsigned short int _m);
            inline const T*     operator[](unsigned short int _m) const;
            Matrix<T,K>&        operator=(const Matrix<T,K>& right);
            Matrix<T,K>&        operator=(Matrix<T,K>&& right);
            Matrix<T,K>&        operator=(const VirtualMatrix<T,K>& right);
            Matrix<T,K>&        operator=(VirtualMatrix<T,K>&& right);
            Matrix<T,K>         operator+(const Matrix<T,K>& right);
            Matrix<T,K>         operator*(const K& right);
                                operator const T*() const;
            


        protected:
             void FillMatrix(T value);
             T*   rGetLines()   const    { return Lines; }
             bool GetChange()   const    { return Change; }
             void ResetChange()          { Change = false; }


        private:
            T*   Lines;
            unsigned short int m;
            unsigned short int n;
            bool Change;
    };
} // end namespace MathGeometry


#include "ThrowError/Throw_Matrix.hpp"
#include "source/Matrix.cpp"


#endif
// END OF FILE

toto j cely hpp soubor. jsou deklarovane dva operatory  pro 2* matrix a matrix * 2, kdyz deklaraci

Matrix<T,K>         operator*(const K& right);

hodite pred friend deklaraci

friend Matrix<T,K>  (operator*<>)(const K& left, const Matrix<T,K> &right);

tak to zacne hazet chybu "error: expected ‘)’ before ‘<’ token" na radku kde je friend ... ma nekdo nejake vysvetleni ?

Nahlásit jako SPAM
IP: 85.92.56.–
Matrix17
~ Anonymní uživatel
300 příspěvků
20. 11. 2011   #4
-
0
-

Předně vůbec nepotřebuješ definovat typy T a K ale stačí ti T. K je stejně stejný jako T a navíc to je matice takže všechny prvky by měly mít stejnej typ.

Pak třeba tyhle řádky sou mi moc nedávají smysl:

Matrix(Matrix<T,K>&& move);
Matrix() = delete;

Třeba mne někdo zkušenější opraví ale mně se to zdá špatně. Tyhle operátory třeba vůbec neexitustujou tak je asi ani nepřetížíš:

friend ostream&     (operator<<<>)(ostream& left, const Matrix<T,K>& right);
friend Matrix<T,K>  (operator*<>)(const K& left, const Matrix<T,K> &right);
Nahlásit jako SPAM
IP: 213.226.237.–
jupiik
~ Anonymní uživatel
9 příspěvků
20. 11. 2011   #5
-
0
-

#4 Matrix17
ale existuji ... zajimave je, ze mi to funguje naprosto v pohode a bez warning ... (ty <> jsou, aspon podle g++, jakesy priznaky - kdyztak me opravte - aby kompilator vedel, ze friend funkce patri presne k danemu template)

Ty dva nove radky jsou z pripravovane normy - prvni je move constructor a druhy je zakazany podle nove pripravovane normy ....

Nahlásit jako SPAM
IP: 85.92.56.–
jupiik
~ Anonymní uživatel
9 příspěvků
20. 11. 2011   #6
-
0
-

To proc definuji dva typy je ciste a jeno moje vec - nasobim matice cislem ktere ne vzdy je s plov des carkou, tak jsem pro nej vytvoril novy typ a tedkom muzu bez nejakeho hnusneho pretypovani napsat ...

int b = 2;
Matrix<float, int> pokus(2,2);
pokus.LoadIdentityMatrix();

pokus = b * pokus;
pokus *= b (tento operator jeste nemam dekl.)
Nahlásit jako SPAM
IP: 85.92.56.–
Matrix17
~ Anonymní uživatel
300 příspěvků
20. 11. 2011   #7
-
0
-

Aha s novou normou sem se ještě tolik neseznámil. Dík za vysvětlení.

Nahlásit jako SPAM
IP: 213.226.237.–
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 24 hostů

Podobná vlákna

Friend operator+ — založil Stamp

Virtuální friend funkce — založil Blujacker

Operátor &lt;&lt; — založil RePRO

Operátor -> — založil Adam

Moderátoři diskuze

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý