Sign in to follow this  
paul23

error message (with VC++ 2008 express)

Recommended Posts

So working on my "heap" container I made this:
template <class T, class _ax = std::allocator<T> >
class heaped_vector : public std::vector<T, _ax > {
public:
    
    template <class _fn1>
    void push_heap2(T elem, _fn1 func) {
        push_back(func(elem));
        std::push_heap(begin(),end());
    }



};
This should perform the function "func" on elem, and then put it at the correct position in the heap! - However if I call it by doing: t.push_heap2(10,::rand_range<int>); with ::rand_range<int> being a function that returns a random value between "0 and the first element".. it gives some "wierd" error messages:
Quote:
1>X:\heaptest.cpp(20) : error C2914: 'heaped_vector<T>::push_heap2' : cannot deduce template argument as function argument is ambiguous 1> with 1> [ 1> T=int 1> ]
Can anyone explain this: and possibly explain how to write such a function (that takes another function as argument, just like "std::for_each"). Thanks in advance, paul EDIT: apparantly if I remove the template part of "rand_range" and make it static type it works.. However how could I keep the template part?

Share this post


Link to post
Share on other sites
At first glance I thought there maybe a ".template<X>" missing yet that is not required here. MSCV is normally more lax than gcc so I ran the following code but it compiles with no problems and also with Comeau, could you run the same code with MSCV and paste the output.


#include <vector>
#include <algorithm>

template <class T, class _ax = std::allocator<T> >
class heaped_vector : public std::vector<T, _ax > {
public:

template <class _fn1>
void push_heap2(T elem, _fn1 func) {
this->push_back(func(elem));
std::push_heap(this->begin(),this->end());
}
};


template<typename T>
T func(T t)
{
return t;
}

int main()
{
heaped_vector<int> h;
int i (1);
h.push_heap2(i,&func<int>);
}



If it compiles without problems can you post a minimal, testable piece of code with shows the C2914 error, also are you running vs9 sp1?

Also I do not think beginners is the correct forum for this.

Share this post


Link to post
Share on other sites
hmm that works, weird..


But I "found" the problem:
template <class T>
rand_range(T rmin,T rmax);
template <class T>
rand_range(T rmax);

2 functions with the same shortname apparantly cause this error?


ps: just posted this here since I haven't really looked futher than the beginners forum, don't really count myself more than that yet in C++ ><.
pps: how you create those fancy-looking codeboxes?

Share this post


Link to post
Share on other sites
You can use [source][/source] for the big, syntax highlighted boxes, and [code][/code] for a couple of lines.

This probably doesn't belong in for beginners.

Finally, it is bad style to derive from the standard containers. They lack virtual destructors for one thing, which can cause issues. You can use composition instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Finally, it is bad style to derive from the standard containers. They lack virtual destructors for one thing, which can cause issues. You can use composition instead.

It maybe bad style yet it is perfectly fine unless you are going to delete via the base class.

Share this post


Link to post
Share on other sites
It is bad style here. I can call someHeap.push_back(element) and break the invariant that the data is a heap. Or std::sort(someHeap.begin(), someHeap.end()). A heap is most certainly not a vector, even if a vector might be used in the implementation, so inheritance doesn't make any sense here.

Writing a wrapper class is the safest route, both in academic style and in practise.

In C++, we might use private inheritance with using to explicitly list which base functions are available from the derived class, but private inheritence is a corner case of C++ which some programmers might be less than familiar with. I would probably avoid it in favour of writing the small wrapper class. But in other languages this isn't an option.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this