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
Like the following way:
set<int> s[3];
I have tried it but it gives error in the line where I had tried to access its elements by writing s[i][j] where the error says
no match for 'operator[ ]'
The problem is not the array of std::set-s but rather the way you try to access elements inside your set.
std::set doesn't support operator [], that is why you are getting the error:
no match for 'operator[ ]'
Instead, access object using find() in the following way:
auto iter = s[i].find(<value>);
if (iter != s[i].end()) {
[..] // Do something with iter
}
Elements of a set are not accessed by index. s[i] is the (i-1)'th set but s[i][j] doesn't mean anything. You can check whether an element is present in the set using the find function. For eg s[i].find(3)!=s[i].end() . You can loop through the elements in sorted order using for(int x : s[i]){} (C++11 and above) or using iterators.
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 1 year ago.
Improve this question
string s="1 23";
string a;
a[0]=s[2];a[1]=s[3];
cout<<a;
Here, I can't get output string a. But I can get all the individual elements by a[0].
Because a is empty, a[i] accesses it out of bounds for every possible i, causing undefined behavior.
Use a.push_back(s[i]) to add characters to a.
a is initialized as an empty string, so no memory is allocated for its characters, so when accessing it with [], you access unallocated memory, and that's undefined behavior.
One way to solve it is to create a as string with enough characters allocated. You can use the std::string fill constructor, that fills the string with a char of your choice:
std::string s = "1 23";
std::string a(s.size(), ' ');
This way you can put the characters in any index that exists in s.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
I have this class namescores_t which is basically a data structure containing a first/last name linked to a score. While filling a vector of namescores_t objects, I check to see if the object already exists like so:
// reading from file and inserting data...
vector<namescores_t> NS;
namescores_t ns(names, scores); // initializing namescores_t object with names & scores read from file
if(find(NS.begin(), NS.end(), ns) == NS.end()); // if object does not exist in the list, add it
NS.push_back(ns);
However, I am getting an error no match for 'operator==' (operand types are 'namescores_t' and 'const namescores_t'). when calling find(NS.begin(), NS.end(), ns) == NS.end()
What am I doing wrong?
I was under the assumption that std::find() was able to use operator< to determine equivalency, similar to std::map, but this is apparently not the case. So I simply overloaded the operator== in order to fix the issue.
bool namescores_t::operator==(const namescores_t &ns) const
{
// returns whether both the names and scores of the object are equal to one another
return (scores.get_mean() == ns.scores.get_mean() && name == ns.name);
}
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 am trying to create a program in Visual Studio Community 2015 to play Tic-Tac-Toe. Sounds simple, not so much.
Anyway, I am using a function [bool playerCheck()] to make a switch for the function that checks whether the space that the user chooses is already taken.
void checkInput()
{
if (playerCheck == true)
Visual Studio gives the squiggly lines under the "==" and gives the message
operand types are incompatible ("bool (*)()" and "bool").
Not sure how the syntax should be for comparing function output to a constant.
playerCheck is a function which needs to be called in order to get the boolean result, i.e.:
if (playerCheck() == true)
bool (*)() is a type for a function which takes no arguments and returns a boolean.
playerCheck == true is an attempt to compare the function pointer with a boolean value, which leads to the compilation error which speaks for itself.
playerCheck() == true is calling the function and comparing the result (of type boolean) with the boolean value.
Note that if (x == true) can be generally abbreviated to if (x), so you could just write
if (playerCheck())
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
The error message appears on Xcode that says "invalid operands to binary expression.
in my code I'm using an array of a struct, i'm trying to sort input data in an ascending order, and i'm getting this error message at the "if" condition shown in the print screen at this link:
https://www.dropbox.com/s/0mch2gbxcif0a20/Screen%20Shot%202016-04-27%20at%2012.45.45%20PM.png?dl=0
The Code
if (studentsInfo[i] > studentsInfo[i + 1]) {}
The Error
Invalid operands to binary expression ('students' and 'students')
What do you compare in your program? As I see, you have to compare names, but all you do is compare an array element which is a struct data type.
If you are trying to compare names, you have to use dot "." operator to reach names. After yo compare names, you can change the elements's place.
The error means that > only takes two arguments and you are using it for something else. In this case you are comparing an entire data structure that does not have an override for > operator and is an undefined behavior. StudentsInfo[i] is a data structure that has more than one element in it. Replace the StudentsInfo[i] with StudentsInfo[i].GPA or another element whose data type has a defined > operator.
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
My code:
{
int reply;
cout<<"Am I doing something wrong: ";
cin>> reply;
if-part( reply == "yes") {
cout<<"Good";
}
}
Hi guys, I am newbie, I googled and youtubed the stuff, but i just can not find an answer.
Why is this code not running well if you text in if condition, but if you put number, everything is fine?
Thank you.
In your case you are comparing an integer with a pointer (address)
if-part( reply == "yes") {
Reply is a in value.
"yes" is a c-string, so it is roughly equivalent to
const char *yes = "yes"
where yes would be a pointer to the first byte in the string in y_e_s -- for example, 0x75243
so you are comparing an integer value to a pointer value such as 0x75243 and they are unlikely to ever be equal.
In java script this would work (better) since it converts types for you -- C++ does not, at least not like this.
reply is of type int and you are comparing it as a string.That's not possible.
You can simply use:
if (reply == 1)
cout << "Good\n";