TR1 regex_replace with wstring in VS2010? - c++

#include <iostream>
#include <string>
#include <regex>
#include <ios>
#include <locale>
using namespace std;
int main ()
{
const wstring wstr(L"<(.|\\n)*?>");
static const wregex wr(wstr);
wstring line (L"<tag>Random text<tag>");
wstring line2 (L"");
wcout << regex_replace<wchar_t>(line,wr,line2) << endl;
}
Compiler says:
ClCompile:
html.cpp
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef "std::tr1::regex_replace"
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef of "std::tr1::regex_replace"
c:\users\usr\documents\visual studio 2010\projects\html\html\html.cpp(34): error C2784: std::basic_string<_Elem> std::tr1::regex_replace(const std::basic_string<_Elem> &,const std::tr1::basic_regex<_Elem,_RxTraits> &,const std::basic_string<_Elem> &,std::tr1::regex_constants::match_flag_type): not able to output argument template for "const std::tr1::basic_regex<_Elem,wchar_t> &" from "const std::tr1::wregex"
c:\program files\microsoft visual studio 10.0\vc\include\regex(2739): look to typedef "std::tr1::regex_replace"

Partial answer:
You should use regex_repalce this way:
wcout << regex_replace(line,wr,line2) << endl;
i.e., without the wchar_t. The first argument is for the Element Traits class, which you'd only rarely want to modify.
Edit
I've checked your code with VC++ 2010. Changing the line as I specified allowed the code to compile, and return the result as expected. Can you try it again?

Related

How to add #include <iostream> to project? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I just started learning C++ and I was trying to create a simple code with Visual Studio 2017.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int x;
std::cout >> "Enter Age";
std::cin >> x;
if (x >= 18) {
std::cout << "You are an adult." << endl;
}
else {
std::cout << "You are a child." << endl;
}
return 0;
}
But I think I was getting a lot of errors such as iostream didn't exist because I didn't see it in the header file or iostream.h
I tried creating a new header file and simply typing #include <iostream> but that didn't seem to work. Here are my errors.
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2784: 'std::basic_istream<char,_Traits> &std::operator >>
(std::basic_istream<char,_Traits> &,signed char &)': could not deduce
template argument for 'std::basic_istream<char,_Traits> &' from
'std::ostream'
1>c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1068):
note: see declaration of 'std::operator >>'
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2784: 'std::basic_istream<char,_Traits> &std::operator >>
(std::basic_istream<char,_Traits> &,signed char *)': could not deduce
template argument for 'std::basic_istream<char,_Traits> &' from
'std::ostream'
1>c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1061):
note: see declaration of 'std::operator >>'
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>
(std::basic_istream<_Elem,_Traits> &,_Elem &)': could not deduce template
argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
1>c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1036):
note: see declaration of 'std::operator >>'
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>
(std::basic_istream<_Elem,_Traits> &,_Elem *)': could not deduce template
argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
1>c:\program files (x86)\microsoft visual
studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(995): note:
see declaration of 'std::operator >>'
1>c:\users\munta\onedrive\documents\visual studio
2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error
C2676: binary '>>': 'std::ostream' does not define this operator or a
conversion to a type acceptable to the predefined operator
1>Done building project "ConsoleApplication3.vcxproj" -- FAILED.
Thanks for any help.
Check this: std::cout >> "Enter Age";. I suppose it to be std::cout << "Enter Age"; instead.
BTW, not related to the question, since you are writing std::cout and std::cin then it would be better if you keep the consistency with std::endl instead of endl.
Problem is in line 8. It shoud be:
std::cout << "Enter Age";

binary '>=': 'std::chrono::system_clock::time_point'

I am not sure how to get ride of this compiler error:
error C2676: binary '>=': 'std::chrono::system_clock::time_point'
#include <ctime>
#include <chrono>
int main()
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));
if (std::chrono::system_clock::now() >= now_c)
{
}
}
Here is what the compiler outputs:
1>------ Build started: Project: test, Configuration: Debug x64 ------
1> Source.cpp
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::operator >=(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)': could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'std::chrono::system_clock::time_point'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\utility(311): note: see declaration of 'std::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::chrono::operator >=(const std::chrono::duration<_Rep,_Period> &,const std::chrono::duration<_Rep2,_Period2> &)': could not deduce template argument for 'const std::chrono::duration<_Rep,_Period> &' from 'std::chrono::system_clock::time_point'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\chrono(538): note: see declaration of 'std::chrono::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::chrono::operator >=(const std::chrono::time_point<_Clock,_Duration> &,const std::chrono::time_point<_Clock,_Duration2> &)': could not deduce template argument for 'const std::chrono::time_point<_Clock,_Duration2> &' from 'time_t'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\chrono(905): note: see declaration of 'std::chrono::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2676: binary '>=': 'std::chrono::system_clock::time_point' does not define this operator or a conversion to a type acceptable to the predefined operator
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I recommend you to use type deduction with auto for this kind of case, as it makes the code much clearer. Also, as said in the comments above, the std::chrono facilities are not directly compatible with c-style time_t. I would recommend to keep using just std::chrono since it is more type-safe than it's counterpart.
#include <ctime>
#include <chrono>
#include <iostream>
int main()
{
auto now = std::chrono::system_clock::now();
auto now_c = now - std::chrono::hours(24);
if (std::chrono::system_clock::now() >= now_c)
{
std::cout << "it works!" << std::endl;
}
return 0;
}
You are try to compare a C++ time_point with a C time! And there is no operator >= to compare. Then you try to compare nanosecond with second
The time_point has a function named time_since_epoch and you can use it.
Using auto can help solve the problem but not understanding what happens and what is under the hood!
So you simply can compare(not good):
if ( now.time_since_epoch().count() >= now_c)
And the better code is:
std::chrono::duration_cast< std::chrono::seconds>(now.time_since_epoch()).count()
Because time_t is per second
if ( now.time_since_epoch().count() >= now_c){
std::cout << now.time_since_epoch().count() << '\n';
std::cout << std::chrono::duration_cast< std::chrono::seconds>(now.time_since_epoch()).count() << '\n';
std::cout << now_c << '\n';
}
the output:
1487879248873636085
1487879248
1487792848

Keyboard input on unsigned char?

uchar szPlaintext[128]; //dato da criptare
cout << "\nInserisci testo : ";
getline(cin, szPlaintext);
I tried it with a getline (cin, szPlaintext); but I have a lot of errors. I compile with VS2015.
Premise that I am trying to implement an AES (not mine), to my program; the uchar declaration was in this way.
uchar szPlaintext [128] = "text that I want to insert";
The errors are:
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2672: 'getline': no matching overloaded function found
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'uchar [128]'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(157): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(146): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'uchar [128]'
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(126): note: see declaration of 'std::getline'
1>d:\download\scuola\c++\virtualaes\virtualaes\encrypt.cpp(32): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': expects 3 arguments - 2 provided
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\string(73): note: see declaration of 'std::getline'
Pietrob0b
Utente Junior
Messaggi: 36
Iscritto il: 10 dic 2015, 20:44
std::getline takes a std::string reference, not a raw pointer.
string sPlaintext;
cout << "\nInserisci testo : ";
getline(cin, sPlaintext);
uchar* szPlaintext = (uchar*) sPlaintext.c_str(); //dato da criptare
As others have already pointed out std::getline takes a reference of std::string not "char *" , so your program has failed to compile .
Below are the two version of std::getline .
istream& getline (istream& is, string& str, char delim);
istream& getline (istream& is, string& str);
Now , how to make it work assuming you want input string in char* .Follow the below program .
// Example program
#include <iostream>
#include <string>
#include<cstring>
int main()
{
char szPlaintext[128]; //dato da criptare
std::cout << "\nInserisci testo : ";
std::string str ;
getline(std::cin, str);
strcpy(szPlaintext,str.c_str());
std::cout << szPlaintext;
}

My libraries are not recognized in visual studio Express 2015

I am doing a c++ assignment and I am using visual studio express. In my header.h file I have the following libraries:
#include <cstdlib>
#include<iostream>
#include <cstring>
#include<fstream>
#include<string>
#include <vector>
#include<iomanip>
#ifndef _HEADER_H_
#define _HEADER_H_
using namespace std;
and on my main.cpp and Source.cpp I have a reference to the Header.h:
#include"Header.h"
It seams that the libraries are not recognized since I get the following errors:
Severity Code Description Project File Line
Error C2079 'ss' uses undefined class 'std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>'
Error C2440 'initializing': cannot convert from 'std::string' to 'int'
Error C2780 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': expects 2 arguments - 3 provided
Error C2784 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
Error C2780 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)': expects 2 arguments - 3 provided
Error C2784 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'int'
Error C2780 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)': expects 2 arguments - 3 provided
Error C2784 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'int'
However the project runs fine in Visual studio professional. But we have been asked to do it in Express.
Please help
Thank you
The failing code would be nice, but it looks as if you are missing the
#include <sstream>
header. The full suite seems to have that included in some other header file, hence the different behavior.

error C2784 ,class in key map

I have a problem with the container map. I need to store my own class Person in key but i have error C2784 (i.e., "The compiler cannot determine a template argument from the supplied function arguments."). It's example from the book "Ivor Horton's beginning Visual C++ 2010"
#include<map>
#include<string>
#include <iostream>
using namespace std;
void main()
{
class Person{
public:
string c_name,c_surname;
Person(string name,string surname){
c_name=name;
c_surname=surname;
}
};
map<Person,string> phonebook;
phonebook.insert(make_pair(Person("Mel","GIBSON"),"24 32 23"));
phonebook[Person("Mel2","Gibson2")]="243 32 23";
/* it doesn`t work too
typedef pair<Person,string> Entry;
Entry entry1= Entry(Person("Jack","Jones"),"213 567 1234");
phonebook.insert(entry1);*/
system("Pause");
}
Error 1 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 2 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 3 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 4 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 5 error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 6 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 7 error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 8 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
Error 9 error C2676: binary '<' : 'const main::Person' does not define this operator or a conversion to a type acceptable to the predefined operator e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses
The problem here is that std::map requires your keys to be comparable with the < operator. Custom structures/classes are not that by default, you need to make a custom operator< for comparison.
In C++03 you could not use local classes (classes defined within functions) as template arguments.
In C++11 you can.
So one fix is to update the compiler (there is Visual C++ 2013), and another fix is to move the class definition out of main.
By the way, void main is invalid as standard C++, and as standard C, and it's more to type than standard int main. If your book has void main, then that's a very ungood book. Microsoft's examples that include void main are also very ungood.
Also, by the way, the
system("Pause");
at the end is also very ungood practice because
it is not necessary, has no advantage, but
it makes the program more difficult to use and has some other problems, and to top it all,
it's Windows-specific, non-portable code.
To run a console program so that it stops at the end
in Visual Studio use Ctrl+F5, or
in Visual Studio place a breakpoint at the end of main (just click in the left margin) and run it with debugging (e.g. via keypress F5), or
run it from a command interpreter.
UPDATE: the now added error messages (even the first one) mention operator<. You need to define that also. That is, define an operator< function for your class Person.