Re: [問題] BCB6: '> >' for nested templates

看板C_and_CPP作者 (高髮箍)時間10年前 (2013/09/19 14:22), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《deo2000 (800IM)》之銘言: : #include <vector> : using namespace std; : . : . : . : vector<vector<int>> vPixel(jpImg->Width,vector<int>(jpImg->Width)); : 會錯, : [C++ Warning] Unit1.cpp(46): W8049 Use '> >' for nested templates instead of : '>>' : [C++ Error] Unit1.cpp(46): E2268 Call to undefined function 'vPixel' : vector<vector<int> > vPixel(jpImg->Width,vector<int>(jpImg->Width)); : ^^ : 若加一格空白,編譯就過了! : 超怪的! : 這是IDE的bug嗎? vector< vector<int> > vPixel( jpImg->Width , vector<int>(jpImg->Width) ); 我的排版會像上面那樣, 所以不會遇到>>的問題, 但是一般我們不會這樣 寫. 因為型別重複次數太高將使得切換實作容器型別以及客製化模板型別 引數時會麻煩許多. 你需要抽象化: namespace detail { // not interface typedef int Pixel; typedef std::vector<Pixel> Pixels1D; } typedef std::vector<Pixels1D> Pixels2D; namespace detail { // not interface Pixels2D create_pixels(size_t height, size_t width) { return Pixels2D ( height, Pixels1D(width) ); } } // 使用者應該利用此 helper 產生物件而非直接呼叫建構子 Pixels2D create_pixels_from(???* img) { return detail::create_pixels(img->Height, img->Width); } // client code Pixels2D vPixel = create_pixels_from(jpImg); // or "auto vPixel = create_pixels_from(jpImg);" in C++11 太多層的[]會讓我有閱讀障礙, 並且使我懷疑能不能使用其他vector介面 直接存取資料成員, 這樣容易產生對實作的假設. 以我的情況則是會另外 寫一個類別 Pixels 包裝容器, 並且提供介面限制使用者存取: // client code auto vPixel = Pixels(jpImp); auto height = vPixel.getHeight(); auto width = vPixel.getWidth(); auto first_pixel = vPixel.get(0, 0); // 自由發揮 for(auto row : vPixel.getRows()) { // ^這裡可以設計成不用 auto& 來接且不增加複製成本 ... } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 112.105.98.50 ※ 編輯: loveme00835 來自: 112.105.98.50 (09/19 14:42)
文章代碼(AID): #1IEfWfAk (C_and_CPP)
文章代碼(AID): #1IEfWfAk (C_and_CPP)