Compiling multiple cpp files (abstract factory pattern)


 
Thread Tools Search this Thread
Top Forums Programming Compiling multiple cpp files (abstract factory pattern)
# 1  
Old 06-07-2008
Power 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 javaSmilie) 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 Smilie

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
# 2  
Old 06-07-2008
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.
# 3  
Old 06-10-2008
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...
# 4  
Old 06-11-2008
chnaged color

you have a small error Smilie

you wrote :

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


but it should be Smilie

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

Last edited by vovan; 06-11-2008 at 11:00 AM.. Reason: changed color
# 5  
Old 06-13-2008
Oups, thanks Smilie ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pattern search multiple files

#!/usr/bin/ksh a="Run successfully" cd $APPS ls -l *.txt | while read $txt do if then cp $APPS/$txt cp $hist/$txt else rm $APPS/$txt echo "Files has been removed" fi done New in shell script please help me out Around 100 txt files in $APPS dir i want to search pattern from... (8 Replies)
Discussion started by: Kalia
8 Replies

2. UNIX for Dummies Questions & Answers

Error in compiling .cpp file

I get this error, defaults.cpp: In member function ‘int Defaults::GetIntDefault(const std::string&)’: defaults.cpp:68: error: ‘atoi’ was not declared in this scope defaults.cpp: In member function ‘real_t Defaults::GetRealDefault(const std::string&)’: defaults.cpp:76: error: ‘atof’ was not... (1 Reply)
Discussion started by: bstephens
1 Replies

3. Programming

Error with shared lIBMpi.so.1 when compiling CPP program

Hello, Met a problem when compiling a C++ program from source code without error, but when ran it there was always an error message: ./Ray: error while loading shared libraries: libmpi_cxx.so.1: cannot open shared object file: No such file or directoryAs the error points to openmpi which was... (0 Replies)
Discussion started by: yifangt
0 Replies

4. Programming

C++ abstract (singleton) factory implementation...

I want to create an abstract factory template which will allow me to pass in an "ID" for a subclass and return the singleton instance of that class stored in the factory. It'd be easy to adapt for "multi-ton", but for its present use this isn't necessary. The requirements are: - I don't want... (2 Replies)
Discussion started by: DreamWarrior
2 Replies

5. Web Development

<Apache>error when compiling CPP modules

Hi, I am working on Linux Platform. I am just trying to port a CPP module to apache as a module. When I try to build the Apache , it throws an error as follows libtool: unrecognized option `-DLINUX=2' Later I did some search and changed the config_vars.mk file under the "build" directory of... (0 Replies)
Discussion started by: ashabb
0 Replies

6. UNIX for Dummies Questions & Answers

Compiling multiple files

Hey there,hope you are all well. Actually i am trying to compile two .c files with gcc in unix and i also have one .txt and one .h..How do i compine them alltogether in order for my program to run? I will have to do gcc -o myprog myprog1.c myprog2.c and then how i will use myprog with the .txt... (1 Reply)
Discussion started by: Aeria Gloris
1 Replies

7. Shell Programming and Scripting

Searching across multiple files if pattern is available in all files searched

I have a list of pattern in a file, I want each of these pattern been searched from 4 files. I was wondering this can be done in SED / AWK. say my 4 files to be searched are > cat f1 abc/x(12) 1 abc/x 3 cde 2 zzz 3 fdf 4 > cat f2 fdf 4 cde 3 abc 2... (6 Replies)
Discussion started by: novice_man
6 Replies

8. UNIX for Dummies Questions & Answers

convert all *. c files to *.cpp files in a directory

Hiiiii.... how to convert all *. c files to *.cpp files , in a directory given using shell script. :pThnaking u.:p (10 Replies)
Discussion started by: krishnampkkm
10 Replies

9. Shell Programming and Scripting

Compiling multiple ".c" files starting with xxx

Hello, I am trying to figure out how I can write a bashscript that compiles several ".c" files that start with xxx (example: xxx_try.c and xxx_that.c) So I want to compile all these files with a bash script. Anyone can help pls? (6 Replies)
Discussion started by: Freak79
6 Replies

10. Programming

Compiling kernel mnodule spanning multiple files

Hello i guys i have a kernel module which has a list.c library I have one file mod.c (the main kernel module) and one list.c (which is the library) Could you please help me with the following questions .. 1) Is a header file needed (list.h) to be included in the mod.c ? 2) do we need to... (0 Replies)
Discussion started by: natraj
0 Replies
Login or Register to Ask a Question