Having a problem with Classes

This is a discussion on Having a problem with Classes within the C++ Programming forums, part of the General Programming Boards category; Hey everyone, First a little background. I just started learning C++ using C++ for Dummies 7in1 (from my search on ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    4

    Having a problem with Classes

    Hey everyone,

    First a little background. I just started learning C++ using C++ for Dummies 7in1 (from my search on this site I guess some people have said it isn't as good as other books and may teach bad habits but it is what I have). I've had Pascal back 9th (25yo grad student now) grade and have been doing html since 6th or 7th and have a firm grasp on it, CSS, and some javascript.

    To the point... I've just reached the section of the book introducing classes. I am trying to run the code and running into compile errors. Running the code from the CD I receive the same errors (at least I think so). I believe the problem is in my header file establishing the class. Anyhow, here is my code, any help as to why it is giving me errors would be greatly appreciated!

    Using Dev C++ btw, 5 beta 9.2

    main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "Pen.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        Pen FavoritePen;
        FavoritePen.InkColor = blue;
        FavoritePen.ShellColor = clear;
        FavoritePen.CapColor = black;
        FavoritePen.Style = ballpoint;
        FavoritePen.Length = 6.0;
        FavoritePen.Brand = "Pilot";
        FavoritePen.InkLevelPercent = 90;
        
        Pen WorstPen;
        WorstPen.InkColor = blue;
        WorstPen.ShellColor = red;
        WorstPen.CapCOlor = black;
        WorstPen.Style = felt_tip;
        WorstPen.Length = 3.5;
        WorstPen.Brand = "Acme Special";
        WorstPen.InkLevelPercent = 100;
        
        cout << "This is my favorite pen" << endl;
        cout << "Color: " << FavoritePen.InkColor << endl;
        cout << "Brand: " << FavoritePen.Brand << endl;
        cout << "Ink Level: " << FavoritePen.InkLevelPercent << "%" << endl;
        FavoritePen.write_on_paper("Hello I am a pen");
        cout << "Ink Level: " << FavoritePen.InkLevelPercent << "%" << endl;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Pen.h
    Code:
    #ifndef PEN_H
    #define PEN_H
    #include <string>
    
    enum Color {blue, red, black, clear};
    enum PenStyle {ballpoint, felt_tip, fountain_pen};
    
    class Pen {
          public:
                 Color InkColor;
                 Color ShellColor;
                 Color CapColor;
                 PenStyle Style;
                 float Length;
                 string Brand;
                 int InkLevelPercent;
                 
                 void write_on_paper(string words) {
                      if (InkLevelPercent <= 0) {
                                          cout << "Oops! Out of ink!" << endl;
                      }
                      else {
                           cout << words << endl;
                           InkLevelPercent = InkLevelPercent - words.length();
                      }
                 }
                 void break_in_half() {
                      InkLevelPercent = InkLevelPercent / 2;
                      Length = Length / 2.0;
                 }
                 void run_out_of_ink() {
                      InkLevelPercent = 0;
                 }
    };
    #endif
    TIA for any help!

    -ck

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Could you post the errors? (Copy and paste from your compiler)

    --
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Sure no prob, here they are:

    3 \main.cpp In file included from main.cpp

    15 \Pen.h `string' does not name a type

    18 \Pen.h variable or field `write_on_paper' declared void

    18 \Pen.h expected `;' before '(' token

    27 \Pen.h expected `;' before "void"

    \main.cpp In function `int main(int, char**)':

    15 \main.cpp 'class Pen' has no member named 'Brand'

    21 \main.cpp 'class Pen' has no member named 'CapCOlor'

    24 \main.cpp 'class Pen' has no member named 'Brand'

    29 \main.cpp 'class Pen' has no member named 'Brand'

    31 \main.cpp 'class Pen' has no member named 'write_on_paper'

    \Makefile.win [Build Error] [main.o] Error 1

    *fixed 21
    Last edited by FoxTrot; 09-06-2007 at 05:08 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    1. Declare your usage of the std namespace before you include Pen.h. Otherwise, a better practice would probably be to specifically go through Pen.h and instead of using string, use std::string.
    2. "CapCOlor" is a typo. Fix accordingly. Variable names in C++ are case sensitive.


    That should fix a good number of errors if not them all.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    15 \Pen.h `string' does not name a type
    use std::string
    21 \main.cpp 'class Pen' has no member named 'CapCOlor'
    spelling

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    It works! Thanks for the help.

    What is the logic behind why #include "Pen.h" must occur after std namespace (not quite sure what the std namespace does yet either)?

    My book makes no mention of std namespace, which I figure must be due to an older compiler version or some other reason. Anyhow, when I was first learning and toying with headers, coming up with my own examples and such, I included them before std namespace and the program worked.

    I also am unsure as to what std::string does.

    Again, thanks for the help

  7. #7

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Put simply, so you don't have a bunch of naming conflicts, you can wrap classes and functions into a namespace. That means you have to specifically tell the compiler where you expect to find a class or function (that isn't in the global namespace). The "using namespace" line tells the compiler that you want to treat the following namespace as if it's global (so to speak), so that you can just specify a class or function from that namespace without being completely explicit.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    4
    Quote Originally Posted by MacGyver View Post
    Put simply, so you don't have a bunch of naming conflicts, you can wrap classes and functions into a namespace. That means you have to specifically tell the compiler where you expect to find a class or function (that isn't in the global namespace). The "using namespace" line tells the compiler that you want to treat the following namespace as if it's global (so to speak), so that you can just specify a class or function from that namespace without being completely explicit.
    That makes some sense but I think i'm still a bit away from fully grasping it. With a little more research/learning I think I'll have it.

    Thanks to both of you for your help and thanks for the links robwhit, I'm checking them out now!

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Putting the using namespace std before #include "Pen.h" is bad practice. The best solution is to just use std:: in your header files.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You should also #include <iostream> in your header file and put std:: before cout and endl, otherwise your code will get compile errors depending on what order your .cpp file includes your header...

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, 05:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Probably simple problem with classes
    By slaad in forum C++ Programming
    Replies: 6
    Last Post: 12-13-2004, 11:35 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

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