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

  • Home
  • SEARCH
  • 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 7979989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:55:55+00:00 2026-06-04T09:55:55+00:00

Why do I can’t make l->set(new StateB); in StateA ? (this line is commented

  • 0

Why do I can’t make l->set(new StateB); in StateA? (this line is commented below)

It says:

main.cpp: In member function ‘virtual void StateA::writeName(Lap*, char*)’:
main.cpp:19:4: error: invalid use of incomplete type ‘struct Lap’
main.cpp:3:7: error: forward declaration of ‘struct Lap’

but I cant solve that 🙁

#include <stdio.h>

class Lap;
class State;
class StateB;
class StateA;

class State { public:
    virtual void writeName(Lap *l, char *str) = 0;
};
class StateB : public State { public:
    void writeName(Lap *l, char *str) {
        printf("%s B\n", str);
    }
};
class StateA : public State { public:
    void writeName(Lap *l, char *str) {
        printf("%s A\n", str);
        //l->set(new StateB);
    }
};
class Lap { public:
    State *ss;
    Lap(){
        printf("[Lap]\n");
        set(new StateA);
    }

    void set(State *s){
        ss = s;
    }

    void writeName(char *str){
        ss->writeName(this, str);
    }
};

int main()
{
    printf("\n\n");

    Lap lap;
    lap.writeName((char*)"Fulano");
    lap.writeName((char*)"Fulano");

    printf("\n\n");
    return 0;
}
  • 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-04T09:55:56+00:00Added an answer on June 4, 2026 at 9:55 am

    The problem is that the forward declaration

    class Lap;
    

    only tells the compiler that such a class exists and lets you declare pointers to Lap, but it does not give the compiler enough information to handle any Lap method calls.

    So you need to have Lap fully declared before the attempt to use its method.

    With your code as given this can’t be done in a single file because Lap does new StateA while StateA calls a method on Lap — a circular dependency.

    You’ll need to move the declarations of at least one (and better yet, all) out to header files and include the headers where needed. Then the compiler will know the full interface details of the classes before either class’s definitions try to use methods in the other class.

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

Sidebar

Related Questions

can I make my own headers in HTTP request ? e.g. This is normal
Can't work out a way to make an array of buttons in android. This
can someone show me the regex for this preg_match. I want to make sure
Does anyone know how can I replace this 2 symbol below from the string
Can anyone tell the block animation equivalent of this below code snippet? [UIView beginAnimations:@View
Can this be done? It seems like this should be possible. In general, I
Can somebody help me with this. There is HTML code: <h3> <label> <input type=checkbox
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I run this in a Windows command prompt like I can run it
Can anybody help me? What should be the datatype for this type -07:00:00 of

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.