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