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";
Related
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;
}
I was trying to make a calculator for one of my firsts projects in C++ but I was getting an error.
Here's my code:
#include <iostream>
#include <cmath>
using namespace std;
int a;
int b;
int sum;
int opp;
int yn;
int main()
{
cout << "Enter your first value" << endl;
cin >> a;
cout << "Type the number that corrosponds to your opperation \n 1.add \n 2.subtract \n 3.mutipliy \n 4.divide" << endl;
cin >> opp;
cout << "Choose your last value" << endl;
cin >> b;
return 0;
}
int action()
{
switch(opp)
{
case 1:
sum = a + b;
cout << "Your answer is " << sum << endl;
break;
case 2:
sum = a - b;
cout << "Your answer is " << sum << endl;
break;
case 3:
sum = a * b;
cout << "Your answer is " << sum << endl;
break;
case 4:
sum = a / b;
cout << "Your answer is " << sum << endl;
break;
case >> 5:
cout << "Type the number that corrosponds to your opperation \n 1.add \n 2.subtract \n 3.mutipliy \n 4.divide"
}
cout << "Would you like to use another oppperation? \n Type the number that relates to the answer you want to choose \n 1.Yes \n 2.No" << endl;
cin >> yn;
if (yn = 1)
{
cout << "what would you like your nxt opperation to be? \n 1.add \n 2.subtract \n 3.mutiply \n 4.divide" << endl;
cin >> opp;
return 0;
}
}
I know it is not the best written code but I am more focused on getting it to work.
If it helps the error code I was getting was:
1>------ Build started: Project: ConsoleApplication1, Configuration: Debug Win32 ------
1> calculator.cpp
1>c:\users\"my acount"\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\calculator.cpp(12): error C2365: 'yn' : redefinition; previous definition was 'function'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\math.h(1002) : see declaration of 'yn'
1>c:\users\"my account"\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\calculator.cpp(56): error C2059: syntax error : '>>'
1>c:\users\"my account"\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\calculator.cpp(61): error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'double (__cdecl *)(int,double)' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(485): could be 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_streambuf<char,std::char_traits<char>> *)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(466): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(void *&)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(448): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(long double &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(430): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(double &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(411): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(float &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(392): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned __int64 &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(373): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(__int64 &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(353): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned long &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(335): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(long &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(317): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned int &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(291): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(int &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(272): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned short &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(237): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(short &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(218): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::_Bool &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(211): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(204): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_ios<char,std::char_traits<char>> &(__cdecl *)(std::basic_ios<char,std::char_traits<char>> &))'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(198): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_istream<char,std::char_traits<char>> &(__cdecl *)(std::basic_istream<char,std::char_traits<char>> &))'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1103): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><char,std::char_traits<char>,double(int,double)>(std::basic_istream<char,std::char_traits<char>> &&,_Ty (__cdecl &))'
1> with
1> [
1> _Ty=double (int,double)
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1093): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,unsigned char &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1086): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,unsigned char *)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1079): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,signed char &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1072): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,signed char *)'
1> while trying to match the argument list '(std::istream, double (__cdecl *)(int,double))'
1>c:\users\"my account"\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1\calculator.cpp(63): error C2659: '=' : function as left operand
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If you can try and explain the answer so I can lern from it, as this is my first project.
int yn;
Here you re-declared yn because math.h already has a variable named yn:
extern double yn _PARAMS((int, double));
So rename yn.
case >> 5: is wrong, change it to case 5:
if (yn = 1) will always be true, as its an assignment operation rather Comparison operator (==). Rewrite it as if (yn == 1).
According to the error message, your <cmath> defines (or includes something that defines) something called yn.
And since you're pulling everything from inside std:: to the global namespace (with using namespace std;), your own yn clashes with the yn inside std::.
Solution: stop using using namespace std;.
Well lets just read the output of the compiler.
\calculator.cpp(12): error C2365: 'yn' : redefinition;
previous definition was 'function'
\include\math.h(1002) : see declaration of 'yn'
that first line tells us that there is already of definition of 'yn'
being used. That second line
tells us where: In the math.h file you included. To fix this, choose
another name for 'yn' in your main.cpp file (or you can remove the "using namespace std" line, and append "std::" to the beginning of every call to cout and so on, but I don't think you will know whats really going on there at this stage)
\calculator.cpp(56): error C2059: syntax error : '>>'
This is just saying that there is incorrect syntax, and rightfully so. Take those << out of your last "case", and make it look like every other case you did before that, e.g. "case 5:" This would be just a simple typo on your part.
\calculator.cpp(61): error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'double (__cdecl *)(int,double)' (or there is no acceptable conversion)
This is something I believe is also a result of the redefinition of "yn" and it is treating "yn" as something you don't want it to be, and it doesn't make sense for the >> operator to be used on it. You would use it on a string for instance, as you probably know.
After that it looks like it lists all the ways you can use it, which for the most part isn't immediately important to you.
\calculator.cpp(63): error C2659: '=' : function as left operand
Here you are mistaking the assignment operator (=) for the comparison operator (==). The former assigns values as you have been doing, and the latter produces a boolean of "true" or "false" for usage in, say, if statements. You want the latter, so replace the "=" with "==".
Learn to read through the output of the compiler. It actually says useful stuff, you just have to learn to use it.
I'm studying Beginning C++ Through Game Programming
In the book I have this example:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main (){
string word1 = "Game";
string word2("Over");
string word3(3, '!');
string phrase= word1 + word2 + word3;
string phrase= word1 + "" + word2 + word3;
cout<< " The phrase is: " << phrase << "\n \n";
cout<< " The phrase has " << phrase.size() << "on it! \n\n";
cout<< " The character at position 0 is " << phrase[0] << "on it! \n\n";
cout<< "Changing the character at position 0 \n\n";
phrase[0] = 'V';
cout << "The phrase now is " << phrase << "\n\n";
for( unsigned int i=0; i < phrase.size() ; i++){
cout << " Character at position "<< i << "is : " << phrase[i] << "\n" ;
}
cout << "The word 'Over' begin at " << phrase.find("Over") << endl;
if (phrase.find("eggplant")==string::npos)
{
cout<<"'Eggplant' is not in the phrase"<<endl;
}
phrase.erase(4,5);
cout<<"phrase now is" << phrase << endl;
phrase.erase();
if(phrase.empty())
{
cout << "phrase is empty" << endl;
}
getchar();
}
When I try to run the code, the debug gives me back this:
1>------ Build started: Project: GameTestExample, Configuration: Debug Win32 ------
1> gameover.cpp
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_String_iterator<_Mystr> std::operator +(_String_iterator<_Mystr>::difference_type,std::_String_iterator<_Mystr>)' : could not deduce template argument for 'std::_String_iterator<_Mystr>' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_String_const_iterator<_Mystr> std::operator +(_String_const_iterator<_Mystr>::difference_type,std::_String_const_iterator<_Mystr>)' : could not deduce template argument for 'std::_String_const_iterator<_Mystr>' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::move_iterator<_RanIt> std::operator +(_Diff,const std::move_iterator<_RanIt> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_Array_iterator<_Ty,_Size> std::operator +(_Array_iterator<_Ty,_Size>::difference_type,std::_Array_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_iterator<_Ty,_Size>' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_Array_const_iterator<_Ty,_Size> std::operator +(_Array_const_iterator<_Ty,_Size>::difference_type,std::_Array_const_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_const_iterator<_Ty,_Size>' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2784: 'std::_Revranit<_RanIt,_Base> std::operator +(_Diff,const std::_Revranit<_RanIt,_Base> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'std::string'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14): error C2676: binary '+' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2374: 'phrase' : redefinition; multiple initialization
1> c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(14) : see declaration of 'phrase'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_String_iterator<_Mystr> std::operator +(_String_iterator<_Mystr>::difference_type,std::_String_iterator<_Mystr>)' : could not deduce template argument for 'std::_String_iterator<_Mystr>' from 'const char [1]'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(420) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_String_const_iterator<_Mystr> std::operator +(_String_const_iterator<_Mystr>::difference_type,std::_String_const_iterator<_Mystr>)' : could not deduce template argument for 'std::_String_const_iterator<_Mystr>' from 'const char [1]'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring(288) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::move_iterator<_RanIt> std::operator +(_Diff,const std::move_iterator<_RanIt> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const char [1]'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1947) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_Array_iterator<_Ty,_Size> std::operator +(_Array_iterator<_Ty,_Size>::difference_type,std::_Array_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_iterator<_Ty,_Size>' from 'const char [1]'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1801) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_Array_const_iterator<_Ty,_Size> std::operator +(_Array_const_iterator<_Ty,_Size>::difference_type,std::_Array_const_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_const_iterator<_Ty,_Size>' from 'const char [1]'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1662) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const char [1]'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1226) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2784: 'std::_Revranit<_RanIt,_Base> std::operator +(_Diff,const std::_Revranit<_RanIt,_Base> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const char [1]'
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1031) : see declaration of 'std::operator +'
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(15): error C2676: binary '+' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(17): error C2088: '<<' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(20): error C2088: '[' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(23): error C2088: '[' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(24): error C2088: '<<' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(28): error C2088: '[' : illegal for class
1>c:\users\victor\documents\visual studio 2012\projects\gametestexample\gametestexample\gameover.cpp(37): error C2088: '<<' : illegal for class
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So, My point is, What's exactly going on? Can't realize if this is problem such as "Old syntax" which I never seen something like that before or if the code is wrong (what I can't believe too, No one would do a fail like that in a book)
I'm using Microsoft Visual Studio Premium 20112 as development environment
Use #include <string> too.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
std::to_string - more than instance of overloaded function matches the argument list
#include <string>
int main()
{
double randDouble = 1245.432;
std::wstring stringDouble = std::to_wstring(randDouble);
}
When I compile this in Visual Studio 2010 I get this error
Error 1 error C2668: 'std::to_wstring' : ambiguous call to overloaded
function 6
1> error C2668:
'std::to_string' : ambiguous call to overloaded function
1> d:\program files (x86)\microsoft visual studio
10.0\vc\include\string(688): could be 'std::string std::to_string(long double)'
1> d:\program files (x86)\microsoft visual studio
10.0\vc\include\string(680): or 'std::string std::to_string(_ULonglong)'
1> d:\program files
(x86)\microsoft visual studio 10.0\vc\include\string(672): or
'std::string std::to_string(_Longlong)'
Can someone please explain to me why the compiler is confused and what am I doing wrong?
This was a bug in Visual C++ 2010. It has been fixed in Visual C++ 2012.
#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?