не может создать экземпляр строки битов

Мой класс Row содержит свои данные в битах. класс наполнен шаблонами, но в моей основной программе невозможно создать новый экземпляр объекта Row.

Row.h

#ifndef ROW_H_
#define ROW_H_

#include <bitset>

template <std::size_t N>
class Row {

public:
typedef std::bitset<N> bs;

private:
Row::bs *data;

public:
Row();                          // ctor
Row(const Row::bs&);
Row(const Row&);                // copy construktor
Row<N>& operator=(const Row&);  // copy with '='
virtual ~Row();                 // dtor

// compare
bool operator==(const Row&) const;
bool operator!=(const Row&) const;
bool operator<(const Row&) const;
bool operator>(const Row&) const;

// setter
void reset();                               // set all bits to 0
void set();                                 // set all bits to 1
//void set(std::size_t, bool val = true);   // set bit at position npos to 0 or 1
void set(const Row::bs&);                   // set with another bitset

// getter
std::size_t size() const;   // number of bits
std::size_t count() const;  // number of bits that are set on 1
bool any() const;           // true if any bit is set on 1
bool none() const;          // true if no bit is set on 1
//Row::bs& getData;
//const Row::bs& getData() const;

// to stream
virtual void toStream(std::ostream&) const;
friend std::ostream& operator<<(std::ostream&, const Row&);
};

#endif /* ROW_H_ */

Row.cpp

#include "Row.h"// ------------------------- ctor dtor -------------------------
template <std::size_t N>
Row<N>::Row() : data(new Row::bs()) {}

template <std::size_t N>
Row<N>::Row(const Row::bs& bits) : data(new Row::bs(bits)) {}

template <std::size_t N>
Row<N>::Row(const Row& r) { this->data = new Row::bs(r.getData()); }

template <std::size_t N>
Row<N>& Row<N>::operator=(const Row& r) {
if( this != &r ) { // self assignment ?
delete this->data; this->data=0;
this->set( r.getData() );
}
return *this;
}

template <std::size_t N>
Row<N>::~Row() {
if(this->data) { delete this->data; } this->data=0;
}// ------------------------- compare -------------------------
template <std::size_t N>
bool Row<N>::operator==(const Row& r) const {
if( this->size() == r.size() ) return this->data & r.getData();
else return false;
}

template <std::size_t N>
bool Row<N>::operator!=(const Row& r) const {
return !( *this == r );
}

template <std::size_t N>
bool Row<N>::operator<(const Row& r) const {
return this->size() < r.size();
}

template <std::size_t N>
bool Row<N>::operator>(const Row& r) const {
return r < *this;
}// ------------------------- setter -------------------------
template <std::size_t N>
void Row<N>::reset() { this->data->reset(); }

template <std::size_t N>
void Row<N>::set() { this->data->set(); }

/*template <std::size_t N>
void Row<N>::set(std::size_t npos, bool val = true) { this->data->set(npos,val); }*/

template <std::size_t N>
void Row<N>::set(const Row::bs& bits) { this->data = new Row::bs(bits); }// ------------------------- getter -------------------------
template <std::size_t N>
std::size_t Row<N>::size() const { return N; }

template <std::size_t N>
std::size_t Row<N>::count() const { return this->data->count(); }

template <std::size_t N>
bool Row<N>::any() const { return this->data->any(); }

template <std::size_t N>
bool Row<N>::none() const { return this->data->none(); }

template <std::size_t N>
Row::bs& Row<N>::getData const { return *this->data; }

template <std::size_t N>
const Row::bs& Row<N>::getData() const { return *this->data; }// ------------------------- to stream -------------------------
template <std::size_t N>
void Row<N>::toStream(std::ostream& os) const { os << this->data; } // should call    bitset<N>::to_string

std::ostream& operator<<(std::ostream& os, const Row& r) {
r.toStream(os);
return os;
}

main.cpp:

#include "Row.h"
int main() {

Row<4> *r1 = new Row();

delete r1; r1=0;

return 0;
}

сообщения компилятора на немецком …

In file included from ../Main.cpp:8:0:
../Row.h:54:59: Warnung: »friend«-Deklaration »std::ostream& operator<<(std::ostream&, const Row<N>&)« deklariert eine Nicht-Template-Funktion [-Wnon-template-friend]
../Row.h:54:59: Anmerkung: (wenn das nicht beabsichtigt war, sollte sicher gestellt werden, dass das Funktions-Template bereits deklariert wurde, und <> hier hinter Funktionsnamen eingefügt wurde)
../Main.cpp: In Funktion »int main()«:
../Main.cpp:13:19: Fehler: expected type-specifier before »Row«
../Main.cpp:13:19: Fehler: »int*« kann nicht nach »Row<4ul>*« in Initialisierung umgewandelt werden
../Main.cpp:13:19: Fehler: expected »,« or »;« before »Row«make: *** [Main.o] Fehler 1

но следующие вещи терпят неудачу:
— создать новый экземпляр в главном
— оператор<< для вывода
— getData () методы

-3

Решение

Row<4> *r1 = new Row();

Должно быть:

Row<4> *r1 = new Row<4>();

Или лучше,

Row<4> r1;
4

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector