2014-09-28 117 views
1

我試圖實現一個簡單的N維數組。這似乎是或多或少正常工作,但我不能得到它的超載ostream操作工作。這是我目前的執行:帶有重載ostream的遞歸類模板<<運算符

#include <iostream> 
#include <memory> 
#include <vector> 

template <typename Type, int Dimension> 
struct Array { 
    typedef std::vector<typename Array<Type, Dimension - 1>::type> type; 

    template <typename cType, int cDimension> 
    friend std::ostream &operator<<(std::ostream &stream, const Array<cType, cDimension>::type &object) { 
     if (cDimension == 0) { 
      stream << object << ' '; 
     } else { 
      typedef typename Array<cType, cDimension>::type::iterator ArrayIterator; 
      for (ArrayIterator it = object.begin(); it != object.end(); ++it) { 
       typedef typename Array<cType, cDimension - 1>::type NestedArray; 

       NestedArray nArray = (NestedArray)(*it); 
       stream << nArray << std::endl; 
      } 
     } 

     return stream; 
    } 
}; 

template <typename Type> 
struct Array < Type, 0 > { 
    typedef Type type; 
}; 

int main() { 
    Array<int, 1>::type c00 = { 1, 2, 3 }; 
    Array<int, 1>::type c01 = { 2, 3, 4 }; 
    Array<int, 1>::type c02 = { 3, 4, 5 }; 
    Array<int, 2>::type c10 = { c00, c01 }; 
    Array<int, 2>::type c11 = { c01, c02 }; 
    Array<int, 3>::type c20 = { c10, c11 }; 

    std::cout << c20 << std::endl; 

    return 0; 
} 

我得到以下編譯錯誤:

1>------ Build started: Project: NDepthArray, Configuration: Debug Win32 ------ 
1> Source.cpp 
1>c:\users\Administrator\documents\visual studio 2013\projects\cppmaterials\ndeptharray\source.cpp(10): warning C4346: 'Array<Type,Dimension>::type' : dependent name is not a type 
1>   prefix with 'typename' to indicate a type 
1>   c:\users\Administrator\documents\visual studio 2013\projects\cppmaterials\ndeptharray\source.cpp(25) : see reference to class template instantiation 'Array<Type,Dimension>' being compiled 
1>c:\users\Administrator\documents\visual studio 2013\projects\cppmaterials\ndeptharray\source.cpp(10): error C2061: syntax error : identifier 'type' 
1>c:\users\Administrator\documents\visual studio 2013\projects\cppmaterials\ndeptharray\source.cpp(10): error C2805: binary 'operator <<' has too few parameters 
1>c:\users\Administrator\documents\visual studio 2013\projects\cppmaterials\ndeptharray\source.cpp(10): fatal error C1903: unable to recover from previous error(s); stopping compilation 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

我簡直想我所有的想法已經,其中包括刪除好友關鍵字和實際的類參數,但沒有任何變化。我們如何重載這些類模板的運算符?

乾杯, 喬伊

+1

是否有任何理由不首先使用'std :: array'? – 101010 2014-09-28 20:16:10

回答

2

與方法的問題是,cType無法推斷:

template <typename Type, int Dimension> // Asking for Type as cType 
struct Array { 
    typedef std::vector<typename Array<Type, Dimension - 1>::type> type; 
} 

template <typename cType, int cDimension>      ↓↓↓↓↓ 
std::ostream &operator<<(std::ostream &stream, typename Array<cType, cDimension>::type &object) 

Array<int, 3>::type c20 = { c10, c11 }; 
std::cout << c20 // Deduction failure 

您可以在這裏找到更多的信息:https://stackoverflow.com/a/12566268/1938163

14.8.2.5/4

然而,在某些情況下,第e值不參與 類型的演繹,而是使用 的模板參數的值,這些參數可以在別處推導或明確指定。如果模板 參數僅用於未推導的上下文,並且未明確指定 ,則模板參數推導失敗

作爲一個旁註:實現多維數組或向量與模板的遞歸代碼複雜的結構是不是非常容易維護和肯定難以讀取路徑來實現的東西,本來是可以做得更快,更有效的(更少的分配)和更清晰,只有一個連續的內存塊在不同的步伐索引。

+0

明白了。似乎我必須找到另一種方式。謝謝您的幫助! – Wrath 2014-09-28 20:46:11