我的类Row在bitset中包含其数据。该类是使用模板实现的,但是在主程序中无法创建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
但是以下事情失败了: - 在main中创建新实例 - 运算符&lt;&lt;输出 - getData()方法
答案 0 :(得分:4)
Row<4> *r1 = new Row();
应该是:
Row<4> *r1 = new Row<4>();
或者,更好,
Row<4> r1;