C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-06-2007, 04:52 PM   #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
FoxTrot is offline   Reply With Quote
Old 09-06-2007, 04:56 PM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 09-06-2007, 05:05 PM   #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.
FoxTrot is offline   Reply With Quote
Old 09-06-2007, 05:15 PM   #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.
__________________
MacGyver is offline   Reply With Quote
Old 09-06-2007, 05:19 PM   #5
Registered User
 
Join Date: Oct 2001
Posts: 2,110
Quote:
15 \Pen.h `string' does not name a type
use std::string
Quote:
21 \main.cpp 'class Pen' has no member named 'CapCOlor'
spelling
robwhit is offline   Reply With Quote
Old 09-06-2007, 05:26 PM   #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
FoxTrot is offline   Reply With Quote
Old 09-06-2007, 05:34 PM   #7
Registered User
 
Join Date: Oct 2001
Posts: 2,110
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
http://www.cprogramming.com/tutorial/namespaces.html
robwhit is offline   Reply With Quote
Old 09-06-2007, 05:39 PM   #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.
__________________
MacGyver is offline   Reply With Quote
Old 09-06-2007, 05:47 PM   #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!
FoxTrot is offline   Reply With Quote
Old 09-06-2007, 07:07 PM   #10
Registered User
 
Join Date: Jan 2005
Posts: 7,252
Putting the using namespace std before #include "Pen.h" is bad practice. The best solution is to just use std:: in your header files.
Daved is offline   Reply With Quote
Old 09-06-2007, 07:40 PM   #11
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,282
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...
cpjust is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with classes and pointers Akkernight C++ Programming 18 02-21-2009 06:21 AM
Memory problem with Borland C 3.1 AZ1699 C Programming 16 11-16-2007 11:22 AM
Probably simple problem with classes slaad C++ Programming 6 12-14-2004 12:35 AM
Problem with destructors. Hulag C++ Programming 7 06-11-2004 12:30 PM
problem w/ nested templatized classes *ClownPimp* C++ Programming 8 10-19-2002 07:58 AM


All times are GMT -6. The time now is 10:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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