C++涉及矢量的代码在Visual Studio上工作正常,而在Linux上则不然

C++ code involving vector works fine on visual studio and not on linux

本文关键字:工作 Linux 而在 Studio Visual 代码 C++      更新时间:2023-10-16

我正在为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