1

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

4 回答 4

7

你需要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 上的编译器无论如何都接受了损坏的代码。这是有关放置位置(和)的更多信息。typenametemplate

于 2013-10-08T15:51:04.280 回答
3

type1::const_iterator是一个从属名称。你需要typename type1::const_iterator. Visual Studio 在这里是错误的。

于 2013-10-08T15:51:39.057 回答
2

你应该写:

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++ 以没有正确实现这部分语言而闻名。

于 2013-10-08T15:51:23.397 回答
2

正如其他人指出的那样,您需要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
于 2013-10-08T15:59:04.553 回答