views:

341

answers:

10

I'm going through exercises of a C++ book. For each exercise I want to minimize the boilerplate code I have to write. I've set up my project a certain way but it doesn't seem right, and requires too many changes.

Right now I have a single main.cpp file with the following:

#include "e0614.h"
int main()
{
    E0614 ex;
    ex.solve();
}

Each time I create a new class from an exercise, I have to come and modify this file to change the name of the included header as well as the class i'm instantiating.

So my questions are:

  1. Can I include all headers in the directory so at least I don't have to change the #include line?
  2. Better yet, can I rewrite my solution so that I don't even have to touch main.cpp, without having one file with all the code for every exercise in it?

Update:

I ended up following Poita_'s advice to generate main.cpp via a script.

Since I'm using an IDE (Visual Studio), I wanted this integrated with it, so did a bit of research on how. For those interested in how, read on (it was fairly, but not entirely, straightforward).

Visual Studio lets you use an external tool via the Tools -> External Tools menu, and contains a bunch of pre-defined variables, such as $(ItemFileName), which can be passed on to the tool. So in this instance I used a simple batch file, and it gets passed the name of the currently selected file in Visual Studio.

To add that tool to the toolbar, right click on the toolbar, select Customize -> Commands -> Tools, and select the "External Command X" and drag it to the toolbar. Substitute X with the number corresponding to the tool you created. My installation contained 5 default pre-existing tools listed in Tools -> External Tools, so the one I created was tool number 6. You have to figure out this number as it is not shown. You can then assign an icon to the shortcut (it's the BuildMain command shown below):

alt text

A: 

Make a master include file containing the names of all the headers you want.

It's a really bad idea to include *, even if you could.

ddyer
A master file doesn't really help here. It just means that he includes all the headers in the master file instead of in main.cpp. It could help in other cases, but not here.
Peter Alexander
+3  A: 

If you're on Unix system, you can have a softlink that points to the latest excercise.

ln -s e0615.h latest.h

and name your class E instead of E0614, of course

P.S. To the best of my knowledge, you can't do #include xxx*

DVK
+8  A: 
  1. No. You have to include them all if that's what you want to do.

  2. No. At least, not in a way that's actually going to save typing.

Of course, you could write a script to create main.cpp for you...

Peter Alexander
+1 for suggesting a script
George Edison
A: 

You could use conditional compilation for the class name by using concatenation.

// Somewhere in your other files
define CLASS_NUMBER E0614

// in main.cpp
#define ENTERCLASSNUMBER(num) \
##num## ex;

// in main()
ENTERCLASSNUMBER(CLASS_NUMBER)

Don't know about the includes though. As suggested above, a script might be the best option.

George Edison
A: 

try this:-

#ifndef a_h
#define a_h

#include <iostream>
#include <conio.h>
#incl....as many u like
class a{
f1();//leave it blank
int d;
}
#endif //save this as a.h

later include this in ur main program that is cpp file

#include "a.h"

...your program

comfreak
A: 

writing a makefile rule to pass the name of the executable as a -DHEADERFILE=something parameter to the compiler shouldn't be difficult at all. Something like:

%.exe : %.h %.cpp main.cpp
    gcc -o $< -DHEADER_FILE=$<F $>

OTOH, I don't know if #include does macro expansion on the filename.

Ben Voigt
A: 
sed -i 's/\<\\([eE]\\)[0-9]+\\>/\19999/' main.cpp

Replace 9999 with the required number. There might be better ways.

alvin
+1  A: 
Roger Pate
Note that as this Python script grows, it will get refactored to have a main function and use the optparse (or similar) module.
Roger Pate
+5  A: 

If you build your code using make, you should be able to do this.

Can I include all headers in the directory so at least I don't have to change the #include line?

Change your include line to something like #include <all_headers.h>. Now, you can let your Makefile auto-generate all_headers.h with a target like:

all_headers.h:
    for i in `ls *.h`; do echo "#include <$i>" >>all_headers.h; done

Make sure that all_headers.h is getting deleted when you 'make clean'.

Better yet, can I rewrite my solution so that I don't even have to touch main.cpp, without having one file with all the code for every exercise in it?

You can do this if you abstract away your class with a typedef. In your example, change your class name from E0614 to myClass (or something). Now, add a line to your Makefile underneath the for loop above that says echo "typedef "$MY_TYPE" myClass;" >>all_headers.h. When you build your program, invoke 'make' with something like make MY_TYPE=E0614 and your typedef will be automatically filled in with the class you are wanting to test.

bta
There's a problem with that build rule: it won't pick up new headers unless you manually `make clean` or `trash all_headers.h`. Considering he's progressively completing exercises from a book, this is a significant impediment.
Roger Pate
I wouldn't call it a "significant" impediment. If he's adding new source files, he's probably doing a `make rebuild` each time anyway (using a target like `rebuild: clean build`).
bta
A: 

Why not using object mechanisms ?

You can use an Exemplar strategy for this.

class BaseExercise
{
public:
  static bool Add(BaseExercise* b) { Collection().push_back(b); return true; }
  static size_t Solve() {
    size_t nbErrors = 0;
    for(collections_type::const_iterator it = Collection().begin(), end = Collection().end(); it != end; ++it)
      nbErrors += it->solve();
    return nbErrors;
  }

  size_t solve() const
  {
    try {
      this->solveImpl();
      return 0;
    } catch(std::exception& e) {
      std::cout << mName << " - end - " << e.what() << std::endl;
      return 1;
    }
  }
protected:
  explicit BaseExercise(const char* name): mName(name)
  {
  }
private:
  typedef std::vector<BaseExercise*> collection_type;
  static collection_type& Collection() { collection_type MCollection; return MCollection; }

  virtual void solveImpl() const = 0;

  const char* mName;
}; // class BaseExercise

template <class T>
class BaseExerciseT: public BaseExercise
{
protected:
  explicit BaseExerciseT(const char* b): BaseExercise(b) { 
    static bool MRegistered = BaseExercise::Add(this);
  }
};

Okay, that's the base.

// Exercise007.h
#include "baseExercise.h"

class Exercise007: public BaseExerciseT<Exercise007>
{
public:
  Exercise007(): BaseExerciseT<Exercise007>("Exercise007") {}
private:
  virtual void solveImpl() const { ... }
};

// Exercise007.cpp
Exercise007 gExemplar007;

And for main

// main.cpp
#include "baseExercise.h"

int main(int argc, char* argv[])
{
  size_t nbErrors = BaseExercise::Solve();
  if (nbErrors) std::cout << nbErrors << " errors" << std::endl;
  return nbErrors;
}

And here, you don't need any script ;)

Matthieu M.