2013-01-09 55 views
-3

我的班級行包含它的數據在一個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 

但下面的事情失敗: - 輸出 操作< < - - 創建主 新實例的getData()methodes

+3

大量的代碼和大多數人無法閱讀的錯誤消息;你不可能得到很好的迴應;)我強烈建議創建一個[** minimal ** test-case](http://sscce.org),並將錯誤消息(或使用英文版的編譯器)。 –

+0

對。目前它只是一個_criminal_測試案例。 –

+1

一旦你超出編譯器錯誤,你會得到一些鏈接器錯誤。模板類的實現應該放在.h文件中,而不是.cpp。 –

回答

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

應該是:

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

或者,更好,

Row<4> r1; 
+0

好吧,更改(在.h文件中實現,並在創建實例主)中,現在它的工作原理 - 除了運算符<< << –

+0

的重載。'operator <<'的定義有同樣的問題。 –

+0

實現沒有模板,無法處理它 –