Linking logic: is this even allowed?

This is a discussion on Linking logic: is this even allowed? within the C++ Programming forums, part of the General Programming Boards category; The objects 'board' and 'record' are not recognizing each other. I've used #ifndef #endif commands to prevent header redundancy, but ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    Linking logic: is this even allowed?

    The objects 'board' and 'record' are not recognizing each other. I've used #ifndef #endif commands to prevent header redundancy, but that might be part of the problem. Anyway:

    main.cpp:
    Code:
    #include <iostream>
    #include "cell.h"
    #include "board.h"
    ...
    board.h:
    Code:
    #ifndef BOARD_H
    #define BOARD_H
    #include "record.h" 
    #include "cell.h"
    //'board' object definition that contains a 'record' vector
    #endif
    record.h:
    Code:
    #ifndef COORD_H
    #define COORD_H
    #include "coord.h"
    #include "board.h"
    //'record' object definition that contains a 'board' pointer.
    #endif
    How do I make this work?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you could use a base class. Make the pointer a base class pointer that both are derived from.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Instead of
    Code:
    #include "board.h"
    use
    Code:
    class board;
    inside record.h. Since it is only a pointer, you don't need to include the full declaration, just a forward declaration to tell the compiler that the class exists somewhere. Just make sure you are only using the pointer in record.cpp.

    Then, make sure all your cpp files are compiled and linked and it should work fine.

    BTW, in your record.h code example you have "#ifndef COORD_H". If that wasn't a typo, it will also cause problems.

    No base class is necessary.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Thanks
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Daved
    BTW, in your record.h code example you have "#ifndef COORD_H". If that wasn't a typo, it will also cause problems.

    No base class is necessary.
    #ifndef COORD_H
    #define COORD_H

    #endif

    Are perfectly acceptable standard statements in c++
    They make sure that a header file isnt included twice in a c++ program.

    also base class could be easier because it would automatically create a record(Or any other class ADT) for you when you created an instance of the top class (that inherits from the base)

    an example is:

    Code:
    class Test : public base
    {
       public:
          Test () : base ();
    };
    Last edited by JoshR; 05-23-2005 at 05:18 PM.

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    Make sure your header files are in the same directory or specify your directory in the quotes ex: #include "C:\Cpp\myPrograms...."

    also make sure they are named what you included them as.

    I do not see a coord.h header file, do you have that file or did you forget to make it (its included in one of your headers)

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    >> record.h -> COORD_H
    Perfectly acceptable, but still a typo.

    While were on inheritance: Uses and Abuses of Inheritance, Part 1
    Part 2 is good too.

    gg

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by Codeplug
    >> record.h -> COORD_H
    Perfectly acceptable, but still a typo.

    oops didnt see that it was the record.h that had that name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 12:05 PM
  3. Problems linking with g++
    By Just in forum Linux Programming
    Replies: 11
    Last Post: 07-24-2006, 01:35 AM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21