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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:56:09+00:00 2026-05-22T00:56:09+00:00

Here’s my problem: I wrote two base classes: Wire and CircuitComponent. The two were

  • 0

Here’s my problem: I wrote two base classes: Wire and CircuitComponent. The two were almost similar enough to derive from a common superclass, but not. Wire can only join with CircuitComponent, and CircuitComponent can only join with wire. The implementations were identical aside from type though, so naturally I thought templates were the answer.

Here’s the template, and I have a Wire class that derives from TwoTypeMesh<Wire, CircuitComponent> and a CircuitComponent class that derives from TwoTypeMesh<CircuitComponent, Wire> :

template <class thisType, class otherType>
class TwoTypeMesh {
    std::set<otherType *> neighbors;
public:
    void join(otherType * n){
        if (neighbors.find(n) != neighbors.end()) {
            return;
        } else {
            neighbors.insert(n);
            n->join(this);
        }
    }

    void disconnect(otherType * n){
        if (neighbors.find(n) == neighbors.end()) {
            return;
        } else {
            neighbors.erase(n);
            n->disconnect(this);
        }
    }
};

Problem is I can’t get it to compile, it complains about the line with n->join(this) cause this is of type TwoTypeMesh<Wire, CircuitComponent> (the superclass of Wire) but join is only defined for a wire.

My best theory thus far is that I shouldn’t be subclassing, maybe typedef, but I haven’t managed to make it work yet.

  • 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-22T00:56:10+00:00Added an answer on May 22, 2026 at 12:56 am

    The minimally-invasive way to make your code compile is indeed to use a typedef, and either tag classes, or simply Enumerations:

    enum MeshType { MeshTypeWire, MeshTypeCircuitComponent };
    
    template <MeshType thisType>
    class TwoTypeMesh {
        // calculate 'otherType' from 'thisType' (prevents usage mistakes):
        static const MeshType otherType =
            thisType == MeshTypeWire ? MeshTypeCircuitComponent :
            /* else */                 MeshTypeWire ;
        std::set< TypeTwoMesh<otherType> *> neighbors;
    public:
        void join(TypeTwoMesh<otherType> * n){
            if (neighbors.find(n) != neighbors.end()) {
                return;
            } else {
                neighbors.insert(n);
                n->join(this);
            }
        }
    
        void disconnect(TypeTwoMesh<otherType> * n){
            if (neighbors.find(n) == neighbors.end()) {
                return;
            } else {
                neighbors.erase(n);
                n->disconnect(this);
            }
        }
    };
    
    typedef TwoTypeMesh<MeshTypeWire> Wire;
    typedef TwoTypeMesh<CircuitComponent> CircuitComponent;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem I ran into recently. I have attributes strings of the form
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see
Here's an interesting problem. On a recently installed Server 2008 64bit I opened IE
Here is my current problem. I am working with the chat module and I'm
Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here is another spoj problem that asks how to find the number of distinct

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.