CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM (
-   -   How to add a new class locally (

ville December 8, 2006 06:20

Hi, This question has not bee
This question has not been asked and I thought this
could be usefull for many beginners like me: how to add a new class locally? Without any functionality I tried to add to the same folder where my solver is located just the frames for a new class. First, I included in the Make/files the new .C file. Then, naming the class sV the files

my sV.C file:
#include "sV.H"

namespace Foam {
sV::sV(){} // constructor

my sV.H file:
#ifndef sampleLinesV_H
#define sampleLinesV_H
namespace Foam {
class sampleLinesV

And in the main solver .C-file
I include the sV.H before main
and try to create an object inside
the main by stating

sV sob();

I tried also Foam::sV sob();
I also removed the namespaces from .C and .H
and tried all kinds of combinations but these did not work (to mention: removing the namespaces
produced a compiled version but no functionality
could be added to the class..). I think I should add some header
files (such as standard libraries in c) to my new class but which headers are those?

What about the namespace: why is practically everything (?) declared inside a namespace Foam
and what consequences does this have?
If someone could clarify and I can summarize
my experiences thereafter!

Thank you!

ville December 8, 2006 09:56

Sorry for confusion, of course
Sorry for confusion, of course I meant sV instead of sampleLinesV. This 'sV' was just an abbreviation
I mailed here. So the class name I want to declare is sV.

hartinger December 8, 2006 11:38

Hi Ville, your example look
Hi Ville,

your example looks alright, post the code and error message next time, otherwise its hard to say anything.

Namespaces are used to get some order in big projects and to avoid name conflicts. A class name like "vector" is relatively likely to used by many programmers. So to make sure to get the Foam "vector" you write "Foam::vector". Inside Namespaces you can have more Namespaces. A Foam example would be "Foam::fvc::div" and "Foam::fvm::div", which are explicit and implicit implementations of the divergence of a field.


ville December 11, 2006 10:10

Thank you for the answer! To
Thank you for the answer!
To repeat, in the folder /mysolver/ I wanted
to add a new class sV. Thus I add here the
files sV.C and sV.H and add the sV.C in the
/Make/files. The #include "sV.H" is added before
main in the mysolver.C. I would just like to
first make a function returning an integer.
Here are the files:

#include "sV.H"

namespace Foam {
sV::sV(const int tolerance):tole_(tolerance){}
int fV() { int integ; return(10);}

#ifndef sV_H
#define sV_H

namespace Foam {
class sV
const int tole_;
sV(const int tolerance);
int fV();

and in mysolver.C
int number;

sV sobj(3);

1) I could not define any OF types such as scalars in my own files. int as a type was accepted. This made me to suspect I should add some headers.

2) Does working inside a namespace already include
some standard libraries?

Here is the compilation error with this configuration:

DroptrackV.C: In function 'int main(int, char**)':
DroptrackV.C:90: error: no match for call to '(Foam::sV) ()'
make: *** [Make/linuxGcc4DPOpt/DroptrackV.o] Error 1

What is the mistake I'm making?
Thank you for the help!

hartinger December 11, 2006 14:20

Hi Ville, 1) you're right.
Hi Ville,

1) you're right. Add "fvCFD.H" at the top of your .H file. If you the compiler doesn't know a certain Foam thing, you have to include the appropriate Header file. Look at the example codes

2) No, it's just saving typing. Instead of "Foam::scalar" or "Foam::vector" you write "scalar" and "vector" if you're working inside a namspace. Namespaces are only for book-keeping.


All times are GMT -4. The time now is 00:57.