Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8632535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:21:59+00:00 2026-06-12T09:21:59+00:00

I am using a template in a header file. The function I am using

  • 0

I am using a template in a header file. The function I am using is recursive and I would like to keep two variables outside of the function to compare against. Here is my code (note: does not compile):

#include "Position.h"
#ifndef _SOLVER_H
#define _SOLVER_H

class Solver{
  public:
    template<typename T>
    int EvaluatePosition(T &p, bool me) {
        int score = 0;
        if(p.isGameOver()) {
            return p.score(me);
        }
        else {
            std::vector<T> nextMoves = p.getNextPositions();
            for(int i=0; i != nextMoves.size(); i++) {
                score += EvaluatePosition(nextMoves[i],!me);
                if(score > maxScore) {
                    p.display();
                    maxScore = score;
                    maxP = p;
                    p.display();
                }
            }
            return score;
        }
    }

    T maxP; // Want this to be the same Type T that is used in the function
    int maxScore;
};

#endif

I am trying to create a variable of the same generic type as the T used in the function so that I can save out some data. Is this possible, and if it is, how would it be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:22:00+00:00Added an answer on June 12, 2026 at 9:22 am

    You can make your whole class a templated one, not just the function.

    template< class T > class Solver{
        //here goes your function that takes the argument of T class
        int EvaluatePosition(T &p, bool me){
            //...
        }
    
        T maxP; //here goes your variable
        int maxScore;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class defined in a header file like this. Here I
Here is the problem: Header file (part of a library's API): template <typename IterType>
Here is one of my header file which consists of a union template with
I have a templated column. I am using a Dropdown as header template and
I'm trying to define a function using template template parameters (I just want to
I am using the _.template() function of underscorejs together with backbonejs. When using underscore.js
I am using razor template and following is the scenario $(function(){ //if (ViewBag.IsCallFunction){ somefunction();
I am using a template function and I am passing and I may be
I'm code reviewing a colleagues code and found this: Header file: template<class T> class
I'm making a Perl website, and I'll using Template Toolkit (for the view), a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.