hey,
Just starting to learn how to use classes in c++ and when i try to split my class out of the main code and put it in a headder or a headder and a second source file dev C++ is giving me loads of errors and i dont know what to do to fix it as ive never used dev C++ untill i started C++. Below is my source code for both files and then below those i have included the errors dev throws at me.

i know i could just leave the program in one file but i think its messy.

/********************************************************************
          
  File		: Pen_project_main.cpp
  Date		: 30, jul, 2008
  Target Hardware: N/A
  Tool chain	: DevC++ 
  Version	: 4.9.9.2

  Description	: 
	 	An onbect orientated program to model pens and provide thier main
	 	functionality including writing, breaking, and running out of ink
***************************************************************************/

//--------------------------------------------------------------------------
// Includes
//--------------------------------------------------------------------------

#include <iostream>
#include <stdlib.h>
#include <string>
#include "Pen_project_pen.h"

using namespace std;

//--------------------------------------------------------------------------
// MAIN Routine
//--------------------------------------------------------------------------


int main(int argc, char *argv[])
{
    /* decleration of 2 objects, favourite pen and worst pen*/
  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;
/*displaying information about the pens in the concolse
and calling the function write_on_paper*/
  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 0;
}

/********************************************************************
           
  File		: Pen_project_pen.h
  Date		: 30, jul, 2008
  Target Hardware: N/A
  Tool chain	: DevC++ 
  Version	: 4.9.9.2

  Description	: 
	 	class decleration and functionality declerations
***************************************************************************/
enum Color { blue, red, black, clear};
enum Penstyle { ballpoint, felttip, fountainpen};

class pen
{
      public:
             Color inkcolor;
             Color shellcolor;
             Color capcolor;
             Penstyle style;
             float length;
             string brand;
             int inklevel;
             
             void writeonpaper(string words)
             {
                  if(inklevel <=0)
                  {
                              cout << "oops out of ink" << endl;
                  
                  }
                  else
                  {
                      cout << words << endl;
                  }
             }
             void breakinhalf()
             {
                  inklevel = inklevel/2;
                  length = length/2.0;
             }
             void run out of ink()
             {
                  inklevel = 0;
             }
};

The errors i get when trying to complie this code is as follows

5 C:\prog\cpp\classes\classes1main.cpp In file included from classes1main.cpp
13 C:\prog\cpp\classes\pen.h `string' does not name a type
16 C:\prog\cpp\classes\pen.h variable or field `writeonpaper' declared void
16 C:\prog\cpp\classes\pen.h expected `;' before '(' token
28 C:\prog\cpp\classes\pen.h expected `;' before "void"
33 C:\prog\cpp\classes\pen.h `run' does not name a type
C:\prog\cpp\classes\classes1main.cpp In function `int main(int, char**)':
13 C:\prog\cpp\classes\classes1main.cpp `Pen' undeclared (first use this function)

(Each undeclared identifier is reported only once for each function it appears in.)
13 C:\prog\cpp\classes\classes1main.cpp expected `;' before "FavoritePen"
14 C:\prog\cpp\class
22 C:\prog\cpp\classes\classes1main.cpp expected `;' before "WorstPen"
23 C:\prog\cpp\classes\classes1main.cpp `WorstPen' undeclared (first use this function)
26 C:\prog\cpp\classes\classes1main.cpp `felt_tip' undeclared (first use this function)


C:\prog\cpp\classes\Makefile.win [Build Error] [classes1main.o] Error 1 this line comes from the pen.h file i think it may be the problem the confusion i have is before i had a simple function call from a seperate source file with a main in a .cpp file including a header file which held my function prototype for which the function was in a second source code and it worked fine.

any help here would be exellent im sure its a simple problem but as a rookie i havent the foggiest idea what it is

Thanks in advance
Kanoisa

Recommended Answers

All 3 Replies

Please learn to use code tags when posting to this board. That will maintain the indentation I hope you use in your code. You can read about code tags in the anouncement section at the top of the board or by reading the watermarks in the Message box where you enter your post.

Second learn how to debug your files as you write them. You don't have to write the whole program and then try to compile/debug. In this case the Pen_project_pen.h seems to be included in the overall project without problem. The error messages have to do with problems within the file itself. First include the string header file to the Pen_project_pen.h and either prefix the declaration of each string object with the std:: prefix or add a using statement to show that you want to use namespace std. Recompile and see what error reports remain.

I never realised i had to format the code to keep indentation im sorry bout that ,... ill see if i can edit that, and i have using namespace std below my #includes

#include <string> was present in the header file but i must have lost it when i was trying to use a global #include file which was sugegsted to me as a possible solutiuon by a friend but didnt work ,..

now ..to fix that formatting

Additional things I see looking through without compiling include:

void run out of ink()

should be run_out_of_ink() or runOutOfInk().

C and C++ are case sensitive meaning Pen is not the same as pen. You have to be consistent. I'd change this:

class pen

to this:

class Pen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.