"__comp cannot be used as a function" c++ while trying next_permutation - c++

I'm trying to do permutations with next_permutation from the stl, however I'm getting an error and I can't figure out how to fix it. I've tried googling, however the only results that come up are when people used the same function and function's variables name but thats not the case here.
Here's the error :
'__comp' cannot be used as a function
Here's the code :
struct rectangle{
int n;
int h;
int w;
};
bool check(const rectangle& rect1,const rectangle& rect2){
return rect1.n < rect2.n;
}
do{
//...
} while ( next_permutation(recs.begin(), recs.end(), check) ); // Getting error on this line.
Here's the full source code along with the sample input in case it's needed http://pastebin.com/eNRNCuTf

H = rec4.w + max(rec1.h, rec2.h, rec3.h);
You don't want to pass rec3.h there - The error message simply say that the 3rd argument to max can't be used as a function. I believe you intended:
H = rec4.w + max(max(rec1.h, rec2.h), rec3.h);

Related

How to directly add and call a brief unit test in Solution class of LeetCode

I was training myself to write brief unit tests while solving leetcode questions. However, I couldn't call the unit test I wrote in the notepad on Leetcode website directly.(I understand the most formal way to write unit tests is to create another test class in a test file. However, I just want to quickly warm up on this, so I put it in the same class as Solution.) I'm wondering whether I messed something wrong with the syntax of creating an object or it's due to some hidden structure in the Leetcode?
For example, for Leetcode question 2400, I tried to invoke my testNumberofWays
as:
class Solution {
public:
int numberOfWays(int startPos, int endPos, int k) {
int waysTable[2000][1001] = {{0}};
if((k - abs(endPos-startPos))%2 != 0){
return 0;
}
waysTable[1000][0]=1;
// omit some implementation for simplicity
return waysTable[abs(endPos-startPos+1000)][k];
}
void testNumberOfWays(){
Solution solu = Solution();
assert(solu.numberOfWays(1, 2, 3) == 3);
}
};
Solution *s = new Solution();
s->testNumberofWays();
However I got the error message as
Line 33: Char 1: error: unknown type name 's'
s->testNumberofWays();
^
And I've also tried to write a static function out of Solution class directly like this
class Solution {
public:
int numberOfWays(int startPos, int endPos, int k) {
int waysTable[2000][1001] = {{0}};
//omit some implementation for simplicity
return waysTable[abs(endPos-startPos+1000)][k];
}
};
static void testNumberOfWays(){
Solution solu = Solution();
assert(solu.numberOfWays(1, 2, 3) == 3);
return;
}
testNumberOfWays();
But I got error message as this:
Line 39: Char 1: error: C++ requires a type specifier for all declarations
testNumberOfWays();
^
1 error generated.
Please let me know if the ways above may be achievable on Leetcode. Any help on how to write and run some quick unit tests on the notepad of Leetcode would be super helpful!
I'm not sure what you mean by on the notepad of Leetcode.
if you want to test Leetcode in a local environment, you may want try this.
it can translate test cases into corresponding data structures, and you only need to execute Solution().func() in the main function. .

How to customize function parameter errors(c++)

I wrote a function that requires two parameters, but I don't want those two parameters to be 0.
I want to make the compiler know that those two parameters cannot be 0 through some ways, otherwise the editor will report an error in the form of "red wavy line".
I refer to "custom exception class" to solve this problem, but I find this method does not work.
If there are someone knows how to do , I will be very happy, because it takes me a whole day
For example:
#include<iostream>
using namespace std;
int Fuction(int i , int j){
//code
}
int main(){
Funciton(1,1);
Funciton(0,0);
//I don't want i or j is zero
//But if they are still zero , The program will still work normally
return 0;
}
There is no integer type without a 0. However, you can provoke a compiler error by introducing a conversion to a pointer type. Its a bit hacky, but achieves what you want (I think) for a literal 0:
#include <iostream>
struct from_int {
int value;
from_int(int value) : value(value) {}
};
struct non_zero {
int value;
non_zero(int*) = delete;
non_zero(from_int f) : value(f.value) {}
};
void bar(non_zero n) {
int i = n.value; // cannot be 0
}
int main() {
bar(non_zero(42));
//bar(non_zero(0)); // compiler error
}
bar is the function that cannot be called with a 0 parameter. 0 can be converted to a pointer but that constructor has no definition. Any other int will pick the other constructor. Though it requires the caller to explicitly construct a non_zero because only one user defined conversion is taken into account.
Note that this only works for a literal 0. There is no error when you pass a 0 to this function:
void moo(int x){
bar(non_zero(x));
}
Thats why it should be considered as a hack. Though, in general it is not possible to trigger a compiler error based on the value of x which is only known at runtime.
If you want to throw an exception, thats a whole different story. You'd simply add a check in the function:
if (i == 0) throw my_custom_exception{"some error message"};
If you are using only MSVC you can also take a look at Structured Annotation Language (SAL). It is described on MSDN.
For your case you might be interested in _In_range_(lb,ub). An example would be:
void f(_In_range_(1,300) int a, _In_range_(1, 2147483647) int b);
Please note that this will not prohibit calling f(0, 0) but code analysis will trigger a warning. That warning will be triggered also in cases where you call f(x,x) and the compiler knows that x is zero.
In the past I liked to use SAL as it makes the interface clearer and can help reveal errors because the compiler can check more semantics. But now with modern C++ und the CppCoreGuidelines I am trying to follow the guidelines and so normally I don't need SAL anymore.

How can i use bool to create a file

Hi can someone help me with this function:
bool createfile (string path);
It is supposed to create a file but my problem is:
What exactly the true or false have to do with creating a file?! How can I use it?
The bool is the return type of the function createfile(). Without the definition it is impossible to tell for sure what exactly this value is supposed to be, but often it is used to return if the function was successful in doing what it is supposed to do, in this case, create a file.
What exactly the true or false have to do with creating a file?!
You might want to return true if the file was successfully created or false otherwise.
How can I use it?
This depends on the body of the function and the purpose that you want to use the function for.
Quick answer
To directly answer the "How can I use it" part of your question:
You call it this way:
string path = "/path/to/my/file.txt";
bool returnedValue = createfile(path);
As for "What exactly the true or false have to do with creating a file?!", like mentionned in the other answers, it might indicate the success or failure of the operation, but you might want to double-check that, because the actual value will depend on the implementation of bool createfile(string path)...
Comprehensive answer
It seems you need some help interpreting the syntax of bool createfile(string path);
What we need to clarify here is that in c++ (and many other languages), the first word used in the function declaration is the return type.
You could compare this to some arbitrary mathematical function of the following form: here
x = a + b
In this case, x is the result of the addition function.
Assuming all the elements above are numbers, we could translate this in c++, like so:
int a = 0;
int b = 5;
int x = a + b;
We could extract the example above in a function (to reuse the addition), like so:
int add(int a, int b)
{
return a + b;
}
and use it in the following way (with a main to put some execution context around it):
int main()
{
int x = add(0,5);
return 0;
}
Here are some other examples of functions:
// simple non-member function returning int
int f1()
{
return 42;
}
// function that returns a boolean
bool f2(std::string str)
{
return std::stoi(str) > 0;
}
You'll find more details here. It might seem like a lot to take in (the page is dense with information), but it is a true reference.

BLE gattServer.write() overloaded function

I'm having an issue trying to update the value of a characteristic within a custom BLE service that's running on an MCU running mbedOS v5.8.6. I am attempting to update the value of this characteristic with the value from a sensor. Please see the function below:
void onDataReadCallback(const GattReadCallbackParams *eventDataP) {
if (eventDataP->handle == dhtServicePtr->dataStream.getValueHandle()) {
const uint8_t data = sensorData;
BLE::Instance().gattServer().write(eventDataP->handle, &data, sizeof(data), false);
}
}
I have tried explicitly stating the correct variable type (according to the BLE gattServer reference docs) to no avail.
The exact error I receive is:
Error: No instance of overloaded function "GattServer::write" matches the argument list in "main.cpp", Line: 135, Col: 39
I believe I am doing this correctly according to the afforementioned documentation. So, my question is, where exactly am I going wrong? It's entirely possible that I've just made a stupid mistake!
Thanks,
Adam
You are trying send pointer to constant, Although the function signature requires normal pointer. In the below example when value const then compiler will through error.
#include <iostream>
void test(int *ptr)
{
printf("%d",*ptr);
}
int main ()
{
//const int a = 10; //Gives error
int a = 10; //This works fine.
test(&a);
return 0;
}

How do I use a Merit function in Gecode?

I am trying to use a merit function for my branching in Gecode. In the MPG, the Gecode Manual, an example merit function is stated, and how to pass it to the branching. But I cannot figure out where to put the function. Should it be inside the script or outside? Right now I have put it next to the copy function, etc. I cannot find any example code where someone uses a merit function.
I get the following error:
program.cpp(247): error C2059: syntax error: '}'
program.cpp(247): error C2853: 'm': a non-static data member cannot have a type that contains 'auto'
program.cpp(259): fatal error C1004: unexpected end-of-file found
This is the code I am trying out:
// ...
branch(*this, workers, BOOL_VAR_MERIT_MIN(m), BOOL_VAL_MAX());
}
auto m = [](const Space& home, BoolVar x, int i) {
return i;
}
// ...
I know it is stupid to make a merit function that just returns the index, I am just trying to make the simplest merit function to work before I do what I want to do.
According to the Gecode documentation the merit function should return a double. As suggested by the type definition of BoolBranchMerit:
typedef std::function<double(const Space& home, BoolVar x, int i)> Gecode::BoolBranchMerit
To be safe, you might also want to declare m as being an Gecode::BoolBranchMerit. So I think the following should fix your example:
// ...
branch(*this, workers, BOOL_VAR_MERIT_MIN(m), BOOL_VAL_MAX());
}
BoolBranchMerit m = [](const Space& home, BoolVar x, int i) -> double {
return (double) i;
}
// ...