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 6939043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:34:16+00:00 2026-05-27T12:34:16+00:00

I have a small collection of algorithms in Java for playing multiple turn-based games,

  • 0

I have a small collection of algorithms in Java for playing multiple turn-based games, such as TicTacToe, Othello, Checkers, etc. I do it using Java Generics (self-bounded types) to be able to use the same algorithms without having to change them specifically for each game. The reason why I use self-bounded types is not shown here, but is is needed for the Evaluation Functions.

public interface Game<GAME extends Game<GAME>> {
    GAME copy();
    int getCurPlayer();
    ...
}

public class TicTacToe implements Game<TicTacToe> {
    ...
    @Override
    public TicTacToe copy() {
        ...
    }
    @Override
    public int getCurPlayer() {
        ...
    }
    ...
}

Today, just for learning, I tried to move my Java code to C++, using C++ templates.

This was my approach, and obviously it didn’t work.

Game.h

template <typename T>
class Game
{
    public:
        virtual T copy() const = 0;
        virtual int cur_player() const = 0;
        ...
};

TicTacToe.h

class TicTacToe : public Game<TicTacToe>
{
public:
    virtual TicTacToe copy() const;
    virtual int cur_player() const;
    ...
};

TicTacToe.cpp

TicTacToe TicTacToe::copy() {
    ...
}

int TicTacToe::cur_player() {
    ...
}

When I try to compile, the errors I get are:

out-of-line definition of 'copy' does not match any declaration in 'TicTacToe'

out-of-line definition of 'cur_player' does not match any declaration in 'TicTacToe'

…
and the same for each of the other pure virtual functions.

  • 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-05-27T12:34:17+00:00Added an answer on May 27, 2026 at 12:34 pm

    Your definitions need to have const applied to them as well. The CRTP, as it’s known in C++ (Curiously Recurring Template Pattern) is perfectly valid C++.

    However, there’s no need for virtual here, the CRTP is used to statically dispatch functions and automatically implement functionality.

    template <typename T>
    class Game
    {
        T& crtp_cast() { return *static_cast<T*>(this); }
        const T& crtp_cast() const { return *static_cast<const T*>(this); }
    public:
        T copy() const { return crtp_cast(); }
        int cur_player() const { return crtp_cast().cur_player(); }
        ...
    };
    

    Note that in this case, the derived class does not need to implement a “copy” function, as the copy constructor will automatically be called by “copy”. However, in the general case, as templates are duck typed, it’s unnecessary to do this kind of thing, and normally you’d just use a standard template. Unlike Java’s Generics, C++’s templates have no relation to inheritance at all- the types you can instantiate with do not have to inherit from a common interface.

    template<typename Game> void f(const Game& g) {
        std::cout << g.cur_player();
    }
    class X {
    public:
        int cur_player() const { return 1; }
    };
    class Y {
    public:
        int cur_player() const { return 2; }
    };
    int main() {
        f(X());
        f(Y());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of about 10,000 small VBScript programs (50-100 lines each) and
I have this problem: I have a collection of small files that are about
I have a large collection of small objects, each has a unique string ident.
Assume you have a small collection of WebCMS assembled, subdirs in ~/www/test/ or so.
I have small problem. I learn java SE and find class ClassLoader. I try
have small problem, and would very much appreciate help :) I should convert byte
I have small page which has label, DropDownList and a submit button. <div> <asp:label
I have small problem with my .net 2.0 winforms application. I want to embed
I have small utility that does some processing on a file and changes the
I have small database of business and their addresses. Using the Google Geocode API

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.