gpt4 book ai didi

c++ - 具有重载 ostream<< 运算符的递归类模板

转载 作者:行者123 更新时间:2023-11-30 04:00:50 28 4
gpt4 key购买 nike

我正在尝试实现一个简单的 N 维数组。这似乎或多或少地正常工作,但我就是无法让它重载的 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 ==========

我确实已经尝试了所有的想法,包括删除 friend 关键字和实际的类参数,但没有任何改变。我们如何为此类类模板重载运算符?

干杯, 乔伊

最佳答案

您的方法的问题是无法推断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

In certain contexts, however, the value does not participate in type deduction, but instead uses the values of template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

作为旁注:使用模板化递归代码为多维数组或 vector 实现复杂结构是一条不易维护且肯定难以阅读的途径,以实现本来可以更快完成的事情,更高效(更少的分配)和更清晰,只有一个连续的内存块以不同的步幅索引。

关于c++ - 具有重载 ostream<< 运算符的递归类模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26089189/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com