Witam. Uczę się C++ i mam pytanie:
Robię klasę szablonową i operatory (jest ich więcej, ale dla przejrzystości podałem tylko dwa).
Przy każdym z nich wyskakuje mi:
error C2975: 'Zbior' : invalid template argument for 'N', expected compile-time constant expression
Jak powinienem pisać templaty?
#include <bitset>
#include <string>
#include <string.h>
using namespace std;
template <int N=32>
class Zbior
{
bitset<N> bity;
public:
//konstruktory
template <class N>
friend ostream& operator<<(ostream& os, const Zbior<N>& sb);
template <class N>
friend Zbior<N>& operator+( const Zbior<N>&, const Zbior<N>&);
};
template <class N>
ostream& operator<<(ostream& os, const Zbior<N>& zb)
{
os<<"{";
for (int i=0; i<zb.bity.size(); i++)
{
if (zb.bity[i]==1)
os<<i<<", ";
}
os<<"}"<<endl;
return os;
}
template <class N>
Zbior<N>& operator+( const Zbior<N>& a, const Zbior<N>& b)
{
bitset<N> nowy_bitset= (a.bity | b.bity);
string str=nowy_bitset.to_string();
const char* strr=str.c_str();
Zbior<N> nowy (strr);
return nowy;
}