C++ How to compile multiple classes?

Fox5

Diamond Member
Jan 31, 2005
5,957
7
81
I'm messing around with g++ in Ubuntu at the moment, and I'm having trouble getting my project to compile. I'm also messing around with the boost libraries, and trying to just in general learn C++.

I have a 3 classes called shapeObject, matrix4D, and decompose. Each one is in a separate cpp file of the same name.

matrix4D.cpp has this at the top:
#include <boost/numeric/ublas/matrix.hpp>
using namespace boost::numeric::ublas;
struct matrix4D
{
... followed by code

shapeObject.cpp has this at the top:
#include <iostream>
#include <fstream>
#include <boost/numeric/ublas/matrix.hpp>
#include "matrix4D.cpp"
using namespace std;
using namespace boost::numeric::ublas;

class shapeObject
{
... followed by code


And decompose has:
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/foreach.hpp>
#include <boost/lambda/lambda.hpp>
#include "shapeObject.cpp"
#include "matrix4D.cpp"
using namespace std;
using namespace boost::numeric::ublas;
using namespace boost::lambda;

class decompose
{
... followed by code


Typing g++ matrix4D.cpp shapeObject.cpp compiles just fine. However, adding decompose.cpp as well creates a ton of errors. Some might be legitimate, but others confuse me, such as
matrix4D.cpp:3: error: redefinition of ?struct matrix4D?

This only occurs after compiling in all classes together. This leads me to believe there is something wrong with my include statements. I know of the concept in C of the header file, but I'm not sure how to use it or if it's what I need to do to fix my problem.
 
May 8, 2007
86
0
0
Include headers tell the preprocessor to put code from the specified file into the file the include stems from. In this case you are creating 2 separate declarations of the matrix4d struct. Its generally good practice to have separate interface (hpp file) and implementation (usually cpp files) and to only include the interface file. This way you can create include guards via the #ifndef #endif in the interface file for the preprocessor. Essentially you can say to the preprocessor if the interface is not defined then define it and if it is defined dont define the interface again.

There are a lot of intricacies that I ignored, but thats pretty much how I understand it.
 

Fox5

Diamond Member
Jan 31, 2005
5,957
7
81
Alright, I got it.

Now I don't suppose anyone has experience with lambda_functors, eh?
 

sao123

Lifer
May 27, 2002
12,656
207
106
Originally posted by: Fox5
I'm messing around with g++ in Ubuntu at the moment, and I'm having trouble getting my project to compile. I'm also messing around with the boost libraries, and trying to just in general learn C++.

I have a 3 classes called shapeObject, matrix4D, and decompose. Each one is in a separate cpp file of the same name.

matrix4D.cpp has this at the top:
#include <boost/numeric/ublas/matrix.hpp>
using namespace boost::numeric::ublas;
struct matrix4D
{
... followed by code

shapeObject.cpp has this at the top:
#include <iostream>
#include <fstream>
#include <boost/numeric/ublas/matrix.hpp>
#include "matrix4D.cpp"
using namespace std;
using namespace boost::numeric::ublas;

class shapeObject
{
... followed by code


And decompose has:
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/foreach.hpp>
#include <boost/lambda/lambda.hpp>
#include "shapeObject.cpp"
#include "matrix4D.cpp"
using namespace std;
using namespace boost::numeric::ublas;
using namespace boost::lambda;

class decompose
{
... followed by code


Typing g++ matrix4D.cpp shapeObject.cpp compiles just fine. However, adding decompose.cpp as well creates a ton of errors. Some might be legitimate, but others confuse me, such as
matrix4D.cpp:3: error: redefinition of ?struct matrix4D?

This only occurs after compiling in all classes together. This leads me to believe there is something wrong with my include statements. I know of the concept in C of the header file, but I'm not sure how to use it or if it's what I need to do to fix my problem.


I think using
#ifndef
#define
and
#endif

will alleviate the redefinition problem.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Separate the implementations from the definition, and then surround the definition files (.h) with ifdefs to prevent re-inclusion.

You're getting redef of matrix4D because "decompose.cpp" includes "matrix4D.cpp", and matrix4D.cpp is already in your project.

You could also just surround matrix4D.cpp itself with ifdefs to prevent redefinition of anything, if you'd like to keep your project as-is file-wise.

E.g., make matrix4D.cpp look like this:

#ifndef __MATRIX_4D__CXX__
#define __MATRIX_4D__CXX__

your normal code here

#endif // __MATRIX_4D__CXX__
 

Fox5

Diamond Member
Jan 31, 2005
5,957
7
81
Yep, got everything sorted out, though I do find those header files annoying.
Also, redoing my code for all the errors I'm coming up against is really making me appreciate object oriented coding, as well as better understand the differences between it and functional code. Memory management is so much easier with scoping, it's basically automatic with a good design. You also can't be as fancy with your functions however, and to achieve the same effect without coming up with a completely new design usually requires some management that'll probably have a bit of a performance hit.
It's kind of neat, since prior to this I didn't really see much advantage to object-oriented design other than a fancy way of doing gotos.