Professional Web Applications Themes

PB & Implementation Files & C++ - Mac Programming

Hi, I'm trying out PB for a C++ class at school. The program in question needs a main file and implementation file (.cpp) and a header file (.h). This is a two-part question of sorts: with and without namespaces. With namespaces, PB fails to recognise any standard library items, particularly NULL and cout...despite the presence of "using namespace std;". Specifically: #include "header.h" using namespace std; namespace SomeStupidName { AnotherClass::AnotherClass() { int *myPointer = NULL; // error here cout << "stuff\n"; // error here } } } This is a simplified example of what I copied straight from the textbook, namespaces ...

  1. #1

    Default PB & Implementation Files & C++

    Hi,

    I'm trying out PB for a C++ class at school. The program in question
    needs a main file and implementation file (.cpp) and a header file (.h).

    This is a two-part question of sorts: with and without namespaces.

    With namespaces, PB fails to recognise any standard library items,
    particularly NULL and cout...despite the presence of "using namespace
    std;". Specifically:

    #include "header.h"

    using namespace std;

    namespace SomeStupidName {
    AnotherClass::AnotherClass() {
    int *myPointer = NULL; // error here
    cout << "stuff\n"; // error here
    } }
    }

    This is a simplified example of what I copied straight from the textbook,
    namespaces and all. CodeWarrior also fails to recognise anything in
    stdlib.

    A friend suggested stripping out the namespaces. I did that, leaving the
    header file to look like this:

    class AnotherClass {
    public:
    //Constructor
    AnotherClass();
    };

    PB now complains that AnotherClass was redefined. I'm not aware of a
    previous definition of AnotherClass, so this is indeed a strange error.

    What am I/my textbook doing wrong? Is this a combined PB/CodeWarrior
    quirk, perhaps? What can I do to get around this?

    Keep in mind I might have made an error in editing the sample code above;
    the real code is unnecessarily complicated for viewing audiences.

    If you need further info I will be glad to help. Thanks.
    Will Oram Guest

  2. #2

    Default Re: PB & Implementation Files & C++

    Will Oram <com> wrote in message news:<cwru.edu>...
     

    It sounds like you just are not including the proper headers. Even if
    you say you are using std it won't find cout if you don't include
    <iostream>. NULL is defined in <stdlib.h>. The authors of your book
    probably used an IDE that automaticly included some header files for
    every file in the project and didn't bother to add the includes to
    their samples. If this is the case I would seriously consider getting
    a new book.
     

    Now we get into the fun parts of the C-Precompiler. When you include
    headers it copies the symbols in the header (ie your class name) into
    it's symbol table (simplified). So when you include a header it copies
    all the symbols, now the next time you include the header it tries to
    copy all the symbols again, but they are already defined and you get
    an error. I am going to assume that you include "header.h" in main.cpp
    and your implementation.cpp. So when the compiler sees the include
    from the main.cpp it copies all the symbols defined in the header.h
    into its symbol table. Now when it compiles implementation.cpp in
    tries to copy all of the symbols again and causes an error because
    they are already defined. To get around this you need to encapsulate
    everything you define in your header into a "include sheild" (for the
    lack of a better name). It looks something like this:

    #ifndef __SOME_UNIQUE_IDENTIFIER__
    #define __SOME_UNIQUE_IDENTIFIER__

    class SomeClass {
    };

    #endif

    What this will do is let the compiler include the symbols once because
    the unique identifier is not defined. But after the first include the
    unique identifier is defined (that #define), and if you include the
    header file again, the compiler will skip over the definition of any
    symbols you may have made. By convention the unique identifier is
    based off of the header file name, for example __HEADER__ or
    _HEADER_H. Look at the iostream header to see another example.

    Or you could just skip all this sillyness and use #import which will
    import the symbols exactly once. =) The GCC people don't like this
    because you can play games with the C-Preprocessor and change the
    header based on the current set of defines so you would have to make
    sure to include that header rather then importing it which means you
    have to have know more about the implementation of the header rather
    then just knowing that you need it.
     

    You textbook is just being stupid and making assumptions about your
    IDE and assuming that it automaticly includes a standard set of
    headers. I would suggest getting a new book that doesn't make such
    assumptions.

    Michael
    Michael Guest

  3. #3

    Default Re: PB & Implementation Files & C++

    Michael Milvich wrote:
    | To get around this you need to encapsulate
    | everything you define in your header into a "include sheild" (for
    | the lack of a better name).

    The common name is "include guard".

    | It looks something like this:
    |
    | #ifndef __SOME_UNIQUE_IDENTIFIER__
    | #define __SOME_UNIQUE_IDENTIFIER__
    |
    | class SomeClass {
    | };
    |
    | #endif

    No, it looks something like this:

    #ifndef H_SomeClass
    #define H_SomeClass

    class SomeClass {
    };

    #endif

    *All* names containing two underscores anywhere (e.g.,
    __SOME_UNIQUE_IDENTIFIER__), or starting with one underscore and a
    capital letter (e.g,. _SomeUniqueIdentifier) are reserved for the
    implementation (the compiler and library), and *cannot* be safely
    used. The style "H_YourNameHere" *will* always be valid. (In general,
    names starting with underscores are best avoided, except as the names
    of Objective-C private methods.)

    Two warnings:

    "H_YourNameHere" may still conflict with some #define in some other
    header, especially if everyone follows the same convention. You might
    want to include your initials or some such distinguishing mark (e.g.,
    "H_XYZ_SomeClass").

    Names starting with a capital E are also reserved (for future error
    codes), so they're best avoided, too.

    Glen Fisher
    Glen Guest

Similar Threads

  1. web cam implementation
    By wschwisow in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 4
    Last Post: March 25th, 12:37 PM
  2. MD5 implementation
    By Casper Hornstrup in forum ASP.NET Security
    Replies: 3
    Last Post: February 4th, 01:53 PM
  3. RSA implementation for PHP
    By Daniel Albisser in forum PHP Development
    Replies: 1
    Last Post: October 23rd, 12:01 PM
  4. RSA implementation of PHP
    By Daniel Albisser in forum PHP Development
    Replies: 0
    Last Post: October 23rd, 10:06 AM
  5. NAT implementation on AIX 5.x
    By Bala in forum AIX
    Replies: 0
    Last Post: October 2nd, 02:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139