C++涉及矢量的代码在Visual Studio上工作正常,而在Linux上则不然
C++ code involving vector works fine on visual studio and not on linux
我正在为MRI扫描仪编码,基本要求是任何序列代码都应该在Visual Studio和Linux上运行。
我有一部分代码在Visual Studio 2008上运行良好;但是,在Linux上给了我错误。
有人可以告诉我我做错了什么吗?
template<typename type1, typename type2, typename type3>
void PrintVector3(type1 VectorIn_1, type2 VectorIn_2, type3 VectorIn_3) {
type1::const_iterator i1 = VectorIn_1.begin();
type2::const_iterator i2 = VectorIn_2.begin();
type3::const_iterator i3 = VectorIn_3.begin();
int lLenghtVec = VectorIn_1.n_elem();
for(int i = 0; i != lLenghtVec; ++i){
std::cout << std::setfill('0') << std::setw(3) << *i1 << " " << *i2 << " " << *i3 <<std::endl;
++i3; ++i2; ++i1;
}
}
我收到的错误:
CESTiPAT/ReorderInfo_DK.h:185: error: expected ';' before 'i1'
CESTiPAT/ReorderInfo_DK.h:186: error: expected ';' before 'i2'
CESTiPAT/ReorderInfo_DK.h:187: error: expected ';' before 'i3'
CESTiPAT/ReorderInfo_DK.h:191: error: 'i1' was not declared in this scope
CESTiPAT/ReorderInfo_DK.h:191: error: 'i2' was not declared in this scope
CESTiPAT/ReorderInfo_DK.h:191: error: 'i3' was not declared in this scope
你需要typename
:
typename type1::const_iterator i1 = VectorIn_1.begin();
typename type2::const_iterator i2 = VectorIn_2.begin();
typename type3::const_iterator i3 = VectorIn_3.begin();
区别不是因为平台,而是因为编译器。typename
是必需的,但 Windows 上的编译器还是接受了损坏的代码。以下是有关放置typename
(和template
(的更多信息。
type1::const_iterator
是一个依赖名称。你需要typename type1::const_iterator
.Visual Studio在这里是错误的。
你应该写:
typename type1::const_iterator i1 = VectorIn_1.begin();
typename type2::const_iterator i2 = VectorIn_2.begin();
typename type3::const_iterator i3 = VectorIn_3.begin();
因为const_iterator
名称取决于模板参数。VSC++以没有正确实现这部分语言而闻名。
正如其他人指出的那样,您需要typename
.
#include <vector>
#include <iostream>
#include <iomanip>
template<typename type1, typename type2, typename type3>
void PrintVector3(type1 VectorIn_1, type2 VectorIn_2, type3 VectorIn_3) {
typename type1::const_iterator i1 = VectorIn_1.begin();
typename type2::const_iterator i2 = VectorIn_2.begin();
typename type3::const_iterator i3 = VectorIn_3.begin();
int lLenghtVec = VectorIn_1.size();
for(int i = 0; i != lLenghtVec; ++i){
std::cout << std::setfill('0') << std::setw(3) << *i1 << " " << *i2 << " " << *i3 <<std::endl;
++i3; ++i2; ++i1;
}
}
int main() {
std::vector<int> v(3, 1);
PrintVector3(v, v, v);
}
但是你在Linux上使用哪个编译器呢?GCC 4.7.3给出了非常明确的信息:
$ g++ -Wall -Wextra vect.cpp
vect.cpp: In function ‘void PrintVector3(type1, type2, type3)’:
vect.cpp:9:5: error: need ‘typename’ before ‘type1:: const_iterator’ because ‘type1’ is a dependent scope
vect.cpp:9:28: error: expected ‘;’ before ‘i1’
vect.cpp:10:5: error: need ‘typename’ before ‘type2:: const_iterator’ because ‘type2’ is a dependent scope
vect.cpp:10:28: error: expected ‘;’ before ‘i2’
vect.cpp:11:5: error: need ‘typename’ before ‘type3:: const_iterator’ because ‘type3’ is a dependent scope
vect.cpp:11:28: error: expected ‘;’ before ‘i3’
vect.cpp:15:60: error: ‘i1’ was not declared in this scope
vect.cpp:15:79: error: ‘i2’ was not declared in this scope
vect.cpp:15:99: error: ‘i3’ was not declared in this scope
vect.cpp: In instantiation of ‘void PrintVector3(type1, type2, type3) [with type1 = std::vector<int>; type2 = std::vector<int>; type3 = std::vector<int>]’:
vect.cpp:23:23: required from here
vect.cpp:9:5: error: dependent-name ‘type1:: const_iterator’ is parsed as a non-type, but instantiation yields a type
vect.cpp:9:5: note: say ‘typename type1:: const_iterator’ if a type is meant
vect.cpp:10:5: error: dependent-name ‘type2:: const_iterator’ is parsed as a non-type, but instantiation yields a type
vect.cpp:10:5: note: say ‘typename type2:: const_iterator’ if a type is meant
vect.cpp:11:5: error: dependent-name ‘type3:: const_iterator’ is parsed as a non-type, but instantiation yields a type
vect.cpp:11:5: note: say ‘typename type3:: const_iterator’ if a type is meant
vect.cpp:7:6: warning: unused parameter ‘VectorIn_2’ [-Wunused-parameter]
vect.cpp:7:6: warning: unused parameter ‘VectorIn_3’ [-Wunused-parameter]
您的问题的答案在输出的第 2 行!
Clang 3.1 同样说:
$ clang -c vect.cpp
vect.cpp:9:5: error: missing 'typename' prior to dependent type name
'type1::const_iterator'
type1::const_iterator i1 = VectorIn_1.begin();
^~~~~~~~~~~~~~~~~~~~~
typename
相关文章:
- 这些是什么样的错误?即使我不在 Linux 上工作,我也遇到了 Linux 错误
- 编译在我的 Mac 上工作,但在集群 (Linux) 上不起作用
- 声明C++具有动态大小的数组类型在 Linux 中工作正常,但不能在 Windows 中工作
- 为什么带lcov的codecov在Travis上不能正常工作,而在我当地的Linux Mint上却不能正常工作
- popen() 如何工作以及如何在 Linux 上实现它到 C++ 代码中
- Linux C++读取 UART 设备无法始终如一地工作
- Makefile在Linux中工作,但在Windows下不起作用,在子目录中找不到文件
- C++代码在 Linux 上按预期工作,但在 Windows 上则不然
- QtCUrl post不再工作(Linux nok...窗口确定)
- C++程序使用 mingw 在 Linux 上交叉编译在 MSy2 中工作,但不能直接在 Windows 中工作
- 将Linux打开,读,写,关闭功能以在Windows上工作
- Linux 中共享库中的单例无法按预期工作
- 为什么我的文件/目录操作在Windows上工作正常,但在Linux上不能
- C++:printf 和 wprintf 不能一起工作 (Linux)
- 在macOSX上编译boost_asio/example/ssl/server.cpp可以在Linux上工作
- 为什么这个c++代码在Linux中可以正常工作,而在Windows中却不能
- 可执行文件无法正常工作 [Linux]
- 无法在 Linux 上进行回车工作
- 哔哔声不工作(linux)
- Qt 5.3 QSystemTrayIcon不正常工作[Linux]