LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-26-2005, 07:47 PM   #1
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Compiling C++ classfile


I'm new to C++ and I can't figure out how to compile a class file. I have this simple example:

box.h
Code:
class box{

   public:
  
          int value();

};
box.cc
Code:
#include <iostream.h>

int box::value(){


          cout << "It works!" << endl;
    
           return 0;

}
I'm attempting to create a simple class box. Is there anything wrong with the code, and if not how do I compile it? I'm using g++ and I come from a java background. Thanks for any help.
 
Old 01-26-2005, 07:50 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Your missing #include "box.h" in your .cc file.
 
Old 01-26-2005, 07:54 PM   #3
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Original Poster
Rep: Reputation: 30
Thanks for your reply. I've included that statement.

Now I want to create a simple example to use this box class:

boxmain.cc
Code:
#include "box.h"
#include <iostream.h>

int main(){

          box b;

          b.value();

           return 0;

}
How do I compile this? I'm trying g++ -Wall -o box boxmain.cc but I'm running into errors.

Edit: I've already compiled box.cc with "g++ -c box.cc"
 
Old 01-26-2005, 08:48 PM   #4
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
cout and endl is the namespace std, so either place

using namespace std;

Below the include of iostream, or use the scope resolution std::cout and cout::endl

Also if your returning nothing from a function you can declare it void instead of int, and you don't need the return, and the boxman.cc doesn't require iostream include if you don't use any iostream stuff.

g++ -Wall -o box boxmain.cc box.cc

and to run it
./box

Last edited by leonscape; 01-26-2005 at 08:49 PM.
 
Old 01-26-2005, 08:58 PM   #5
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Original Poster
Rep: Reputation: 30
Thanks for all your help.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP!! Compiling on C killos Linux - Newbie 11 12-21-2004 04:49 PM
Various Compiling Errors (GCC compiling Openal, GUIlib, xmms-wma) gregorya Linux - Software 2 08-27-2004 05:03 AM
Compiling in ACPI support on Compaq 2135CA (system crashes while compiling) Dag Linux - Laptop and Netbook 20 07-30-2004 07:56 PM
Compiling....... chilehead Linux - Software 6 11-03-2003 03:11 PM
Kernel compiling and module compiling tarballed Linux - General 1 12-22-2002 05:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration