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

[C++] error: 'main()::Liczba main()::Liczba::operator+(const main()::Liczba&, const main()::Liczba&)' must take either zero or one argument|

Ostatnio zmodyfikowano 2014-12-23 18:08
Autor Wiadomość
killermannnnn
Temat założony przez niniejszego użytkownika
[C++] error: 'main()::Liczba main()::Liczba::operator+(const main()::Liczba&, const main()::Liczba&)' must take either zero or one argument|
» 2014-12-23 17:03:45
Co jest źle w tym kodzie?

Dostaje takie błędy:

|14|error: 'main()::Liczba main()::Liczba::operator+(const main()::Liczba&, const main()::Liczba&)' must take either zero or one argument|
|23|error: no match for 'operator+' in 'jeden + dwa'|

C/C++
#include <iostream>

using namespace std;

int main()
{
    struct Liczba
    {
        int rzeczyw;
        int zespol;
        Liczba( int a, int b )
            : rzeczyw( a )
             , zespol( b )
        { };
       
        Liczba operator +( const Liczba & a, const Liczba & b )
        {
            return Liczba( a.rzeczyw + b.rzeczyw, a.zespol + b.zespol );
        }
    };
   
    Liczba jeden( 2, 5 ),
    dwa( 1, 3 );
   
    Liczba trzy = jeden + dwa;
   
    cout << trzy.rzeczyw << " " << trzy.zespol << endl;
}
P-123278
szymeczek31
» 2014-12-23 17:50:37
EDIT: To powinno rozwiązać problem
C/C++
#include <iostream>
using namespace std;

class Liczba //mi sie wydaje ze lepiej deklarowac klasy zamiast struktur i przed main
{
    int liczba1;
    int liczba2;
public:
    int rzeczyw;
    int zespol;
    Liczba( int a, int b )
        : rzeczyw( a )
         , zespol( b )
    { liczba1 = a; liczba2 = b; }
   
    Liczba operator +( const Liczba & a ) //Przeciążając operator + możesz podać max 1 argument
    {
        return Liczba( a.rzeczyw + liczba1, a.zespol + liczba2 );
    }
};

int main()
{
    Liczba jeden( 2, 5 ), dwa( 1, 3 );
   
    Liczba trzy = jeden + dwa;
   
    cout << trzy.rzeczyw << " " << trzy.zespol << endl;
}
P-123279
killermannnnn
Temat założony przez niniejszego użytkownika
» 2014-12-23 18:08:11
OK, dzięki za odpowiedź
P-123280
« 1 »
  Strona 1 z 1