2017-10-29 109 views
-4

我想输出这个数组,我不知道我是不是一个白痴,解决方案是盯着我,还是有更多的技术在这里发生。输出二维数组

代码:

for (string i=0;i<row; i++) 
    { 
     for (string j=0; j<col; j++) 
     { 
      out << Array[i][j]; 
     } 
    } 

请注意,Arraychar数据类型。 rowcol被定义为字符串。

我正的错误是从视觉工作室(3个错误):

cpp(97): error C2676: binary '++': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator 

cpp(99): error C2676: binary '++': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator 

cpp(101): error C2677: binary '[': no global operator found which takes type 'std::string' (or there is no acceptable conversion) 

请注意,我试图用整数,而不是为计数器字符串,但我得到了很多错误...

+0

为什么字符串?如果你想增加它,使用一个数字类型,例如'int' – user463035818

+0

因为如果我把'i'和'j'改成int,我会得到更多的错误。 – Bane

+0

“请注意,Array是char数据类型,row和col被定义为字符串。”我很抱歉,但这没有意义,“因为如果我把'i'和'j'改成int,我会犯很多错误,实际上很有趣。无论如何,请提供[mcve]。 – George

回答

0

我同意评论。你不应该为了去除错误或警告而打击编译器,而是试着理解它们的含义并正确地解决它们。在你的情况下,它告诉你j++i++是非法的,因为std::string类型没有为其定义operator++

我建议你Array数据类型的行数和列定义为int,那么你可以写:

for (int i=0;i<row; i++) 
    { 
     for (int j=0; j<col; j++) 
     { 
      std::cout << Array[i][j]; 
     } 
    } 

假设operator<<超载的Array类型,否则该行:

std::cout << Array[i][j]; 

也是非法的。