c++ - 模板别名未被识别为有效
问题描述
我有以下问题:
template<class S>
void setAtIdx(int idx, std::vector<S> toSet) {
cdVec container = cdVec(toSet.size);
std::transform(toSet.begin(), toSet.end(), container,
[](S el) -> std::complex<double>{return std::complex<double>(el);});
if (isHorizontallyPacked()) { m_rows[idx] = container; }
else { m_cols[idx] = container; }
};
template<class S> using matS = std::vector<std::vector<S>>;
void setData(matS<S> dat) {
// same issue pops up when I do setData(matS dat)
if (isHorizontallyPacked()) {m_rows = dat;}
else {m_cols = dat;}
}
我的编译器给我带来了问题,setData
并且正在吐出error: ‘S’ was not declared in this scope
和error: template argument 1 is invalid
.
当我这样做时问题就消失了
template<class S> ;
void setData(std::vector<std::vector<S>> dat) {
// same issue pops up when I do setData(matS dat)
if (isHorizontallyPacked()) {m_rows = dat;}
else {m_cols = dat;}
}
看起来它们会是一样的吗?
解决方案
您必须为 using 和函数指定模板:
#include <vector>
template<class S>
void setAtIdx(int idx, std::vector<S> toSet) {
cdVec container = cdVec(toSet.size);
std::transform(toSet.begin(), toSet.end(), container,
[](S el) -> std::complex<double>{return std::complex<double>(el);});
if (isHorizontallyPacked()) { m_rows[idx] = container; }
else { m_cols[idx] = container; }
};
template<class S>
using matS = std::vector<std::vector<S>>;
template<class S>
void setData(matS<S> dat) {
// same issue pops up when I do setData(matS dat)
if (isHorizontallyPacked()) {m_rows = dat;}
else {m_cols = dat;}
}
推荐阅读
- regex - 使用简单的英语为 AutoHotkey 生成正则表达式?
- javascript - 如何配置 playwright-jest 以排除测试套件(规范)文件以进行运行?
- groovy - GroovyScriptEngine 官方演示不适用于动态脚本依赖重新加载
- php - 未定义类型 'Dotenv\Dotenv\Dotenv'
- c++ - OpenGL清除颜色Alpha通道不起作用
- cpu - 每个 cpu 指令是否都由其核心的逻辑门实现?
- gcc - GCC 使用什么来代替 GEP?
- c++ - 无法提供正确结果的 cin 问题
- javascript - 我的 bot 命令处理程序运行不佳
- python - 即使在确保它是一个字符串之后,将文本转换为小写的问题