Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is my code:
bool State::operator==(const State& s) const
{
bool flag=true;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(s.GetboardEl(i,j)!=board[i][j]){
flag=false;
}
}
}
return flag;
}
getBoardEl is a function in State class which returns an element from board(int[][]). I get this ERROR even if i'm using const before the brackets({)
:
error: passing 'const State' as 'this' argument of 'int State::GetboardEl(int, int)' discards qualifiers [-fpermissive]|
It appears that the member function getBoardEl is a non-const member function.
You should definitely change that to a const member function since the name implies it is a get function not a set function.
That would resolve the compiler error.
Another way to resolve the error is to use the member variable directly, as you have for this.
if ( s.board[i][j] != this->board[i][j] ) {
Related
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 3 years ago.
Improve this question
Here is the code:
bool Vehicle::checkID(std::string id)
{
std::vector<int> digits;
for (char c : id)
{
if(std::isdigit(c))
{
digits.push_back(atoi(c));
}
else
{
digits.push_back(int(c));
}
}
I don't know why he throws this error for "digits.push_back(atoi(c))".
I'm a very beginner, I know this will be not that difficult for you.
You can't do:
atoi(c)
atoi() expects a char *. You probably want
digits.push_back(c - '0');
The function atoi() takes a single const char * type as a parameter.
You're calling it with a char parameter. The compiler doesn't know how to convert from one to the other.
atoi actually wants a string as input, you can't call it with a single character. A string always needs a zero character as a terminator.
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'm new to C++ and I don't understand why I'm getting a not declared error on this:
int main(){
string listOfColors[5] = {"red","blue","green","yellow","magenta"};
for(int i = 0;i < sizeof listofColors;i++){
cout << listofColors[i] << "\n";
}
return 0;
}
This is my first utilization of an array so far, so I may just not be declaring it correctly. I also had the array declaration before the main function beforehand.
You declared your variable as listOfColors (capital "O"), and then you use it as listofColors in your for loop. All you need to do is to capitalize the "O" when using your variable.
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 6 years ago.
Improve this question
I have the following piece of code, and find that I'm not able to explicitly convert the output of lambda function into bool. I'm verifying this on the online IDE http://ideone.com/, and I choose C++14.
#include <iostream>
using namespace std;
int main() {
int number = 10;
int bar = 6;
auto numberisLarger = [&]() -> bool {return number > bar;};
bool isLarger = numberisLarger;
return 0;
}
However, I'm getting compilation error as below.
error: cannot convert 'main()::<lambda()>' to 'bool' in initialization
bool isLarger = numberisLarger;
I did explicitly convert it to bool, why it isn't working?
Thanks!
You need to execute the lambda, like you would a regular function.
bool isLarger = numberisLarger();
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 7 years ago.
Improve this question
I have a user defined class, one of whose members is a char* type. When I try to initialize it in the constructor I get an error saying error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
However, when I changed strcpy to strcpy_s, it would still give the following error IntelliSense: no instance of overloaded function "strcpy_s" matches the argument list argument types are: (char *, char *)
Let's say Student is the class and char* name; is one of the data members.So, my constructor is like:
Student (char* s = NULL) {
if (s != NULL) {
name = new char[strlen(s) + 1];
//strcpy(name,s);
strcpy_s(name,s);
}
}
It's because strcpy_s requires an additional parameter to specify how many bytes to copy.
See here: http://www.cplusplus.com/forum/beginner/118771/
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 9 years ago.
Improve this question
In the following piece of code I get the error mentioned below. Please tell me
Why *p=t gives error here
void reverse (char *p)
{
int length=strlen (p);
int c=0, i=length/2;
char *Temp=p+length-1, t;
while (c<length)
{
t=*Temp;
*Temp=*p
*p=t;
//Gives error as illegal, right operand has type char*
//Why is the error in the above line?
c++;
Temp--;
}
}
There is a semi-colon missing:
t=*Temp;
*Temp=*p ; //--here
*p=t;