C++ class help menu

Shout-Out

User Tag List

Results 1 to 3 of 3
  1. #1
    lewie4's Avatar Contributor
    Reputation
    154
    Join Date
    Feb 2009
    Posts
    228
    Thanks G/R
    1/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C++ class help

    As I mentioned below this code was wrote after being awake for almost 50 hours. Its pretty terrible and misses alot of basic stuff (such a " " around the #include "A.h" etc) but managed to wake myself up and fix it so this thread can be locked/deleted.


    So I'm currently in university and I'm having a bit of trouble with my code. I've been up for almost 50 hours so I may just be missing something dumb but I can't seem to figure out how to do this. I cannot post any of my actual code due to the fact I am working with PS3 Dev kits and the source code is not allowed to be released publicly, so I will try my best to explain my problem without using any of the PS3 specific code.

    Say I have 2 header file (A.h and B.h) along with 2 matching cpp files (A.cpp and B.cpp)

    A.h looks similar to this

    #ifndef A_H
    #define A_H

    #include "B.h"

    class A
    {
    public:
    void function1();
    void functionA();
    B m_b;
    };

    #endif

    While B.h looks like this

    #ifndef B_H
    #define B_H

    #include "A.h"

    class B
    {
    public:
    void function2();
    void functionB();
    A m_a;
    };

    #endif

    Both these classes have other functions the the corresponding cpp files are required to used but I have left out. Within A.cpp I need to use all the functions from A.h and a single function from B.h and within B.cpp I need to use all the functions from B.h and a single function from A.h

    So A.cpp currently looks similar to this:

    #include A.h
    #include B.h

    void function1()
    {
    code;
    }

    void functionA()
    {
    code;
    m_b.function2();
    }

    and B.cpp looks like

    #include A.h
    #include B.h

    void function2()
    {
    code;
    }

    void functionB()
    {
    code;
    m_a.function1();
    }


    Now obviously there is a lot more code than this but this is where I am getting the problem when compiling. I get the compiler error "Error 2 error 20: identifier "A" is undefined" and the matching "Error 2 error 20: identifier "B" is undefined".

    Any help with this would be greatly appreciated. The sooner I get this done, the sooner I can sleep again
    Last edited by lewie4; 05-04-2014 at 03:05 AM.

    C++ class help
  2. #2
    lewie4's Avatar Contributor
    Reputation
    154
    Join Date
    Feb 2009
    Posts
    228
    Thanks G/R
    1/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow I'm tired and noticed how dumb I have been.

    For anyone else having this problem this is how to fix it:

    In B.h you need to forward declare class A and not include it as so:

    #ifndef B_H
    #define B_H

    class A;

    class B
    {
    public:
    void function2();
    void functionB();
    A m_a;
    };

    #endif

    Now include both A.h and B.h in B.cpp and it should fix the problem

    A mod can lock/close this

  3. #3
    stoneharry's Avatar Moderator Harry


    Reputation
    1618
    Join Date
    Sep 2007
    Posts
    4,564
    Thanks G/R
    151/150
    Trade Feedback
    0 (0%)
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Use code tags in the future, [-code] your code [-/code] (without the -).

    Code:
    function foo()
       doStuff();
    end

Similar Threads

  1. Replies: 2
    Last Post: 07-08-2014, 08:05 AM
  2. Making a bot with java (robot class) Help
    By ASDF4Ever in forum Programming
    Replies: 3
    Last Post: 09-10-2013, 01:50 AM
  3. Glider Custm class [help]
    By Siba89 in forum World of Warcraft General
    Replies: 2
    Last Post: 02-16-2008, 12:42 PM
  4. Class index id's!! Help
    By jokerjokes in forum World of Warcraft Emulator Servers
    Replies: 8
    Last Post: 10-23-2007, 07:11 AM
  5. HELP +REP ( WEB DESIGN CLASS, beggining)
    By BrightChild in forum Community Chat
    Replies: 14
    Last Post: 08-17-2007, 12:25 AM
All times are GMT -5. The time now is 11:55 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search