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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:08:15+00:00 2026-05-18T11:08:15+00:00

I am trying to make a template class where there is a function that

  • 0

I am trying to make a template class where there is a function that takes in a specific instance of that template. I have made the following contrived example to illustrate this.

Let’s say, I have a world of Individuals marked with a templated (generic) type of Data. I have a specific Individual, called a King. And all Individuals should be able to Kneel before a King. Individuals, in general, can be marked as anything. Kings are marked by numbers (the 1st, 2nd king).

The Error

g++ -g -O2 -Wall -Wno-sign-compare -Iinclude -DHAVE_CONFIG_H    -c -o Individual.o Individual.cpp
g++ -g -O2 -Wall -Wno-sign-compare -Iinclude -DHAVE_CONFIG_H    -c -o King.o King.cpp
In file included from King.h:3,
                 from King.cpp:2:
Individual.h: In member function ‘void Individual<Data>::KneelBeforeTheKing(King*)’:
Individual.h:21: error: invalid use of incomplete type ‘struct King’
Individual.h:2: error: forward declaration of ‘struct King’
make: *** [King.o] Error 1

Individual.h (Individual.cpp is empty)

//Individual.h
#pragma once
class King;
#include "King.h"
#include <cstdlib>
#include <cstdio>

template <typename Data> class Individual
{
protected:
    Data d;
    
public:

    void Breathe()
    {
        printf("Breathing...\n");
    };
    
    void KneelBeforeTheKing(King* king)
    {
        king->CommandToKneel();
        printf("Kneeling...\n");
    };
    
    Individual(Data a_d):d(a_d){};
};

King.h

//King.h
#pragma once
#include "Individual.h"
#include <cstdlib>
#include <cstdio>

class King : public Individual<int>
{
protected:

    void CommandToKneel();
    
public:
    
    King(int a_d):
        Individual<int>(a_d)
    {
        printf("I am the No. %d King\n", d);
    };
};

King.cpp

//King.cpp
#include "King.h"
#include <string>

int main(int argc, char** argv)
{
    Individual<std::string> person("Townsperson");
    King* king = new King(1);
    king->Breathe();
    person.Breathe();
    person.KneelBeforeTheKing(king);
    
}

void King::CommandToKneel()
{
    printf("Kneel before me!\n");
}

Makefile

CXX = g++
CXXFLAGS = -g -O2 -Wall -Wno-sign-compare -Iinclude -DHAVE_CONFIG_H 
OBJS = Individual.o King.o

test: $(OBJS)
    $(CXX) -o $@ $^

clean:
    rm -rf $(OBJS) test
    
all: test
  • 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-18T11:08:16+00:00Added an answer on May 18, 2026 at 11:08 am

    Your two classes King and Individual are very tightly coupled.

    Having it in two headers like that won’t work because both need each other.

    If your classes have to be designed that way then:

    1. First define class Individual but do not implement KneelBeforeTheKing, just declare that function.

    2. Then define King

    3. Then implement the method above.

    However your design is probably all wrong. For example your template class has many methods that are not dependent on the tamplated type, including KneelBeforeTheKing, and should be refactored out of the template.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.