defining classes sharing same name in different namespaces

Hi,

the problem is following: files are not allowed to be identically named to be compiled.

In my particular case, I want to implement various models to deal with different material properties. I have different classes named after the models I'm using. Each is implemented in a separate file in different folders, files are named after the classes, e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// in property1/Constant/Constant.H:
namespace property1
{
  class Constant {...};
}

// in property1/PowerLaw/PowerLaw.H:
namespace property1
{
  class PowerLaw {...};
}

// in property2/Constant/Constant.H:
namespace property2
{
  class Constant {...};
}

Some models can be used for different properties, thus there are same named classes and accordingly .H files. They don't coincide as they belong to different namespaces, but similar file names cause compilation problems. Of cause, I could give each file an unique name, but I would reluctantly do this because it would be inconsistent with the logic of the previous versions. Is there any possibility to avoid this?

Best regards
Technically property1/Constant/Constant.H and property2/Constant/Constant.H are different filenames (err or at least file paths).

Do you use #include "property2/Constant/Constant.H" or just #include "Constant.H" ?

Exactly what error does the compiler give you?
Last edited on
I don't see anything wrong with that layout. I have, however, heard of some tools that do not like duplicate filenames throughout a project hierarchy. I forget offhand, as they were exceptional cases--not compilers, version control, or make. Perhaps it was a static analysis tool...
Do you use #include "property2/Constant/Constant.H" or just #include "Constant.H" ?

I use the second variant in the .C files, which lie in the same directories as corresponding .H files.
1
2
// in Constant/Constant.C:
#include "Constant.H" 

1
2
3
// in Make/files:
thermalConductivityModels/Constant/Constant.C
viscosityModels/Constant/Constant.C


Exactly what error does the compiler give you?
Constant.o: In function `Foam::thermalConductivityModels::Constant::Constant(Foam::word const&, Foam::dictionary const&, Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh> const&, Foam::GeometricField<double, Foam::fvsPatchField, Foam::surfaceMesh> const&)':
Constant.C:(.text+0x780): multiple definition of `Foam::thermalConductivityModels::Constant::Constant(Foam::word const&, Foam::dictionary const&, Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh> const&, Foam::GeometricField<double, Foam::fvsPatchField, Foam::surfaceMesh> const&)'
Constant.o:Constant.C:(.text+0x780): first defined here
Constant.o: In function `Foam::thermalConductivityModels::Constant::Constant(Foam::word const&, Foam::dictionary const&, Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh> const&, Foam::GeometricField<double, Foam::fvsPatchField, Foam::surfaceMesh> const&)':
Constant.C:(.text+0xa80): multiple definition of `Foam::thermalConductivityModels::Constant::Constant(Foam::word const&, Foam::dictionary const&, Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField, Foam::volMesh> const&, Foam::GeometricField<double, Foam::fvsPatchField, Foam::surfaceMesh> const&)'
Constant.o:Constant.C:(.text+0xa80): first defined here
collect2: ld returned 1 exit status
make: *** [~/OpenFOAM/userext-1.7.x/lib/linux64GccDPOpt/libcompressibleTransportModels.so] Fehler 1

Last edited on
I guess it's a problem with your makefile
You are trying to link against the same file several times
g++ foo.o main.o foo.o foo.o foo.o
I must admit, I don't know much about compiling. That's the good point to start learning it. I dont't have any makefile in the directory from which I compile the lib. I only have a Make folder with Make/files and Make/options within, and execute wmake libso to compile. So I'll go and read a little bit and come back later :-)
Topic archived. No new replies allowed.