for (int v = 0; v <= WordChosen.length();v++)
{
if(Letter == WordChosen[v])
{
WordChosenDuplicate.replace(v,1,Letter);
}
}
I get this error
"Error 4 error C2664:
'std::basic_string<_Elem,_Traits,_Ax>
&std::basic_string<_Elem,_Traits,_Ax>::replace(__w64
unsigned int,__w64 unsigned int,const
std::basic_string<_Elem,_Traits,_Ax>
&)' : cannot convert parameter 3 from
'char' to 'const
std::basic_string<_Elem,_Traits,_Ax>
&' c:\documents and settings\main\my
documents\uni\2nd
year\tp2\hangman\hangman\hangman.cpp 147
"
I only got the error after putting this line in
WordChosenDuplicate.replace(v,1,Letter);
Or
WordChosenDuplicate.replace(v,1,std::string(Letter, 1));
The std::string::replace() function's parameters are incorrect or you need to invoke a different overload of replace. Something like:
WordChosenDuplicate.replace(v, // substring begining at index v
1, // of length 1
1, // replace by 1 copy of
Letter); // character Letter
What do you want to achieve? The version of replace that you are trying to call doesn't exist – as the compiler is telling you. Which of these versions do you mean?
It appears that WordChosenDuplicate is a std::string, in which case the 3rd parameter in the replace() method should be another std::string or a c-style const char*. You are trying to pass a single char instead ("Letter"). The error is saying that there is no version of replace() that takes a char as the 3rd parameter.
Related
I have an array of strings that I want to copy into a vector of strings only if a particular string's length is equal to a known value.
function(int len){
string lines[8]{
"a string",
"another string",
"and another"
etc..
}
vector<string> v (8);
std::copy_if(lines->begin(), lines->end(), std::back_inserter(v),
[len](std::string i) { return len == i.length(); });
The errors i get are:
error C2664: 'bool
Grid::SearchGrid::::operator
()(std::string) const': cannot convert argument 1 from 'char' to
'std::string'
error C2679: binary '=': no operator found which takes a right-hand
operand of type 'char' (or there is no acceptable conversion)
these are both happening within the algorithm header so I'm not sure where I'm going wrong. New to these new lambda expressions.
lines->begin() and lines->end() don't behavior as you expected. lines decays to string*, then lines->begin() will return the iterator on the 1st std::string of the array lines, and dereference on the iterator would get a char.
You could use std::begin and std::end instead.
std::copy_if(std::begin(lines), std::end(lines), std::back_inserter(v),
[len](std::string i) { return len == i.length(); });
BTW: vector<string> v (8); initializes v with 8 elements (empty std::strings); because you're using back_inserter later I think just vector<string> v; is enough. Otherwise you'll get 8+ elements in v at last.
Other issues: The function return type declaration seems lost; lambda parameter type could be changed to const std::string&.
float diff = 0;
const char* str[] = {"Err: ZPROBE: ",diff};
LCD_ALERTMESSAGEPGM(str);
With the code above I get I get this error. Anyone know how to create a single string from "Err: ZPROBE: " and a (float) diff?
exit status 1
cannot convert 'float' to 'const char*' in initializatio
Sorry should add that in the environment i'm using - 'string' : is not a member of 'std',
Ok now trying this
String str = String("Err: ZPROBE: " , diff);
but get this
call of overloaded 'String(const char [14], float&)' is ambiguous
To convert float or double into a string, you can use dtostrf() which is available in the stdlib.h header. Also see the reference of String Object Constructors to construct String objects correctly.
I'm trying to use a separate function to read a few data values in from a file; I'm getting two errors (I haven't used c++ in a long time...):
double * FREAD(std::string fpath){
std::string line;
double * params = new double[14];
std::ifstream parameters (fpath);
if (parameters.is_open())
{
for (int b = 0; b < 14; b++) {
parameters >> line >> params[b];
}
}
parameters.close();
return params;
}
throws
error C2556: 'double *FREAD(std::string)' : overloaded function differs only by return type from 'double FREAD(std::string)'
and
error C2040: 'FREAD' : 'double *(std::string)' differs in levels of indirection from 'double (std::string)'
The second issue is thrown from the line where I call the function in main.
double * param = FREAD(parampath);
error C2440: 'initializing' : cannot convert from 'double' to 'double *'
If I don't define param as a value pointed at by a double, I get the same type mismatch error in reverse...
My understanding is that I'm supposed to return a pointer which is directed at the first value of the array my subfunction has created and use that to work with the data. But I can't seem to pull this value when I call the function in main.
The simplest and fool-proof way to do it would be to return a container, for example
#include <array>
std::array<double,14> FREAD(const std::string& fpath)
{
std::string line;
std::array<double, 14> params;
// .... fill params
return params;
}
Concerning the overload error, you cannot overload a function by return type. It looks like the compiler is seeing these two declarations:
double * FREAD(std::string fpath);
double FREAD(std::string fpath);
Given the above suggestion, you should remove both of them anyway.
Your error C2556 is because you apparently have another FREAD() function that returns something else. The error is telling you that you can't overload functions based only on the return type.
Going from the messages, it appears to me that you have two functions:
double * FREAD(std::string fpath)
double FREAD(std::string fpath)
You only posted the first one.
C++ only allows you have two functions with the same name if they take different arguments (or const-ness for member functions). You should either give your functions different names, or pass in a token argument that the compiler can use to tell which one you want.
error C2664: 'CCertStoreHelper::DeleteCtl' : cannot convert parameter 1 from 'const std::basic_string<_Elem,_Traits,_Ax>' to 'std::wstring &error C2664: 'CCertStoreHelper::DeleteCtl' : cannot convert parameter 1 from 'const std::basic_string<_Elem,_Traits,_Ax>' to 'std::wstring &
with
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>,
_Ax=std::allocator<wchar_t>
]
Conversion loses qualifiers
I have no idea regarding this. So kindly provide the solution.
Code:
CCertStoreHelper certCaStore;
std::set<std::wstring> ctlIdentifiersToRemove; // It populates data which I m not mentioning
std::set<std::wstring>::iterator iter1;
std::set<std::wstring>::iterator iter2;
for(iter1 = ctlIdentifiersToRemove.begin(); iter1 != ctlIdentifiersToRemove.end(); iter1++)
{
iter2 = ctlIdentifiersReferenced.find((*iter1));
if(iter2 == ctlIdentifiersReferenced.end())
{
if(certCaStore.DeleteCtl((*iter1))) // error line
{
// ...
}
}
}
// prototype for DeleteCtl fun is
bool CCertStoreHelper::DeleteCtl(std::wstring &ctlIdentifier)
Kindly correct me what i am doing wrong
Thanks
As twalberg points out, the most important bit of the compiler error message is the "loses qualifiers" bit. It also tells you that it can't convert from const std::wstring to std::wstring&, except that it expanded the first std::wstring into its full template instantiation form.
The issue is that your DeleteCtl takes the argument by non-const reference, as if it wants to modify the string there (bad idea), but it can't do that, because you're iterating over a set, and you cannot change the members of a set once they're in there (std::set doesn't make a difference between const_iterator and iterator, essentially). The reason is that std::set bases its internal structure on the values of its elements, and if you change those values, the internal structure becomes invalid, and horrible things happen.
I have the below program written in C++:
#include <iostream>
using namespace std;
int main()
{
int age[5];
char name[5][10];
age[0]=10;
age[1]=20;
age[2]=30;
age[3]=25;
age[4]=40;
name[0]="abc";
name[1]="abc";
name[2]="abc";
name[3]="abc";
name[4]="abc";
cout<<name[0]<<" is "<<age[0]<<"years old";
cout<<"\n";
cout<<name[1]<<" is "<<age[1]<<"years old";
cout<<"\n";
cout<<name[2]<<" is "<<age[2]<<"years old";
cout<<"\n";
cout<<name[3]<<" is "<<age[3]<<"years old";
cout<<"\n";
cout<<name[4]<<" is "<<age[4]<<"years old";
cout<<"\n\n";
system("PAUSE");
}
When I compile and run it, I get these errors:
error C2440: '=' : cannot convert
from 'const char [3]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
I am running MSVC 2008 under Windows 7. I have tried many possible solutions but I failed in fixing this. Any help would be appreciated,
You are treating the name array as if it was defined thus:
char *name[5];
So either define it that way, or use the following code to populate it:
strcpy(name[0], "abc");
strcpy(name[1], "abc");
strcpy(name[2], "abc");
strcpy(name[3], "abc");
strcpy(name[4], "abc");
I prefer the former choice. The point being you are trying to assign a char * to a char [] which is what strcpy is for. Given you are manipulating initialized C strings in this case anyway, you might as well deal with char * throughout the code.
You should use std::string for this purpose. The use of char* and char[] to represent strings is deprecated in C++ for many good reasons.
Given the program snippet, name can be initialized at the declaration itself.
char name[5][10] = { "abc", "abc", "abc", "abc", "abc" } ;
// ^ index 5 is not necessary. char name[][10] = { .. } would also suffice.
Specified the length of each row is 10 but only using first 3 indexes of it. Every 3rd index ( i.e., 4th element in the array ) is automatically added with a '\0'.
Initialization can be done in case of age array too.
You can use also std::string name[10] instead of 2d char's array. In this case only you can assign new values to the strings through operator '='.
Otherwise you should to use array of char* and use strcpy() function for assignment.