![]() |
| | #1 |
| Registered User Join Date: Sep 2007
Posts: 4
| Having a problem with Classes 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;
}
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
-ck |
| FoxTrot is offline | |
| | #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 | |
| | #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 | |
| | #4 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
|
That should fix a good number of errors if not them all.
__________________ |
| MacGyver is offline | |
| | #5 | ||
| Registered User Join Date: Oct 2001
Posts: 2,110
| Quote:
Quote:
| ||
| robwhit is offline | |
| | #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 | |
| | #7 |
| Registered User Join Date: Oct 2001
Posts: 2,110
| |
| robwhit is offline | |
| | #8 |
| Deathray Engineer 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 | |
| | #9 | |
| Registered User Join Date: Sep 2007
Posts: 4
| Quote:
Thanks to both of you for your help and thanks for the links robwhit, I'm checking them out now! | |
| FoxTrot is offline | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |