I simply want to match "{". But don't know why giving this error:
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted (core dumped)
Compilation on Ubuntu with g++ version 4.6.3
g++ -std=c++0x a.c
Program
#include<iostream>
#include<regex>
using namespace std;
main(int argc,char**argv){
if (regex_match("{1}" , std::regex ("[{]"))) {
cout<<"Hello World"<<endl;
}
}
I've also checked the ECMAScript details and this regular expression should match. It also does not match when I use like : std::regex ("\\{"))
What am I wrong?
You need at least gcc 4.9 to make regexps work with gcc, once you will have 4.9 version add .* to make it match the rest of the string:
if (regex_match("{1}" , std::regex ("[{].*"))) {
^^
http://coliru.stacked-crooked.com/a/99e405e66906804d
I have the same error with you! And my IDE is Clion, I choose the C++ version with Clion is C++17 and my test code is:
std::string pattern{ "http|hppts://\\w.*$" }; // url
std::regex re(pattern);
std::vector<std::string> str{ "http://blog.net/xxx",
"https://github.com/jasonhubs", "abcd://124.456",
"abcdhttps://github.com/jasonhubs"
};
for (auto tmp : str)
{
bool ret = std::regex_search(tmp, re);
if (ret) fprintf(stderr, "%s, can search\n", tmp.c_str());
else fprintf(stderr, "%s, can not search\n", tmp.c_str());
}
and I solve it by updating the gcc and g++.
sudo yum install centos-release-scl yum-utils
sudo yum-config-manager --enable rhel-server-rhscl-7-rpms
sudo yum install devtoolset-7
scl enable devtoolset-7 bash
Related
Given the following code:
std::string str;
str= std::regex_replace(str, std::regex("\r"), ""); // ERROR
I get the following error:
no matching function for call to ‘regex_replace(std::string&, std::regex, const char [1])’
How can I fix this error?
**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.
My system: macOS Catalina(10.15.4)
Brew config
Clang: 11.0 build 1103
Git: 2.21.0 => /usr/local/bin/git
Curl: 7.64.1 => /usr/bin/curl
Java: 1.8.0_101
macOS: 10.15.4-x86_64
CLT: 11.4.1.0.1.1586360307
Xcode: N/A
Issue: can't include some C++17 headers: execution, filesystem
while can include the other like: any, variant.
Most of C++17 features like structured binding, template parameters deduction etc works.
Example. Trying to build a file with the following code snippet:
#include <numeric>
#include <vector>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <utility> //std::pair
#include <tuple> // std::tie
#include <algorithm> // std::clamp
#include <optional> // std::optional
#include <map>
// ... other stuff
#include <execution>
namespace parallel_algoritmhs {
void show() {
std::vector<int> v = {1,2,3,4,5,6};
std::for_each(std::execution::par, v.begin(), v.end(), [](auto& e) {e+=100;});
int main() {
//... use
}
I am building with:
g++ -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
Update:
I have tried suggested:
clang -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
clang++ -std=c++17 -fsanitize=undefined -fno-sanitize-recover=all -o main c++17.cpp
And getting the same result.
If I comment out code snippet related to different execution strategies, while leaving other code snippets like:
#include <any>
namespace any {
void show() {
std::any v = 42;
v = 4.2;
v = std::string{"hello"};
std::cout << std::any_cast<std::string>(v) << '\n';
}
}
namespace map_cpp17 {
void show() {
std::map<int, std::string> myMap{ { 1, "Gennady" }, { 2, "Petr" }, { 3, "Makoto" } };
auto node = myMap.extract(2);
node.key() = 42;
myMap.insert(std::move(node));
std::map<int, std::string> m1{ { 1, "aa" }, { 2, "bb" }, { 3, "cc" } };
std::map<int, std::string> m2{ { 4, "dd" }, { 5, "ee" }, { 6, "ff" } };
m1.merge(m2);
std::map<int, std::string> m; m.emplace(1, "aaa"); m.emplace(2, "bbb"); m.emplace(3, "ccc");
auto [it1, inserted1] = m.insert_or_assign(3, "ddd"); std::cout << inserted1; // 0
auto [it2, inserted2] = m.insert_or_assign(4, "eee"); std::cout << inserted2; // 1
}
}
Everything compiles
With
#include <execution>
getting:
fatal error: 'execution' file not found
#include <execution>
I wonder what might be the problem behind that and how to fix this.
Thank you!
Update 2:
brew info llvm:
llvm: stable 10.0.0 (bottled), HEAD [keg-only]
Next-gen compiler infrastructure
https://llvm.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/llvm.rb
==> Dependencies
Build: cmake ✔, python#3.8 ✘
Required: libffi ✘
==> Requirements
Build: xcode ✘
==> Options
--HEAD
Install HEAD version
==> Caveats
To use the bundled libc++ please add the following LDFLAGS:
LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
llvm is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
Update 3:
clang --version
Apple clang version 11.0.3 (clang-1103.0.32.59)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
You're not actually using the Clang you installed. The official LLVM Clang doesn't have a version 11.0! LLVM is only on version 10.0 (check brew info llvm). You're using Apple Clang, which comes with its own installation of libc++. Presumably to catch this exact type of problem, Apple Clang responds to clang --version/clang++ --version with the "future" version 11.0. However, Apple's distribution is actually a bit behind the state of the art, and it simply isn't updated to these features.
When you install LLVM with Homebrew, it doesn't automatically set it up the new clang/clang++ to be easily callable, because things might expect clang to specifically mean "Apple Clang" and break if that changes. You should add /usr/local/opt/llvm/bin (which Homebrew symlinks into the LLVM installation) to your PATH (which is where your shell and other programs search for programs). In an already running shell session
export PATH="/usr/local/opt/llvm/bin:$PATH"
will switch to using the newly installed LLVM Clang until you exit the session. Putting this line in a shell startup file, like ~/.bash_profile, will set up this adjustment to be applied every time you open a new shell.
Alternatively, like #Eljay does, you could just manually type out the full path to the new clang/clang++ (/usr/local/opt/llvm/bin/clang/-++), thereby bypassing the search into PATH, but that's a pain. In any case, you'll know you got it right if clang --version/clang++ --version gives you 10.0.0.
I installed boost from synaptic.
Now i need to convert date from/to string, but when I write code like below,
date dt{2018-9-14};
string str=to_simple_string(dt);
cout<<str<<endl;
it gives an error:
/usr/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
/usr/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
How can I solve this???
As the other answer stated, the constructor is wrong (use commas, or you'll simply say 2018-9-14 which is equal to 1995).
Next, you forgot to link the boost_date_time library:
Starting from a completely new and fresh 16.04 machine:
apt update; apt install -yy build-essential libboost-all-dev
echo -e '#include <boost/date_time.hpp>\nint main(){std::cout<<boost::gregorian::to_simple_string({2018,9,14})<<std::endl;}' > test.cpp
g++ -std=c++11 test.cpp -lboost_date_time && ./a.out
Works and prints
2018-Sep-14
Teach a man how to fish: What is an undefined reference/unresolved external symbol error and how do I fix it?
Show a man eating the fish:
Your initialization of dt looks suspicious. I think you want dt{2018, 9, 14} if you want 14th Septmber 2018.
A fuller version of your code might look like:
#include <boost/date_time.hpp>
int main(int argc, char* argv[]) {
const boost::gregorian::date dt{2018, 9, 14};
const std::string str = boost::gregorian::to_simple_string(dt);
std::cout << str << std::endl;
return 0;
}
which correctly produces
2018-Sep-14
as its output.
I have a problem compiling code using std::variant.
I try to compile this code with g++ 5.4/6.2 on ubuntu and fedora with -std=c++17:
#include <variant>
#include <string>
int main()
{
std::variant<int, float> v, w;
v = 12; // v contains int
int i = std::get<int>(v);
w = std::get<int>(v);
w = std::get<0>(v); // same effect as the previous line
w = v; // same effect as the previous line
try {
std::get<float>(w); // w contains int, not float: will throw
}
catch (std::bad_variant_access&) {}
std::variant<std::string> v("abc"); // converting constructors work when unambiguous
v = "def"; // converting assignment also works when unambiguous
}
found on cppreference.com but this error append: "fatal error: variant: No such file or directory".
Install g++-7; e.g. if ubuntu
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt-get update
sudo apt-get install g++-7
run /usr/bin/g++-7 -std=c++1z your_program.cc
Optional, if you use cmake system, add these lines :
set(CMAKE_CXX_COMPILER "/usr/bin/g++-7")
set(CMAKE_CXX_FLAGS "-std=c++1z")
std::variant was added in C++17.
gcc does not yet fully support the relevant bits of the C++17 standard.
I don't even see std::variant listed in the gcc's tracking page.
Consider this code (badcast.cpp):
#include <exception>
#include <typeinfo>
#include <stdio.h>
class foo {
public:
virtual ~foo() {}
};
class bar: public foo {
public:
int val;
bar(): val(123) {}
};
static void
cast_test(const foo &f) {
try {
const bar &b = dynamic_cast<const bar &>(f);
printf("%d\n", b.val);
} catch (const std::bad_cast &) {
printf("bad cast\n");
}
}
int main() {
foo f;
cast_test(f);
return 0;
}
FreeBSD 9.1:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap (core dumped)
$ g++ badcast.cpp -o badcast -frtti -fexceptions -Wall && ./badcast
terminate called after throwing an instance of 'std::bad_cast'
what(): std::bad_cast
Abort trap (core dumped)
$ gcc -v
Using built-in specs.
Target: amd64-undermydesk-freebsd
Configured with: FreeBSD/amd64 system compiler
Thread model: posix
gcc version 4.2.1 20070831 patched [FreeBSD]
$ uname -a
FreeBSD freebsd9 9.1-RELEASE FreeBSD 9.1-RELEASE #0 r243825: Tue Dec 4 09:23:10 UTC 2012 root#farrell.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC amd64
Debian Linux 6:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
bad cast
OS X 10.8:
$ g++ badcast.cpp -o badcast -Wall && ./badcast
bad cast
Why does catching bad_cast not work on FreeBSD?
As a wild guess, there’s a chance in FreeBSD you might be using LLVM’s new libc++, instead of the old GNU libstdc++. FreeBSD has been working towards switching over to the LLVM toolchain, away from GNU GPL licensed software.
Apple’s moving that way too, and in the past I’ve ran into issues developing for Mac using libc++ that libstdc++ didn’t have (especially with Boost).
You can use ldd to confirm what libraries you’re linking against:
ldd ./badcast
If it is linking against libc++, you might want to file the bug and test case with the LLVM project.