0

I have a situation where I am not allowed to modify the header file for my class. I want to add just a helper function to use with one of my functions.. but cant quite figure out the correct way to implement it. Normally I try google but couldn't find any help on there.

Here is my current code:

    template<typename T>
void Set<T>::doubleRotateRight(Elem *& node) {
    // you fill in here
    rotateRight(node->left);
    rotateLeft(node);

    //call private helper not defined in header
    privmed();

}
void privmed(){
    //out << "who" << endl;
}

However, when I run this I get the error:

error: there are no arguments to ‘privmed’ that depend on a template parameter, so a declaration of ‘privmed’ must be available [-fpermissive]
     privmed();

Any help with this would be incredible!

Joe Caraccio
  • 1,899
  • 3
  • 24
  • 41
  • In C++ everything must be declared before it is used. Have you tried moving the `privmed` function implementation to before the `doubleRotateRight` function? – Some programmer dude Nov 06 '16 at 12:29
  • Voted to close as **unclear**. Needs info about what can be modified and what cannot be modified; needs info about what `privmed` is meant to be doing; needs info about the goal(s). – Cheers and hth. - Alf Nov 06 '16 at 12:46
  • cmon.... thanks a lot – Joe Caraccio Nov 06 '16 at 12:49
  • *"I am not allowed to modify the header file".* The code you are showing is in that header, is it not? – Captain Giraffe Nov 06 '16 at 12:56
  • @CaptainGiraffe that code is in the .cpp file.. I could be wrong but pretty sure that isn't that header – Joe Caraccio Nov 06 '16 at 13:00
  • @JoeCaraccio You can't have `template void Set::doubleRotateRight(Elem *& node)` in the cpp file. See http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Captain Giraffe Nov 06 '16 at 13:01
  • I'm voting to close this question as off-topic because the Question is unanswerable under the conditions posted. It is based on flawed premises. – Captain Giraffe Nov 06 '16 at 13:04
  • @CaptainGiraffe you are completely wrong.. You can.. I do right now.. and without the private function it runs fine.. please.. if you don't want to help me that is fine.. but don't come and say wrong withings – Joe Caraccio Nov 06 '16 at 13:05
  • Did you try to find any info about this error e.g. here - http://stackoverflow.com/questions/9941987/there-are-no-arguments-that-depend-on-a-template-parameter – segevara Nov 06 '16 at 15:34

2 Answers2

1

You can just use a lambda:

template<typename T>
void Set<T>::doubleRotateRight(Elem *& node)
{
    static auto const privmed = []() -> void 
    {
        //out << "who" << endl;
    };

    // you fill in here
    rotateRight(node->left);
    rotateLeft(node);

    //call private helper not defined in header
    privmed();
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Well, then declare a local class and use a `static` member function in that class. – Cheers and hth. - Alf Nov 06 '16 at 12:39
  • Or, depending on what you mean, you might make do with the lambda calling the `doubleRotateRight` function or some other function that in turn calls it. It's hard to sketch solutions without a very concrete question. But anything recursive is doable: how to do it just depends on what it is. – Cheers and hth. - Alf Nov 06 '16 at 12:42
  • when i run the code you posted it just says "‘privmed’ does not name a type" – Joe Caraccio Nov 06 '16 at 12:44
  • well my goal was to be able to run that helper function and have it call itself reccursively – Joe Caraccio Nov 06 '16 at 12:45
  • I don't think that's the code I posted you got that error from. You need to be way more clear about things (e.g., what's your header file, what's your goal, exactly how recursive) in order to get good answers other than by chance.' – Cheers and hth. - Alf Nov 06 '16 at 12:46
  • What the heck man? its clear enough... I need to figure out how to call the method without defining it in the header file.. thats my question.. if you could unclose my question that would be great.. instead of making things more difficult for me.. – Joe Caraccio Nov 06 '16 at 12:48
1

I think the problem is that you have to define the "privmed()" function before you use it, like this:

void privmed(){
    //out << "who" << endl;
}

template<typename T>
void Set<T>::doubleRotateRight(Elem *& node) {
    // you fill in here
    rotateRight(node->left);
    rotateLeft(node);

    //call private helper not defined in header
    privmed();
}
Daniel Illescas
  • 5,346
  • 3
  • 17
  • 23