Przy próbie wywołania metody sum(), za pomocą more.sum() otrzymuje taki błąd od kompilatora: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'|.
Nie za bardzo rozumiem o co chodzi :/
Nagłowek:
#ifndef WINEC_H_INCLUDED
#define WINEC_H_INCLUDED
#include <iostream>
#include <valarray>
#include <string>
#include <cstring>
using namespace std;
template <class T1, class T2>
class Pair
{
private:
T1 Year;
T2 Bottles;
public:
T1 & first();
T2 & second();
T1 first() const {return Year;}
T1 second() const {return Bottles;}
Pair(const T1 & aval, const T2 & bval) : Year(aval), Bottles(aval) {}
Pair() {}
};
template<class T1, class T2>
T1 & Pair<T1, T2>::first()
{
return Year;
}
template<class T1, class T2>
T2 & Pair<T1,T2>::second()
{
return Bottles;
}
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
class Wine
{
private:
string label;
int years;
PairArray ilosc;
public:
Wine() : ilosc() {}
Wine(const string & l, int y, const int yr[], const int bot[]);
Wine(const string & l, int y) : label(l), years(y), ilosc(ArrayInt(y),ArrayInt(y)) {}
void GetBottles();
void Show() const;
const string & Label() const;
PairArray sum();
};
Definicje:
#include <iostream>
#include <valarray>
#include <string>
#include <cstring>
#include "winec.h"
using namespace std;
Wine::Wine(const string & l, int y, const int yr[], const int bot[]) : label(l), years(y), ilosc(ArrayInt(y),ArrayInt(y))
{
for(int i = 0; i < years; i++)
{
ilosc.first()[i] = yr[i];
ilosc.second()[i] = yr[i];
}
}
void Wine::GetBottles()
{
cout << "Podaj dane o winie " << label << " dla " << years << " rocznikow: " << endl;
for(int i = 0; i < years; i++)
{
cout << "Podaj rocznik: ";
cin >> ilosc.first()[i];
cout << "Podaj liczbe butelelek: ";
cin >> ilosc.second()[i];
}
}
void Wine::Show() const
{
cout << "Wino: " << label << endl;
cout << "\tRocznik\t Butelki\n";
for(int i = 0; i < years; i++)
{
cout << "\t" << ilosc.first()[i] << "\t" << ilosc.second()[i] << endl;
}
}
const string & Wine::Label() const
{
return label;
}
PairArray Wine::sum()
{
PairArray sum;
for(int i = 0; i < years; i++)
{
sum.second() += ilosc.second()[i];
}
return sum;
}
Plik testowy:
#include <iostream>
#include "winec.h"
using namespace std;
int main()
{
cout << "Podaj nazwe wina: ";
char lab[50];
cin.getline (lab, 50);
cout << "Podaj liczbe rocznikow: ";
int yrs;
cin >> yrs;
Wine holding (lab, yrs) ; // Zapisuje nazwe, liczbe roczników i przekazuje
// informacje o tej liczbie do tablicy
holding.GetBottles () ; // Pobiera dane o roczniku i liczbie butelek
holding.Show () ; // Wyswietla zawartosc obiektu
const int YRS = 3;
int y[YRS] = { 1993, 1995, 1998 };
int b[YRS] = { 48, 60, 72 } ;
// Tworzy nowy obiekt, inicjalizuje za pomoca danych w tablicach y oraz b
Wine more("Gushing Grape Red", YRS, y, b ) ;
more.Show();
cout << "Laczna liczba butelek wina " << more.Label() << ": " << more.sum() << endl;
cout << "Koniec\n";
return 0;
}