Fehler: expected init-declarator before '&' token



  • Hallo zusammen!

    Ich mach gerade nen Programm (bzw. hab damit angefangen) und wollte nun Operatoren überladen.Habe die Klasse matrix definiert und wollte nun mit

    #include <matrix.hpp>
    
    (...)
    matrix matrix&::operator = ( const matrix &rhsMatrix )
    {for(int i=0;i<this->numOfColumns;i++)
    		{
    			for(int j=0;j<this->numOfRows;j++)
    			{
    	 		 this->entry[i][j]=rhsMatrix.entry[i][j];
    			}
    			return *this
    		};
    

    und in matrix.hpp

    #ifndef MATRIX_HPP
    #define MATRIX_HPP
    
    #include <iostream>
    #include <vector>
    
    (...)
    
    /** A general class for a matrix with double entries only.
     */
    class matrix {
    
      private:
      /** Those are the actual matrix entries (matrix is a vector of vectors).
       * This is a non-sparse version.
       */
      vector< vector< double > > entry;
    
      /** The number of rows of the matrix
       */
      int numOfRows;
    
      /** The number of coloumns of the matrix.
       */
      int numOfColumns;
    
      public:
      /** Use this to construct a new matrix.
       */
      matrix( const int &numOfRows, const int &numOfColumns );
    
      /** Return the transposed matrix.
       */
      matrix getTransposed() const;
    
      /** Dumps matrix to stdout.
       */
      void dump() const;
    
      /** Retrieve element (i,j).
       */
      double get( const int &i, const int &j ) const;
    
      /** Set element (i,j) to value.
       */
      void set( const int &i, const int &j, const double &value );
    
      /** Function that returns the number of rows.
       */
      int getNumOfRows() const;
    
      /** Function to retrieve the number of columns.
       */
      int getNumOfColumns() const;
    
      /* some operators */
      /** Assign rhsMatrix to current one.
       */
      matrix& operator = ( const matrix &rhsMatrix );
    
      /** Add rhsMatrix to current matrix and return NEW matrix.
       */
      matrix operator + ( const matrix &rhsMatrix ) const;
    
      /** Add rhsMatrix to instance and update it afterwards.
       */
      matrix& operator += ( const matrix &rhsMatrix );
    
      /** Standard matrix multiplication; result will be stored in new object.
       */
      matrix operator * ( const matrix &rhsMatrix ) const;
    };
    

    Und nun bekomme ich da oben den Fehler...warum?Hab mich schon durchs Internet gesucht,aber iwie gibts da anscheinend mehrere Meinungen zu....keine Ahnung,bin ziemlich verwirrt,wäre super,wenn ihr mir helfen könntet 🙂

    Edit: expected init-declarator before '&' token
    expected ,' or;' before '&' token

    das ist der Fehler.
    Machts gut,
    Mirko



  • Nächstes Mal bitte noch die Zeilennummer des Fehlers (bezogen auf den abgebildeten Code) angeben.

    matrix matrix&::operator =
    

    Das ist falsch. Das zweite matrix sollte die Klassenzugehörigkeit angeben, das erste ist für den Rückgabewert. Also

    matrix& matrix::operator =
    


  • Tschuldige,mach ich 🙂

    Danke,es klappt!


Anmelden zum Antworten