“operator<<”不匹配(操作数类型为 'std::ostream {aka std::basic_ostream<char>}' 和 'std::_Setfill<const char*>')

no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::_Setfill<const char*>')

提问人:cutelittlebunny 提问时间:6/23/2023 最后编辑:cutelittlebunny 更新时间:6/24/2023 访问量:57

问:

我刚开始在学校学习cpp。我试图做这件事:

请按照输出示例的格式输出NxN斜纹地砖的设计和颜色。

注意:斜纹地砖的设计和颜色是这样的。首先,从左上角到右下角的对角线上的每个网格都是一个“@”符号,对角线向左和向右移动三个网格,即间隔为两个网格后线上的图案也是“@”,然后在两侧继续重复,直到超过地砖的范围。如果没有“@”模式,则用“-”符号表示。

EXAMPLE =
INPUT
10

OUTPUT
@--@--@--
-@--@--@-
--@--@--@
@--@--@--
-@--@--@-
--@--@--@
@--@--@--
-@--@--@-
--@--@--@

我尝试使用 setw 和 setfill,这是我的代码:

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main(){;
    int a,i,n;
    cin >> a;
    n=a;

    for(i=1;i<=a;i++){
        cout<<setw(a);
        if(n==1||(n-1)%3==0){
            cout<<setfill("@--")<<"@";
            cout<<endl;
        }
        if(n==2||(n-2)%3==0){
            cout<<setfill("-@-")<<"-";
            cout<<endl;
        }
        if(n==3||(n-3)%3==0){
            cout<<"--";
            cout<<setfill("--@")<<"--";
            cout<<endl;
        }
        n=n-1;
    }
return 0;
}

我试图做的是尝试从行的后面输入,因为据我所知,setw 和 setfill 从输入行的后面填充。

我设置了 3 个场景,从“@”、“-”或“--”开始。在满足场景后,我尝试用“@--”、“-@-”或“--@”填充剩余的输入宽度。

但是,当我尝试编译文件时,编译器说:

no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::_Setfill<const char*>')

我不知道错误是什么以及如何解决它。

我遇到的错误是什么,如何解决?另外,如果可能的话,是否有另一种最佳的编写代码的方法?

非常感谢。

编辑:我重写了代码,这次使用了更多的数组,if和for循环。它终于奏效了。感谢您的见解!

代码如下:

#include<iostream>
using namespace std;

int main(){;
    int a;
    cin >> a;
    char one[a];
    char two[a];
    char three[a];

    int eFst,eScd,eTrd;
    //1,4,7... line elements
    for(eFst=0;eFst<a;eFst++){
        if((eFst-1)%3==0){
            one[eFst]='-';
            }
        if((eFst-2)%3==0){
            one[eFst]='-';
            }
        if((eFst-3)%3==0){
            one[eFst]='@';
            }
    }
    //2,5,8... line elements
    for(eScd=0;eScd<a;eScd++){
        if((eScd-1)%3==0){
            two[eScd]='@';
            }
        if((eScd-2)%3==0){
            two[eScd]='-';
            }
        if((eScd-3)%3==0){
            two[eScd]='-';
            }
    }
    //3,6,9... line elements
    for(eTrd=0;eTrd<a;eTrd++){
        if((eTrd-1)%3==0){
            three[eTrd]='-';
            }
        if((eTrd-2)%3==0){
            three[eTrd]='@';
            }
        if((eTrd-3)%3==0){
            three[eTrd]='-';
            }
    }
    
    int i,rFst,rScd,rTrd;       
//how many rows
    for(i=0;i<a;i++){
        //1,4,7 line and so on
        if((i-3)%3==0){
            for(rFst=0;rFst<a;rFst++){
                cout<<one[rFst];
            }
            cout<<endl;
        }
        //2,5,8 line and so on
        if((i-1)%3==0){
            for(rScd=0;rScd<a;rScd++){
                cout<<two[rScd];
            }
            cout<<endl;
        }
        //3,6,9 line and so on
        if((i-2)%3==0){
            for(rTrd=0;rTrd<a;rTrd++){
                cout<<three[rTrd];
            }
            cout<<endl;
        }
    }

return 0;
}

如果有办法优化我的代码,请告诉我,谢谢!

c++ 编译器错误 mingw-w64 iomanip

评论

0赞 NathanOliver 6/23/2023
std::setfill 接受字符,而不是字符串
0赞 Yksisarvinen 6/23/2023
这不是 std::setfill 的工作方式。如果设置了,则它需要用于填充的单个字符,并且要打印的元素短于传递给的值。std::setwsetw
0赞 Pete Becker 6/23/2023
作为风格问题,该循环始终是编写的。当您开始使用数组时,您会看到从 0 而不是 1 开始的重要性。这不是这里的代码中的问题;循环运行正确的次数。for (int i = 0; i < a; i++) ...
0赞 cutelittlebunny 6/23/2023
解决这个问题的最佳方法是什么?for 循环?而?阵 列?
0赞 dalfaB 6/24/2023
我认为最简单的是在线上的for循环与列上的for循环。如果行和列以 0 开头,则更容易。然后我们打印一个或一个'-'。唯一的挑战是找到在它们之间做出决定的测试。如果绘制了 column==line a,请看图片。查找所有情况 是使用模 3 的简单表达式。@@@

答:

1赞 463035818_is_not_an_ai 6/23/2023 #1

没有人对所有C++了如指掌。你需要习惯于一些可以查找东西的参考资料。我推荐 cppreference.com。

std::setfill

template< class CharT >
/*unspecified*/ setfill( CharT c );

它是具有未指定返回类型的函数模板。这听起来可能有点奇怪,但大多数 iomanipulator 都是函数。 表示字符类型。字符串和流以及大多数相关内容都根据字符类型进行参数化。当您使用实际的类型时(请参阅此处。因此,在这里您想使用 .模板参数可以推导出来,所以你只需要提供一个参数:CharTstd::coutstd::ostreamstd::basic_ostream<char>std::setfill<char>

 std::cout << std::setfill('.'); 

错误消息相当刺耳...

no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::_Setfill<const char*>')

std::_Setfill<const char*>是上面未指定的返回类型。在不同的标准库实现中,它可能是完全不同的类型。它只是您可以通过管道获得所需效果的东西。这是因为字符数组 a 已经衰减为指针。std::coutconst char*"@--"const char[4]

TL的;DR:不适用于字符串作为参数。std::setfill

评论

1赞 cutelittlebunny 6/23/2023
谢谢。换句话说,setfill 不能将字符串作为输入。