Iamtehwalrus
Veteran XV
If you're bored, feel free to help me
i suck at coding, and i'm prolly gonna give it up (cuz y'know... i suck)
anyway, I'm trying to make a priority queue (with the min at the node) for this class I made, and I don't know how exactly to implement the comparing part. What I have already is probably retarded and hence doesn't compile
I've included the important parts--tell me if i should include more (and this is all in a header file if u couldn't tell). Pink code are the key parts
Errors if compiled as is:
Line 58: is
errors if I use the commented PQ constructor instead:
line 59 is:
I'm not sure exactly how to make a PQ, so I just tried all the things I saw on google. Ideally what I want is a min-heap priority queue (with min at the root) with a list of Edge<T>'s that I have (edges is the variable).
Any ideas?
i suck at coding, and i'm prolly gonna give it up (cuz y'know... i suck)
anyway, I'm trying to make a priority queue (with the min at the node) for this class I made, and I don't know how exactly to implement the comparing part. What I have already is probably retarded and hence doesn't compile
I've included the important parts--tell me if i should include more (and this is all in a header file if u couldn't tell). Pink code are the key parts
Code:
#include <vector>
#include <list>
#include <queue>
#include <iterator>
template <typename T>
struct Edge
{
Edge(T vertex1, T vertex2, double cost) {
v1 = vertex1;
v2 = vertex2;
distance = cost;
}
T v1;
T v2;
double distance;
[color=pink] bool operator> (const Edge<T>& param) {
return (distance>param.distance);
}[/color]
};
template <typename T>
class Graph
{
public:
//...
int partition(int);
//...
private:
std::vector<T> vertices;
std::list< Edge<T> > edges;
};
//...
template <typename T>
int Graph<T>::partition(int clusterMax) {
[color=pink] std::priority_queue< Edge<T> > pq(edges.begin(), edges.end(), std::greater< Edge<T> >);
// std::priority_queue< Edge<T>, std::vector< Edge<T> >, std::greater< Edge<T> > > pq;[/color]
return 0;
}
Errors if compiled as is:
Code:
58 C:\CS311\PA4\graph.hpp expected primary-expression before ')' token
Code:
std::priority_queue< Edge<T> > pq(edges.begin(), edges.end(), std::greater< Edge<T> >);
errors if I use the commented PQ constructor instead:
Code:
C:\Dev-Cpp\include\c++\3.4.2\bits\stl_function.h In member function `bool std::greater<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Edge<Sample>]':
279 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_heap.h instantiated from `void std::__adjust_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Edge<Sample>*, std::vector<Edge<Sample>, std::allocator<Edge<Sample> > > >, _Distance = int, _Tp = Edge<Sample>, _Compare = std::greater<Edge<Sample> >]'
404 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_heap.h instantiated from `void std::make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Edge<Sample>*, std::vector<Edge<Sample>, std::allocator<Edge<Sample> > > >, _Compare = std::greater<Edge<Sample> >]'
369 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_queue.h instantiated from `std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, const _Sequence&) [with _Tp = Edge<Sample>, _Sequence = std::vector<Edge<Sample>, std::allocator<Edge<Sample> > >, _Compare = std::greater<Edge<Sample> >]'
59 C:\CS311\PA4\graph.hpp instantiated from `int Graph<T>::partition(int) [with T = Sample]'
90 C:\CS311\PA4\test_mst.cpp instantiated from here
218 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_function.h passing `const Edge<Sample>' as `this' argument of `bool Edge<T>::operator>(const Edge<T>&) [with T = Sample]' discards qualifiers
C:\CS311\PA4\Makefile.win [Build Error] [test_mst.o] Error 1
Code:
std::priority_queue< Edge<T>, std::vector< Edge<T> >, std::greater< Edge<T> > > pq;
I'm not sure exactly how to make a PQ, so I just tried all the things I saw on google. Ideally what I want is a min-heap priority queue (with min at the root) with a list of Edge<T>'s that I have (edges is the variable).
Any ideas?
Last edited: