C++ 无法创建位行的实例
“我的类”行包含位集中的数据。这个类是用模板实现的,但是在我的主程序中不可能创建一个新的Row对象实例 第h排 Row.cppC++ 无法创建位行的实例,c++,templates,stl,bitset,C++,Templates,Stl,Bitset,“我的类”行包含位集中的数据。这个类是用模板实现的,但是在我的主程序中不可能创建一个新的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&
#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中创建新实例
-接线员
应该是:
Row<4> *r1 = new Row<4>();
或者更好
Row<4> r1;
大量的代码和大多数人看不懂的错误信息;你不太可能得到好的回应;我强烈建议您创建一个编译器,并翻译错误消息或使用英语版本的编译器。对。目前,这只是一个犯罪测试案例。一旦你超越了编译器错误,你将得到一些链接器错误。模板类的实现应该放在.h文件中,而不是.cpp.ok文件中。h文件和创建实例main中的更改实现已经完成,现在它可以工作了-除了运算符的重载运算符的定义运算符的实现是在没有模板的情况下完成的,无法处理它
Row<4> *r1 = new Row<4>();
Row<4> r1;