Thread: Problem with classes

  1. #1

    Join Date
    Apr 2008

    Problem with classes

    Say you have two classes, class A and class B. class A is defined first, but class B has a prototype. When I make a method in class A whose argument is of type B&, I get a strange error message from the compiler saying " invalid use of undefined type 'struct B' ".

    Why am I getting this error, and what should I do to fix it?

    class B;
    class A
            void test(B& something)
                something.x = 0;
    class B
            int x;
    I get the following error:

    In member function `void A::test(B&)':
    (8) invalid use of undefined type `struct B'
    (1) forward declaration of `struct B'

    I'm using Dev-C++.
    Last edited by rudyman; 04-22-2008 at 11:22 PM.

  2. #2
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    It's been a while, but I think you need to define a new class B inside of class A, just like you would do if you were using an int or something else.

    something like this inside of class A:

    B class_b_variable;

    and then instead of using void test(B& something) you need void test(class_b_variable& something)

    I could be way off...
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    but I think you need to define a new class B inside of class A,

    class A is defined first, but class B has a prototype
    it is enough for using reference or pointer, not enough to access members

    or define class B first
    or move the function body into the cpp file where you can include full B class definition
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    Ė David J. Wheeler

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    Problem is that the compiler does not know how B looks like when compiling A, so you can't use B. Therefore it is always best to put the implementation in a source file. The source file can safely include both headers and you can do what you want to do without problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 06:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM