{ A simple and interesting question about #include }

This is a discussion on { A simple and interesting question about #include } within the C++ Programming forums, part of the General Programming Boards category; I got an interesting problem: Lib.h Code: namespace MySpace{ class Utility{ ...omit constructor... void foo(); }; } Lib.cc Code: #include ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    { A simple and interesting question about #include }

    I got an interesting problem:

    Lib.h
    Code:
    namespace MySpace{
    class Utility{
    ...omit constructor...
    void foo();
    };
    }
    Lib.cc
    Code:
    #include "Lib.h"
    ...omit constructor...
    void MySpace::Utility::foo(){
    ...
    }
    Main.h
    Code:
    #include "Lib.h"
    namespace MySpace{
    class ABC{
    void abcFoo();
    };
    }
    Main.cc
    Code:
    #include "Main.h"
    void MySpace::ABC::abcFoo(){
    Utility uty;
    uty.foo();
    }
    The linker complains:
    Main.cc line3: undefined reference to MySpace::Utility::Utility()
    Main.cc line4: undefined reference to MySpace::Utility::foo()
    collect2: ld returned 1 exit status

    But when I put Utility class in Main.h the problem disappears!

    Main.h
    Code:
    namespace MySpace{
    class ABC{
    void abcFoo();
    };
    
    class Utility{
    void foo();
    };
    }
    Main.cc
    Code:
    #include "Main.h"
    void MySpace::ABC::abcFoo(){
    Utility uty;
    }
    
    void MySpace::Utility::foo(){
    ...
    }
    What's going on here?
    Last edited by meili100; 12-05-2007 at 10:34 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Is this the EXACT code you are using? Simplifications like "constructor omitted" are often where the problem truly lies

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also as a side note, why are the file extensions .cc instead of .cpp? I've never seen a .cc extension.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by cpjust View Post
    Also as a side note, why are the file extensions .cc instead of .cpp? I've never seen a .cc extension.
    I use .cc for my C++ source files. I understand that I am in the minority, but the minority does consist of a few more people than just me

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Did you add Lib.cc to your project/makefile/command line or whatever you use to compile the program?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If Lib.h and Lib.cc are part of a separate library (guessing by the name), then you need to link to that library before you can instantiate a class from it.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    gcc/g++ recognizes the following extensions as C++: '.C', '.cc', '.cpp'. Any others are treated as either C or object files.

    In linux/unix, where filename case isn't ignored, the extension '.C' is quite often used for C++, but that obviously doesn't work for Windows/DOS based environments, where a.c and A.C are considered the same file.

    The .cc isn't a common extension, but it's used in places.

    Back to the main programme here:
    It sounds like the compiler isn't told about lib.o (or lib.cc) when it's linking the application, which is why you get the errors for "no symbol".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    gcc also supports cxx, c++, cp, CPP, and others for header files. Check the man/info pages for more information.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    hey, should not you put semicolons at the end of namespace declarations?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Not on a namespace...
    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.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Problem solved! I forgot to put lib.o in Makefile

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need simple C graphics library
    By Sharke in forum C Programming
    Replies: 4
    Last Post: 02-16-2009, 09:25 PM
  2. Replies: 7
    Last Post: 11-15-2007, 12:36 AM
  3. Interesting Question
    By Swordsman in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 07-10-2007, 11:19 AM
  4. Replies: 2
    Last Post: 05-09-2007, 08:46 AM
  5. Simple Program?
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-13-2003, 12:36 AM

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