Line 21: Char 40: error: reference to non-static member function must be called 21 | sort(words.begin(),words.end(),lencom); | ^~~~~~1 error generated.
Question
Line 21: Char 40: error: reference to non-static member function must be called 21 | sort(words.begin(),words.end(),lencom); | ^~~~~~1 error generated.
Solution
The error message you're seeing is because you're trying to use a non-static member function as a comparison function for std::sort. The comparison function should be either a standalone function or a static member function.
Here's how you can fix it:
- If
lencomis a method of a class, you should make it a static method. Here's an example:
class MyClass {
public:
static bool lencom(const string &a, const string &b) {
return a.length() < b.length();
}
};
Then you can call it like this:
sort(words.begin(), words.end(), MyClass::lencom);
- If
lencomdoesn't need to access any member variables, you can make it a standalone function:
bool lencom(const string &a, const string &b) {
return a.length() < b.length();
}
Then you can call it like this:
sort(words.begin(), words.end(), lencom);
- If
lencomneeds to access member variables, you can use a lambda function to capture the necessary variables:
auto lencom = [this](const string &a, const string &b) {
return a.length() < b.length();
};
sort(words.begin(), words.end(), lencom);
Remember to include the necessary headers and use the correct namespaces as needed.
Similar Questions
codes/mainc-5557-1710246799.872938.cpp: In function 'int main()':codes/mainc-5557-1710246799.872938.cpp:42:5: error: 'qsort' was not declared in this scope qsort(students, n, sizeof(struct Student), compare_students); ^~~~~
Line 20: Char 32: error: called object type 'int' is not a function or function pointer 20 | max_pattern=max(max_pattern,lcs);
Static MemberAs told earlier, the static members are only declared in the class declaration. If we try to access the static data member without an explicit definition, the compiler will give an error.
Which header file is required to use the sort() and stable_sort() functions in C++?Options: Pick one correct answer from below<iostream><string><vector><algorithm>
Point out the error line in the following program.1. #include<stdio.h>2. #include<string.h>3. int main()4. {5. static char str1[] = "Welcome";6. static char str2[20];7. static char str3[] = "Sir";8. str2 = strcpy(str3, str1);9. printf("%s\n", str2);10. return 0;11. }Select one:Line number 8Line number 6None of these.Line number 7
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.