custom comparetor in stl - stl

This has been driving me nuts for 3 hours. Anybody see a reason why this isn't working?
struct sortByPropRev
{
bool operator()(const cust_type &a, const cust_type &b) const
{
return a.prop > b.prop;
}
};
...
priority_queue<cust_type, vector<cust_type>, sortByPropRev> x;
I get compile errors:
Error C2664: 'bool (cust_type &,cust_type &)' : cannot convert parameter 1 from 'const cust_type' to 'cust_type &'
and 2 more just like it but on different lines of algorithm.h

You gave it b.pprop, vs a.prop. I think given the error that the compiler failed to parse the struct's definition properly- check for syntax errors in the code just above it.

Never mind. I found the problem. I t was in a different part of the code that was calling the same algorithm functions. Sorry to bother everybody and thanks for trying to help.

Related

Why does vector.push_back(System::Byte) not compile any more in VC++ 14.29 (C++/CLI)

I have the following code that used to compile and work fine:
std::vector<unsigned char> marshal_as(cli::array<System::Byte>^ const& from)
{
std::vector<unsigned char> result;
result.reserve(from->Length);
for (int i = 0; i < from->Length; i++)
{
result.push_back(from[i]);
}
return result;
}
After updating VisualStudio to version 16.10 - which updates the C++ compiler to version 14.29 - the code produces an error:
error C2664: 'void std::vector<unsigned
char,std::allocator<_Ty>>::push_back(const _Ty &)': cannot convert
argument 1 from 'unsigned char' to 'const _Ty &'
with
[
_Ty=unsigned char
]
message : An object from the gc heap (element of a managed array) cannot be converted to a native reference
message : see
declaration of 'std::vector<unsigned
char,std::allocator<_Ty>>::push_back'
with
[
_Ty=unsigned char
]
Changing the code in the loop body to
unsigned char b = from[i];
result.push_back(b);
fixes the problem.
I would like to understand the cause of this error. Is this somehow related to a change due to the C++ 20 standard?
Is this somehow related to a change due to the C++ 20 standard?
No. While std::vector<>::push() has subtly changed in C++20, it's not a change that materially affects what's going on here, the issue is definitely clr-specific.
I would like to understand the cause of this error.
This is almost certainly (see below) an error that was always present in your code, but was not being reported by previous versions of the C++/CLI compiler.
Consider the following function:
void foo(const int& v) {
int* ptr = &v;
// store ptr somewhere, long-term.
}
It's obvious that invoking foo() with a reference to a gc-backed int would be a recipe for disaster. Yet that's exactly what result.push_back(from[i]); does.
Your code "works" because push_back() happens to do nothing with its parameter that causes an issue. However, the compiler is not supposed to know that.
N.B. I say almost certainly because I'm having a heck of a time tracking down the call signature for cli::array<T>::operator[](std::size_t) const. It's not impossible that it used to return a T and now returns const T%.

What is a return type function

I am trying to understand the return type function but I really can't. For example in this code:
int add(int a, int b){
result=a-(-b);
return result;
}
I cant understand why it became return result and what is it for and what it do.I am new in c++ and Iwanted to become better in c++.
This is meant to give back an answer (value) to the other part of your program, which called this function.
Let's say you do something like:
print(add(2,4));
Your program would print the value: 6
Disclaimer: not really sure if the syntax is correct, it's been over a decade since I last programmed in C++;

QTCreator function placement - error: reference to non-static member function must be called-

I am new on the QT world. And I am confused to where to put my small bool function:
bool Widget::compareBy(const dist_obj &a, const dist_obj &b)
{
return a.distance < b.distance;
}
and I want to call it from `void Widget::update_window()
like:
`
std::sort(found_obj.begin() , found_obj.end() , compareBy);
it gave:
error: reference to non-static member function must be called
I am using this function in my normal c++11 Clion environment. What am I doing wroin in this QT world :O
thanks
I found an answer which is worked for me from sorting a vector of classes based on a variable in the class
worked like a charm without defining any function or class.

PyBind - Overloaded functions

Firstly, my thanks to all of you for trying to resolve this doubt of mine. I am working on converting a minimal C++ project to be used in Python. The real reason behind this effort is for speed.
I came across PyBind and was quite surprised at its capabilities and also the amount of documentation they have provided. Right now there is something stopping the work because I have no idea on how to do it. Consider the below code in the file "MySource.hpp" and can you kindly tell me how a binding can be done?
struct Point3D
{
public:
double x, y, z;
CPoint3D();
CPoint3D(double x, double y, double z);
inline double Len() const;
inline void Normalize();
};
Point3D VectorCross(const Point3D& pt1, const Point3D& pt2, const Point3D& pt3);
void VectorCross(const float* u, const float* v, float * n);
I was able to define a binding for Point3D as a class and certain of its member functions. But I don't have a clue about how to do the binding for the overloaded method "VectorCross". It has two methods with one accepting instances of Point3D, and another one accepting pointers to float arrays.
The binding I wrote so far is shown below
PYBIND11_MODULE(mymodule, m)
{
py::class_<Point3D> point3d(m, "Point3D");
point3d.def_readwrite("x", &CPoint3D::x);
point3d.def_readwrite("y", &CPoint3D::y);
point3d.def_readwrite("z", &CPoint3D::z);
point3d.def(py::init<>());
point3d.def(py::init<double , double , double >());
point3d.def("Len", &CPoint3D::Len);
point3d.def("Normalize", &CPoint3D::Normalize);
}
Can someone please guide me on how to do this?
It seems that you need to do overload cast as described here.
m.def("VectorCross", py::overload_cast<const Point3D&, const Point3D&, const Point3D&>(&VectorCross));
m.def("VectorCross", py::overload_cast<const float*, const float*, float*>(&VectorCross));
Roman,
I figured this out but still selecting your answer as the right one for it really is the answer. But still in the case of method signature where it expects the arguments to be float pointers (below line)
m.def("VectorCross", py::overload_cast<const float*, const float*, float*>(&VectorCross));
compiles fine in creating a python library. But when you are try calling the method from python after importing the same will result in an argument error.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: AngleBetween(): incompatible function arguments. The following argument types are supported:
1. (pt1: chenhancc.CPoint3D, pt2: chenhancc.CPoint3D) -> float
2. (pt1: chenhancc.CPoint3D, pt2: chenhancc.CPoint3D, pt3: chenhancc.CPoint3D) -> float
3. (u: float, v: float) -> float
Looks like the python looks at it as if they are plain float number arguments.
But still my sincere thanks and appreciate your time.
Regards,
0K

Generic function in Vala

I wrote a maximum() generic function in Vala.
However, it does not compile.
Here it is:
T maximum<T>(T a, T b) {
return a > b ? a : b;
}
void main() {
stdout.printf("%d\n", maximum(10, 2));
}
I got the following error:
generics.vala:2.12-2.16: error: Relational operation not supported for types `T' and `T'
Do you know how I can fix this function to be able to compile it?
Thanks.
Generic direct comparison and various other operations aren't supported by current Vala. You may want to use and implement Gee.Comparable interface to use a compare_to() method instead.