GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Sep-2008, 15:32
Micah_Da_Giant Micah_Da_Giant is offline
New Member
 
Join Date: Sep 2008
Posts: 7
Micah_Da_Giant is on a distinguished road
Cool

Compiling multiple files in DevC++


I having problems compiling a header and and a .cpp file in a project.
I get message: "The system cannot find the file specified

Main.cpp

CPP / C++ / C Code:
#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;
    }
};
Pen.h

CPP / C++ / C Code:
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;
    }
};
Last edited by admin : 06-Sep-2008 at 18:37. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 06-Sep-2008, 15:51
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Compiling multiple files in DevC++


Quote:
Originally Posted by Micah_Da_Giant
I having problems compiling a header and and a .cpp
Main.cpp
1. You didn't post anything with a main() function. It looks like you pasted Pen.h twice. Did your real main.cpp file have #include "Pen.h" or what? Is Pen.h in the same directory as your main program file?

2. In C++, it's spelled class, not Class

3. You also have a capitalization inconsistency in one use of InklevelPercent.

Regards,

Dave
  #3  
Old 06-Sep-2008, 16:00
Micah_Da_Giant Micah_Da_Giant is offline
New Member
 
Join Date: Sep 2008
Posts: 7
Micah_Da_Giant is on a distinguished road

Re: Compiling multiple files in DevC++


Thank you Very much
  #4  
Old 06-Sep-2008, 16:21
Micah_Da_Giant Micah_Da_Giant is offline
New Member
 
Join Date: Sep 2008
Posts: 7
Micah_Da_Giant is on a distinguished road
Unhappy

Re: Compiling multiple files in DevC++


Pen.h


CPP / C++ / C Code:
#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;
    }
};

Main.cpp

CPP / C++ / C Code:
#include <iostream>
#include <stdlib.h>
[color="Red"]#include "Pen.h"[/color]
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;
    return 0;
}

Yes, I did include the "#include "Pen.h"" in the main.cpp file
Last edited by admin : 06-Sep-2008 at 18:38. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #5  
Old 06-Sep-2008, 18:24
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Compiling multiple files in DevC++


Quote:
Originally Posted by Micah_Da_Giant
Yes, I did include the "#include "Pen.h"" in the main.cpp file

Is Pen.h in the same directory as main.cpp?


Regards,

Dave
  #6  
Old 07-Sep-2008, 16:12
Micah_Da_Giant Micah_Da_Giant is offline
New Member
 
Join Date: Sep 2008
Posts: 7
Micah_Da_Giant is on a distinguished road

Re: Compiling multiple files in DevC++


What do you mean??
  #7  
Old 07-Sep-2008, 16:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Compiling multiple files in DevC++


Quote:
Originally Posted by Micah_Da_Giant
What do you mean??
I mean:

In what directory is your file "main.cpp"?

Is "Pen.h" in that same directory?

How did you create the files?

How did you save them?

Did you create a project in Dev-C++?

Did you add both of those files to the project before attempting to compile?


Regards,

Dave
  #8  
Old 07-Sep-2008, 16:37
Micah_Da_Giant Micah_Da_Giant is offline
New Member
 
Join Date: Sep 2008
Posts: 7
Micah_Da_Giant is on a distinguished road

Re: Compiling multiple files in DevC++


Yes, I did create a project in Dev C++. First, I created the project then I created the files for the projects. Yes, I believe that "Pen.h" is in the same directory.
  #9  
Old 07-Sep-2008, 17:00
Micah_Da_Giant Micah_Da_Giant is offline
New Member
 
Join Date: Sep 2008
Posts: 7
Micah_Da_Giant is on a distinguished road
Cool

Re: Compiling multiple files in DevC++


Now, I'm getting these errrors:

Line 3: In file included from main.cpp
Line 11: 'string' does not name a type
Line 13: variable or field 'write_on_paper' declared void
Line 13: expected ';' before '(' token
Line 14: expected unqualified-id before '{' token
in function 'int main(int, char**)':
Line 15: 'class Pen' has no member named 'Brand'
Line 23: 'class Pen' has no member named 'Brand'
Line 27: 'class Pen' has no member named 'Brand'
Line 29: 'class Pen' has no member named 'write_on_paper'
[Build Error] [main.o] Error 1

Pen.h

CPP / C++ / C Code:
#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;
    }
};

main.cpp

CPP / C++ / C Code:
#include <iostream>
#include <stdlib.h>
#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" <<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;
}
Last edited by LuciWiz : 08-Sep-2008 at 04:01. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #10  
Old 07-Sep-2008, 17:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Compiling multiple files in DevC++


Quote:
Originally Posted by Micah_Da_Giant
Now, I'm getting these errrors:

Line 3: In file included from main.cpp
Line 11: 'string' does not name a type

The Good News: It's now finding your header file.

The not-so Good News: There is a little more work to be done.

The Good News: It's really minor, once you get familiar with it:

At the point in your program where you include Pen.h, the compiler doesn't know what a "string" is, even though you have included <string>.

It's a namespace thing.

Here's how I would start Pen.h:
CPP / C++ / C Code:
#include <string>
#include <iostream>

using std::string;
using std::cout;
using std::endl;

.
.
.

In header files, I always give all of the information that they need, so they don't depend on something else having been included in the other file (like <iostream>)

This should clear up lots (most? maybe even all???) of the error messages. Try it again.

Regards,

Dave

Footnote: I would also recommend that you use C++ library headers in main.cpp:

CPP / C++ / C Code:
#include <iostream>
#include <cstdlib>
#include "Pen.h"

using namespace std;

int main(...

I mean, it's not "wrong" to include <stdlib.h>, but the "right way" is with <cstdlib>.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling and linking resource files. Preferably with tcc. Uten C Programming Language 1 22-Nov-2007 18:44
Need help for write data into multiple files sitha C++ Forum 8 19-Nov-2006 21:05
issues compiling .h and .cpp files with Dev C++ iceman2006 C++ Forum 9 17-May-2006 20:32
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 18:23
controling dialog objects & multiple source files omills MS Visual C++ / MFC Forum 0 15-Jul-2004 00:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:22.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.