【问题标题】:How can I fix this error "no matching function for call to ‘regex_replace..` "?如何解决此错误“没有匹配函数调用‘regex_replace..`”?
【发布时间】:2020-09-25 06:29:20
【问题描述】:

给定以下代码:

std::string str;
str= std::regex_replace(str, std::regex("\r"), ""); // ERROR

我收到以下错误:
no matching function for call to ‘regex_replace(std::string&, std::regex, const char [1])’

我该如何解决这个错误?

【问题讨论】:

  • 很幼稚的问题,但你记得导入<regex>吗?
  • 我写了#include <regex>
  • Works for me。有问题的话,一定是代码没有显示出来。准备一个minimal reproducible example

标签: regex c++11 compiler-errors


【解决方案1】:
**I've had this problem before.**  
The following is the phenomenon that I test the same piece of code on different versions of G++.  
    
### 1.code
    
    
    
        #include<bits/stdc++.h>  
        using namespace std;  
        int main()   
        {  
            string str="abc111abc222";  
            regex reg("abc");  
            string solve=regex_replace(str,reg,"");  
            cout<<solve<<endl;  
            return 0;  
        }
    
     
    
### 2.Test
    
    
#### (1)The origin of the problem:g++ version 4.8.1
    
Testing environment:    
DevC++5.9.2     
g++ version 4.8.1 (tdm64-2)   
Whether to enable the c++11 option:    
Yes.Turn on`-std=c++11`
    
    
    Error:
    
    `[Error] no matching function for call to 'regex_replace(std::string&, std::regex&, const char [1])'`
    
    
    
#### (2)g++ 6.5.0
    
Testing environment:     
g++ 6.5.0  
Output:`111222`
    
    
#### (3)clang++11
    
Testing environment:  
In site of “nowcoder”  
C++(clang++11)  
Output:`111222`
    
    
### 3、The final solution:
    
    We could choose a later version of the G++ compiler.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-11
    • 2020-07-07
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2016-01-18
    相关资源
    最近更新 更多