Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[C++] error C2244: 'Polynomial<T>::operator []' : unable to match function definition to an existing declaration

Ostatnio zmodyfikowano 2014-01-16 20:14
Autor Wiadomość
Yanushu
Temat założony przez niniejszego użytkownika
[C++] error C2244: 'Polynomial<T>::operator []' : unable to match function definition to an existing declaration
» 2014-01-16 00:35:28
Witam. Nie wiedziałem jak sprecyzować problem w tytule bardziej szczegółowo bo sam nie wiem co to za problem. Otoz ucze sie pisac szablony dla klas i kompilator wyrzuca mi nastepujacy error

error C2244: 'Polynomial<T>::operator []' : unable to match function definition to an existing declaration

Oto kod:
C/C++
template < class T >
class Polynomial {
private:
    int degree;
    T * coefficients;
public:
    static const string error[ 9 ];
    Polynomial( unsigned int n );
    Polynomial( const Polynomial & p );
    ~Polynomial();
    T value( T x );
    friend ostream & operator <<( ostream & out, const Polynomial & p );
    Polynomial & operator =( const Polynomial & p );
    T & operator []( unsigned int i );
    T operator ()( T x );
    const Polynomial operator +( const Polynomial & p ) const;
    friend Polynomial operator *( T x, const Polynomial & p );
    const Polynomial operator -( const Polynomial & p ) const;
    Polynomial operator -();
    const Polynomial operator *( const Polynomial & p ) const;
    Polynomial & operator +=( const Polynomial & p );
    Polynomial & operator -=( const Polynomial & p );
    Polynomial operator *=( const Polynomial & p );
    bool operator ==( const Polynomial & p );
};
#endif

C/C++
template < class T > T & Polynomial < T >::operator []( unsigned int i )
{
    return coefficients[ i ];
}


Z góry bardzo dziekuje za jakiekolwiek wskazówki jak rozwiazać problem. Dodam jeszcze ze te same błedy maja inne operatory.
P-102287
Monika90
» 2014-01-16 13:31:01
Nie widzę co tu może powodować błąd C2244 (choć widzę inne problemy). Podaj minimalny ale kompletny kod, który demonstruje problem (i wersję kompilatora też podaj).
P-102301
Yanushu
Temat założony przez niniejszego użytkownika
» 2014-01-16 18:55:05
ok to plik Polynomial.cpp
C/C++
#include"Polynomial.h"
#include"mException.h"
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
using namespace std;
template < typename T >
const string Polynomial < T >::error[] = { "0. Stopien wielomianu jest ujemny lub rowny zero", "1. Stopien wielomianu nie jest liczba naturalna",
    "2. Za mala liczba wspolczynnikow", "3. Za duza liczba wspolczynnikow, dalsze zostaly pominiete",
    "4. Wspolczynnik nie jest liczba", "5. W pliku dane napotkano argument, ktory nie jest liczba",
    "6. Nie udalo sie otworzyc pliku wielomian.txt", "7. Nie udalo sie otworzyc pliku dane.txt",
    "8. Wspolczynnik przy najwyzszej potedze jest rowny 0" };

// dla z_2 funkcja xor
template < class T > Polynomial < T >::Polynomial( unsigned int n )
{
    degree = n;
    try
    { coefficients = new T[ n + 1 ]; }
    catch( bad_alloc & w )
    { throw mException( "Za mało pamięci" ); }
    for( unsigned int i = 0; i < n; i++ )
         coefficients[ i ] = 0.0;
   
    coefficients[ n ] = 1.0;
}

template < class T > Polynomial < T >::Polynomial( const Polynomial & p )
{
    degree = p.degree;
    try
    { coefficients = new T[ degree + 1 ]; }
    catch( bad_alloc & w )
    { throw mException( "Za mało pamięci" ); }
    for( unsigned int i = 0; i <= degree; i++ )
         coefficients[ i ] = p.coefficients[ i ];
   
}

template < class T > Polynomial < T >::~Polynomial()
{
    delete[] coefficients;
}

template < class T > T Polynomial < T >::value( T x )
{
    T w = 0;
    w = coefficients[ 0 ]
    for( int i = 1; i < degree + 1; i++ )
         w = w * x + coefficients[ i ];
   
    return w;
}

template < class T > ostream & operator <<( ostream & out, const Polynomial < T > & p )
{
    bool first = false;
    for( unsigned int i = p.degree; i > 1; i-- )
    {
        if( first && p.coefficients[ i ] > 0 ) out << "+";
       
        if( p.coefficients[ i ] != 0 ) { out << p.coefficients[ i ] << "*x^" << i; first = true; }
    }
    if( first && p.coefficients[ 1 ] > 0 ) out << "+";
   
    if( p.coefficients[ 1 ] != 0 ) { out << p.coefficients[ 1 ] << "*x"; first = true; }
    if( first && p.coefficients[ 0 ] > 0 ) out << "+";
   
    if( p.coefficients[ 0 ] != 0 ) out << p.coefficients[ 0 ];
   
    return out;
}

template < class T > Polynomial & Polynomial < T >::operator =( const Polynomial & p )
{
    delete[] coefficients;
    degree = p.degree;
    try
    { coefficients = new T[ degree + 1 ]; }
    catch( bad_alloc & w )
    { throw mException( "Za mało pamięci" ); }
    for( unsigned int i = 0; i <= degree; i++ ) coefficients[ i ] = p.coefficients[ i ];
   
    return( * this );
}

template < class T > T & Polynomial < T >::operator []( unsigned int i )
{
    return coefficients[ i ];
}

template < class T > T Polynomial < T >::operator ()( T x )
{
    return value( x );
}

template < class T > const Polynomial Polynomial < T >::operator +( const Polynomial & p ) const
{
    unsigned int max, min;
    bool ch = false;
    if( degree >= p.degree ) { max = degree; min = p.degree; ch = true; }
    else { max = p.degree; min = degree; }
    Polynomial < T > c( max );
    for( unsigned int i = 0; i <= min; i++ ) c.coefficients[ i ] = coefficients[ i ] + p.coefficients[ i ];
   
    for( unsigned int i = min + 1; i <= max; i++ )
         if( ch ) c.coefficients[ i ] = coefficients[ i ];
    else c.coefficients[ i ] = p.coefficients[ i ];
   
    return c;
}

template < class T > Polynomial operator *( T x, const Polynomial & p )
{
    Polynomial c( p.degree );
    for( unsigned int i = 0; i <= p.degree; i++ ) c.coefficients[ i ] = x * p.coefficients[ i ];
   
    return c;
}

template < class T > const Polynomial Polynomial < T >::operator -( const Polynomial & p ) const
{
    unsigned int max, min;
    bool ch = false;
    if( degree >= p.degree ) { max = degree; min = p.degree; ch = true; }
    else { max = p.degree; min = degree; }
    Polynomial c( max );
    for( unsigned int i = 0; i <= min; i++ ) c.coefficients[ i ] = coefficients[ i ] - p.coefficients[ i ];
   
    for( unsigned int i = min + 1; i <= max; i++ )
         if( ch ) c.coefficients[ i ] = coefficients[ i ];
    else c.coefficients[ i ] = p.coefficients[ i ];
   
    return c;
}

template < class T > Polynomial Polynomial < T >::operator -()
{
    for( unsigned int i = 0; i <= degree; i++ ) coefficients[ i ] = - 1.0 * coefficients[ i ];
   
    return( * this );
}

template < class T > const Polynomial Polynomial < T >::operator *( const Polynomial < T > & p ) const
{
    Polynomial c( degree + p.degree );
    for( int i = 0; i <= degree; i++ )
    {
        for( int j = 0; j <= p.degree; j++ )
        {
            c.coefficients[ i + j ] += coefficients[ i ] * p.coefficients[ j ];
        }
    }
    return c;
}

template < class T > Polynomial & Polynomial < T >::operator +=( const Polynomial & p )
{
    if( degree == p.degree )
    {
        for( int i = 0; i <= degree; i++ )
             coefficients[ i ] += p.coefficients[ i ];
       
        return( * this );
    }
    if( degree <= p.degree )
    {
        coefficients[ p.degree ];
        for( int i = 0; i <= degree; i++ )
             coefficients[ i ] += p.coefficients[ i ];
       
        coefficients[ p.degree ] = p.coefficients[ p.degree ];
        return( * this );
    }
    if( degree >= p.degree )
    {
        coefficients[ degree ];
        for( int i = 0; i <= p.degree; i++ )
             coefficients[ i ] += p.coefficients[ i ];
       
        return( * this );
    }
}

template < class T > Polynomial & Polynomial < T >::operator -=( const Polynomial & p )
{
    if( degree == p.degree )
    {
        for( int i = 0; i <= degree; i++ )
             coefficients[ i ] -= p.coefficients[ i ];
       
        return( * this );
    }
    if( degree <= p.degree )
    {
        coefficients[ p.degree ];
        for( int i = 0; i <= degree; i++ )
             coefficients[ i ] -= p.coefficients[ i ];
       
        coefficients[ p.degree ] = p.coefficients[ p.degree ];
        return( * this );
    }
    if( degree >= p.degree )
    {
        coefficients[ degree ];
        for( int i = 0; i <= p.degree; i++ )
             coefficients[ i ] -= p.coefficients[ i ];
       
        return( * this );
    }
}

template < class T > Polynomial Polynomial < T >::operator *=( const Polynomial & p )
{
    Polynomial cr( degree + p.degree );
    for( int i = 0; i <= degree; i++ )
    {
        for( int j = 0; j <= p.degree; j++ )
        {
            cr.coefficients[ i + j ] += coefficients[ i ] * p.coefficients[ j ];
        }
    }
    degree = cr.degree;
    delete[] coefficients;
    try
    { coefficients = new T[ degree + p.degree ]; }
    catch( bad_alloc & w )
    { throw mException( "Za mało pamięci" ); }
    for( int i = 0; i <= cr.degree; i++ )
         coefficients[ i ] = cr.coefficients[ i ];
   
    return( * this );
}

template < class T > bool Polynomial < T >::operator ==( const Polynomial & p )
{
    int a;
    if( degree == p.degree )
    {
        for( int i = 0; i <= degree; i++ )
        {
            if( coefficients[ i ] == p.coefficients[ i ] )
                 a = 0;
            else
                 a = 1;
           
        }
        if( a == 0 )
             return true;
        else
             return false;
       
    }
    else
         return false;
   
}

template < class T > bool Polynomial < T >::operator !=( const Polynomial & p )
{
    int a;
    if( degree == p.degree )
    {
        for( int i = 0; i <= degree; i++ )
        {
            if( coefficients[ i ] == p.coefficients[ i ] )
                 a = 0;
            else
                 a = 1;
           
        }
        if( a == 1 )
             return true;
        else
             return false;
       
    }
    else
         return true;
   
}

template < class T > bool Polynomial < T >::operator >( const Polynomial & p )
{
    if( degree < p.degree )
         return false;
    else
    {
        if( degree > p.degree )
             return true;
       
        if( degree == p.degree )
        {
            for( int i = 0; i <= degree; i++ )
            {
                if( coefficients[ i ] > p.coefficients[ i ] )
                     return true;
               
                if( coefficients[ i ] < p.coefficients[ i ] )
                     return false;
               
                if( coefficients[ i ] == p.coefficients[ i ] )
                     return false;
               
            }
        }
    }
}

template < class T > bool Polynomial < T >::operator <( const Polynomial & p )
{
    if( degree > p.degree )
         return false;
    else
    {
        if( degree < p.degree )
             return true;
       
        if( degree == p.degree )
        {
            for( int i = 0; i <= degree; i++ )
            {
                if( coefficients[ i ] > p.coefficients[ i ] )
                     return false;
               
                if( coefficients[ i ] < p.coefficients[ i ] )
                     return true;
               
                if( coefficients[ i ] == p.coefficients[ i ] )
                     return false;
               
            }
        }
    }
}

template < class T > bool Polynomial < T >::operator <=( const Polynomial & p )
{
    if( degree > p.degree )
         return false;
   
    if( degree <= p.degree )
    {
        if( degree == p.degree )
        {
            for( int i = 0; i <= degree; i++ )
            {
                if( coefficients[ i ] == p.coefficients[ i ] )
                     return true;
               
                if( coefficients[ i ] < p.coefficients[ i ] )
                     return true;
               
                if( coefficients[ i ] > p.coefficients[ i ] )
                     return false;
               
            }
        }
        else
             return true;
       
    }
}

template < class T > bool Polynomial < T >::operator >=( const Polynomial & p )
{
    if( degree < p.degree )
         return false;
   
    if( degree >= p.degree )
    {
        if( degree == p.degree )
        {
            for( int i = 0; i <= degree; i++ )
            {
                if( coefficients[ i ] == p.coefficients[ i ] )
                     return true;
               
                if( coefficients[ i ] > p.coefficients[ i ] )
                     return true;
               
                if( coefficients[ i ] < p.coefficients[ i ] )
                     return false;
               
            }
        }
        else
             return true;
       
    }
};

Wymieniony blad występuje przy wszystkich operatorach oprócz boolowskich. Maina nie zamieszczam tam jest z 3 razy tyle i chyba nic by nie pomógł bo polega na wczytywaniu danych z plikow. Oczywiście istnieją inne errory oprócz tego co wymieniłem ale chce stopniowo je usuwać. Zamieszczam jeszcze cały Polynomial.h

C/C++
#ifndef _Polynomial_h_
#define _Polynomial_h_
#include"mException.h"
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
using namespace std;
template < class T >
class Polynomial {
private:
    int degree;
    T * coefficients;
public:
    static const string error[ 9 ];
    Polynomial( unsigned int n );
    Polynomial( const Polynomial & p );
    ~Polynomial();
    T value( T x );
    friend ostream & operator <<( ostream & out, const Polynomial & p );
    Polynomial & operator =( const Polynomial & p );
    T & operator []( unsigned int i );
    T operator ()( T x );
    const Polynomial operator +( const Polynomial & p ) const;
    friend Polynomial operator *( T x, const Polynomial & p );
    const Polynomial operator -( const Polynomial & p ) const;
    Polynomial operator -();
    const Polynomial operator *( const Polynomial & p ) const;
    Polynomial & operator +=( const Polynomial & p );
    Polynomial & operator -=( const Polynomial & p );
    Polynomial operator *=( const Polynomial & p );
    bool operator ==( const Polynomial & p );
    bool operator !=( const Polynomial & p );
    bool operator >( const Polynomial & p );
    bool operator <( const Polynomial & p );
    bool operator <=( const Polynomial & p );
    bool operator >=( const Polynomial & p );
};
#endif

Ok dodam jeszcze ze program służy do tworzenia i wykonywania podstawowych działań na wielomianach wiec parametrem T są tutaj tylko typ tablicy współczynników, współczynniki i x dla których liczymy wartość.
Uzywam  MS VS Express 2013 wiec kompilator nie wiem jak się dokładnie nazywa.

P-102321
Monika90
» 2014-01-16 20:14:28
Minimalne i kompletne to to nie jest, ale teraz przynajmniej widać błędy, we wszystkich definicjach poza klasą, zamiast Polynomial powinno być Polynomial<T>
np. zamiast tego
C/C++
template < class T > Polynomial & Polynomial < T >::operator =( const Polynomial & p )
{
    //...
}
to:
C/C++
template < class T > Polynomial < T > & Polynomial < T >::operator =( const Polynomial < T > & p )
{
    //...
}
itd. dla całej reszty.

Jak już to poprawisz i dodasz brakujący średnik w definicji template<class T> T Polynomial<T>::value(T x), to nawet da się to skompilować, ale czeka cię jeszcze wiele niespodzianek...
P-102323
« 1 »
  Strona 1 z 1