The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
removing pattern which is spread in multiple lines sabyasm Shell Programming and Scripting 2 05-13-2008 02:19 AM
Concatenating multiple lines to one line if match pattern phixsius Shell Programming and Scripting 13 01-24-2008 08:02 PM
Pattern searching pattern in c files murthybptl Shell Programming and Scripting 6 11-17-2007 06:15 AM
multiple pattern split in perl umen Shell Programming and Scripting 3 07-31-2006 11:43 PM
Un-Compiling Files ?? tripp4337 UNIX for Advanced & Expert Users 2 03-09-2003 11:34 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-07-2008
Registered User
 

Join Date: May 2005
Posts: 19
Red face Compiling multiple cpp files (abstract factory pattern)

Hi all,

I have been working with java for awhile and because of my school projects I needed to switch C++. I tried to implement some patterns in C++ but unfortunately I couldn't. Specifically, I tried to implement abstract factory pattern but since I used separated files (habitual behavior from java) I got errors during compilation phase. Could you give me some tips about the compilation and modeling(if I should use .h files instead of .cpp files or not) of my code?

Here is my code:

Code:
//run.cpp
#include <iostream>

using namespace std;

int main(){
        HFact* fact;
#ifdef LINUX
        HFact* fact = new HelloLinFact;
#elif WINDOWS
        //HFact* fact = new HelloWinFact;
        cout<<"At the moment this implementation is not valid..."<<endl;
#endif

        CHello* hello = fact->getHelloNormal();
        hello->say();
        hello = fact->getHelloReversed();
        hello->say();
        hello = fact->getHelloWorld();
        hello->say();

        return 0;

}

//HFact.cpp
class HFact {
        public:
                extern virtual CHello* sayHelloNormal() = 0;
                extern virtual CHello* sayHelloRev() = 0;
                extern virtual CHello* sayHelloWorld() = 0;
};

//CHello.cpp
class CHello{
        public:
                virtual void say() = 0;
};

//HelloLinFact.cpp
class HelloLinFact : public HFact {
        public:
                CHello* getHelloNormal() {
                        return new HelloLinNormal;
                }

                CHello* getHelloReversed(){
                        return new HelloLinReversed;
                }

                CHello* getHelloWorld(){
                        return new HelloLinWorld;
                }
};

//HelloLinNormal.cpp
#include <iostream>

class HelloLinNormal : public CHello(){
        public:
                void say(){
                        cout>>"HelloNormal class says: Hello!";
                }
}

//HelloLinReversed.cpp
#include <iostream>

class HelloLinReversed : public CHello{
        public:
                void say(){
                        cout<<"HelloLinReversed says: olleH";
                }
}

//HelloLinWorld.cpp
#include <iostream>

class HelloLinWorld : CHello{
        public:
                void say(){
                        cout<<"HelloLinWorld says: Hello World!";
                }
}
I put all the classes in separate .cpp files. I will appreciate if you give me the compilation instructions for this code (I think I should link the object files to each other with gcc's -LObjName parameter but I didn't succeed). Additionally, any advice (putting classes to headers etc.) about the model is welcome.

Thanks for your helps in advance

Info. about my environment:
Linux 2.6.24-18-generic i686 GNU/Linux
gcc version 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
editor vim
Reply With Quote
Forum Sponsor
  #2  
Old 06-07-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
let's start with this:
Code:
g++  file1.cpp file2.cpp file3.cpp -o myprogram
The order may be important in terms of external symbols being defined. ld works from left to right. SO -- if an external symbol in file1 also needs symbols in file2, you have a problem.
Reply With Quote
  #3  
Old 06-10-2008
Registered User
 

Join Date: May 2005
Posts: 19
I think I should add the .cpp files using #include (and also I need to change the extension names to .hpp except run.cpp).

Thanks...
Reply With Quote
  #4  
Old 06-11-2008
Registered User
 

Join Date: May 2008
Posts: 10
chnaged color

you have a small error

you wrote :

void say(){
cout>>"HelloNormal class says: Hello!";
}


but it should be

void say(){
cout<<"HelloNormal class says: Hello!";
}

Last edited by vovan; 06-11-2008 at 07:00 AM. Reason: changed color
Reply With Quote
  #5  
Old 06-13-2008
Registered User
 

Join Date: May 2005
Posts: 19
Oups, thanks ...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:32 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0