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
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.
#!/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)
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)
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)
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)
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)
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)
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)
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)
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)