Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
为什么我不能做

cout << 3*" ";


错误:

E:\C++\test\main.cpp|12|error: invalid operands of types 'int.' and 'const char [2]' to binary 'operator*'

最佳答案

某些语言允许以这种方式使用乘法运算符。例如,Python允许您编写:

3*" "


并将其评估为

"   "


但是C ++不允许使用乘法运算符。这正是编译错误告诉您的内容。

您试图创建一个包含三个空格的字符串。例如,通过使用标准字符串类的fill构造函数来执行此操作:

std::string(3, ' ')


并将其发送到cout

std::cout << std::string(3, ' ');

08-26 16:58