[C++] Help I'm retarded

Iamtehwalrus

Veteran XV
If you're bored, feel free to help me :D
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
Line 58: is
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
line 59 is:
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?

:sunny:
 
Last edited:
go to line 58 and check to see that you formatted what ever expression is there properly. Since you cut and paste i'm pretty sure the line 58 i count to would not be line 58 of your code.

EDIT: nm that 58 doesn't appear to be a line indicator... get a better compiler that doesn't leave criptic messages for starters. But as far as i can tell you have formatted an argument improperly.
 
go to line 58 and check to see that you formatted what ever expression is there properly. Since you cut and paste i'm pretty sure the line 58 i count to would not be line 58 of your code.

yeah sorry that was retarded
line 58:
Code:
std::priority_queue< Edge<T> > pq(edges.begin(), edges.end(), std::greater< Edge<T> >);
 
what is the pq() asking for as arguments? it looks like you are sending it (int, int, what ever greater<> returns) try determining the greater number outside the pq argument and stick the result in there.

I'm not all that good at this but thats where i would start
 
what is the pq() asking for as arguments? it looks like you are sending it (int, int, what ever greater<> returns) try determining the greater number outside the pq argument and stick the result in there.

I'm not all that good at this but thats where i would start

i might be wrong but I thought i was creating a priority queue named pq... so pq isn't a function
I was basing it off this constructor
Code:
priority_queue ( InputIterator first, InputIterator last, const Compare& x = Compare(), const Container& y = Container() );
from priority_queue::priority_queue - C++ Reference

I guess I don't really know what the priority_queue constructor is. A lot of things are different on google, and it confuses me. It seems the commented pq constructor i have might be the "proper" way but i don't really know
 

priority_queue(const value_type* first, const value_type* last, const Compare& comp)
yeah this is what I was trying to use. i'm really bad at syntax and i forget basic things, so it wouldn't surprise me if i'm not using it correctly

EDIT:
I guess I don't know waht to put the for comparison argument. For some reason I got the idea I could just put greater<> or less<> and it would work fine as long as I overloaded the opeartor> for my class
 
Last edited:
Back
Top