首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

错误:不匹配'operator*‘(操作数类型为' std::string {aka std basic_string<char>}’和{aka std basic_string<char>}')

错误:不匹配'operator*‘(操作数类型为' std::string {aka std basic_string<char>}’和{aka std basic_string<char>}')

这个错误是由于在C++中,字符串之间不能直接使用乘法运算符进行操作。乘法运算符只能用于数字类型的操作数,用于执行乘法运算。如果想要对字符串进行重复操作,可以使用循环或者字符串拼接的方式来实现。

例如,如果想要将字符串重复3次,可以使用循环的方式:

代码语言:txt
复制
std::string str = "Hello";
std::string result = "";
for (int i = 0; i < 3; i++) {
    result += str;
}

或者使用字符串拼接的方式:

代码语言:txt
复制
std::string str = "Hello";
std::string result = str + str + str;

在云计算领域中,字符串的乘法运算并不常见,因此也没有相关的推荐产品和链接地址。

相关搜索:错误:无法在赋值中将“std::string {aka std::basic_string<char>}”转换为“int”错误:无法在赋值中将“std::string* {aka std::basic_string<char>*}”转换为“node*”不匹配'operator<<‘(操作数类型为'std::ostream {aka std::basic_ostream<char>}’错误:在C++中从‘char’转换为非标量类型‘std::string {aka std::basic_string}’错误:将‘const string {aka const std::__cxx11::basic_string<char>}’作为‘this’参数传递错误:调用‘std::map<std::__cxx11::basic_string<char>’时没有匹配的函数错误:没有与‘std::__cxx11::basic_string<char>::basic_string(int&)’调用匹配的函数如何修复错误:在使用字符串和堆栈时,'operator<<‘(操作数类型为'std::ostream {aka std::basic_ostream<char>}’和'void')不匹配不匹配‘operator<<’(操作数类型为‘std::ostream’{又名‘std::basic_ostream<char>’}和‘const std::type_index’)不匹配"operator<<“(操作数类型为”std::ostream“错误:‘operator<<’没有匹配项(操作数类型是‘std::ostream’{又名‘std::basic_ostream<char>’}和‘std::_List_iterator<int>’)在将路径作为参数传递时,出现" error : cannot convert 'std::__cxx11::string* { as std::__cxx11::basic_string<char>*}‘to 'const char*’错误无法编译捕获库:不匹配‘operator!=’(操作数类型为‘std::basic_istream<char>’和‘long int’)不匹配'operator=‘(操作数类型为'__gnu_cxx::__alloc_traits<std::allocator<std::vector<int> >>获取错误:尽管重载了<<运算符,但不匹配'operator<<‘(操作数类型为'std::basic_ostream<char>’和'Complex')Android NDK16错误(“运算符'<<‘不明确(操作数类型为'basic_ostream<char,std::char_traits<char> >’和'long NDK16‘)”继续收到此错误main.cpp:9:91: error: no match for‘operator<<’(操作数类型为‘std::basic_ostream’和‘const std::vector’C++ Error project.cpp:11:20: error:'operator[]‘不匹配(操作数类型为'std::__cxx11::list<int>’和'int')在operator>> :C++的实现过程中出错没有运算符匹配这些操作数操作数类型为: std::istream >> const双重错误
相关搜索:
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • C++的重载流输出运算符

    // 下列代码输出什么? #include #include // typedef basic_ostream ostream; class A { private:     int m1,m2; public:     A(int a, int b) {         m1=a;m2=b;     }     operator std::string() const { return "str"; }     operator int() const { return 2018; } }; int main() {     A a(1,2);     std::cout << a;     return 0; }; 答案是2018, 因为类basic_ostream有成员函数operator<<(int), 而没有成员函数operator<<(const std::string&), 优先调用同名的成员函数,故输出2018,相关源代码如下: // 名字空间std中的全局函数 /usr/include/c++/4.8.2/bits/basic_string.h: template inline basic_ostream<_CharT, _Traits>& operator <<(basic_ostream<_CharT, _Traits>& __os,             const basic_string<_CharT, _Traits, _Alloc>& __str) {     return __ostream_insert(__os, __str.data(), __str.size()); } // 类basic_ostream的成员函数 //  std::cout为名字空间std中的类basic_ostream的一个实例 ostream: __ostream_type& basic_ostream::operator<<(int __n); // 下列代码有什么问题,如何修改? #include #include class A { public:     int m1,m2; public:     A(int a, int b) {         m1=a;m2=b;     }     std::ostream& operator <<(std::ostream& os) {         os << m1 << m2; return os;     } }; int main() {     A a(1,2);     std::cout << a;     return 0; }; 类basic_ostream没有成员函数“operator <<(const A&)”, 也不存在全局的: operator <<(const basic_ostream&, const A&) 而只有左操作数是自己时才会调用成员重载操作符, 都不符合,所以语法错误。 有两种修改方式: 1) 将“std::cout << a”改成“a.operator <<(std::cout)”, 2) 或定义全局的: std::ostream& operator<<(std::ostream& os, const A& a) {     os << a.m1 << a.m2; return os; }

    04

    C/C++常见gcc编译链接错误解决方法

    用“-Wl,-Bstatic”指定链接静态库,使用“-Wl,-Bdynamic”指定链接共享库,使用示例: -Wl,-Bstatic -lmysqlclient_r -lssl -lcrypto -Wl,-Bdynamic -lrt -Wl,-Bdynamic -pthread -Wl,-Bstatic -lgtest ("-Wl"表示是传递给链接器ld的参数,而不是编译器gcc/g++的参数。) 1) 下面是因为没有指定链接参数-lz(/usr/lib/libz.so,/usr/lib/libz.a ) /usr/local/mysql/lib/mysql/libmysqlclient.a(my_compress.c.o): In function `my_uncompress': /home/software/mysql-5.5.24/mysys/my_compress.c:122: undefined reference to `uncompress' /usr/local/mysql/lib/mysql/libmysqlclient.a(my_compress.c.o): In function `my_compress_alloc': /home/software/mysql-5.5.24/mysys/my_compress.c:71: undefined reference to `compress' 2) 下面是因为没有指定编译链接参数-pthread(注意不仅仅是-lpthraed) /usr/local/mysql/lib/mysql/libmysqlclient.a(charset.c.o): In function `get_charset_name': /home/zhangsan/mysql-5.5.24/mysys/charset.c:533: undefined reference to `pthread_once' 3) 下面这个是因为没有指定链接参数-lrt /usr/local/thirdparty/curl/lib/libcurl.a(libcurl_la-timeval.o): In function `curlx_tvnow': timeval.c:(.text+0xe9): undefined reference to `clock_gettime' 4) 下面这个是因为没有指定链接参数-ldl /usr/local/thirdparty/openssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup': dso_dlfcn.c:(.text+0x4c): undefined reference to `dlopen' dso_dlfcn.c:(.text+0x62): undefined reference to `dlsym' dso_dlfcn.c:(.text+0x6c): undefined reference to `dlclose' 5) 下面这个是因为指定了链接参数-static,它的存在,要求链接的必须是静态库,而不能是共享库 ld: attempted static link of dynamic object 如果是以-L加-l方式指定,则目录下必须有.a文件存在,否则会报-l的库文件找不到:ld: cannot find -lACE 6) GCC编译遇到如下的错误,可能是因为在编译时没有指定-fPIC,记住:-fPIC即是编译参数,也是链接参数 relocation R_x86_64_32S against `vtable for CMyClass` can not be used when making a shared object 7) 下面的错误表示gcc编译时需要定义宏__STDC_FORMAT_MACROS,并且必须包含头文件inttypes.h test.cpp:35: error: expected `)' before 'PRIu64' 8) 下面是因为在x86机器(32位)上编译没有指定编译参数-march=pentium4 ../../src/common/libmooon.a(logger.o): In function `atomic_dec_and_test': ../../include/mooon/sys/atomic_gcc.h:103: undefined reference to `__sync_sub_and_fetch_4' 9) 下列错误可能是因为多了个“}” error: expected d

    03

    如何优雅的传递 stl 容器作为函数参数来实现元素插入和遍历?

    开始正文之前,做一些背景铺垫,方便读者了解我的工程需求。我的项目是一个客户端消息分发中心,在连接上消息后台后,后台会不定时的给我推送一些消息,我再将它们转发给本机的其它桌面产品去做显示。后台为了保证消息一定可以推到客户端,它采取了一种重复推送的策略,也就是说,每次当我重新连接上后台时,后台会把一段时间内的消息都推给我、而不论这些消息之前是否已经推送过,如果我不加处理的直接推给产品,可能造成同一个消息重复展示多次的问题。为此,我在接收到消息后,会将它们保存在进程中的一个容器中,当有新消息到达时,会先在这个容器里检查有没有收到这条消息,如果有,就不再转发。

    02
    领券