0

I am developing a series of event classes. These classes contain information acquired from a system. They are from different natures and may contain different messages, for example: one possible event will contain a huge list of number, while another will carry query info in form of strings. I would like to create a builder function that creates the event of the correct type and returns to the user.

Here is a demonstration of how it will look like:

main.cpp:

#include <iostream>

#include "A.h"
#include "dA.h"

using namespace std;

int main()
{

   cout << "Hello world!" << endl;

   A a = build(5);

   return 0;
}

Event.h:

#ifndef A_H_INCLUDED
#define A_H_INCLUDED

#include <iostream>

class A
{
public:
    friend A build(int x);
protected:
    A(int x);
private:
};
#endif // A_H_INCLUDED

event.cpp:

#include "A.h"

A::A(int x)
{
    {
        std::cout << "A\n";
        std::cout << x << std::endl;
    }
}

A build(int x)
{
    if(x == 5)
        return dA(x);
    return make(x);
}

Special event.h

#ifndef DA_H_INCLUDED
#define DA_H_INCLUDED

#include <iostream>

#include "A.h"

class dA : public A
{
public:
protected:
    dA(int x);
};

#endif // DA_H_INCLUDED

special event.cpp:

#include "dA.h"

dA::dA(int x) : A(x)
{
    std::cout << "dA\n";
}

As you can see, all the constructors are protected, the intent is to hide the construction from the user - (s)he should not create events from nothing, only the system can - and to guarantee that build function will return the correct type of event.

When compiling, I get:

error: 'build' was not declared in this scope
warning: unused variable 'a' [-Wunused-variable]

If I put every thing in the same file, I can compile, however, I end up with a huge file difficult to manage. Note that the above code is just an example, the real class I will use are huge, putting all in the same file, both declaration and implementation, everything becomes a mess.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Fred
  • 417
  • 6
  • 14
  • Judging from the error encounted in Joachim's answer, my suggestion is to put it all into one file. That way you can isolate if you're just not building correctly, or if there's actually a coding error. – Cramer May 24 '13 at 02:53

1 Answers1

1

You have to declare the build function. Right now, you only tell the compiler that it's a friend of A but you do not declare it in the global scope.

Just add a prototype in the A.h header file:

A build(int x);

It seems that you don't link the files together properly. As it seems that you are using GCC (judging by the error messages) you can do it two ways:

  1. Combine compilation and linking in one step:

    $ g++ main.cpp event.cpp -Wall -g -o my_program
    

    In the above command you tell GCC to compile the files main.cpp and event.cpp and link them together, producing the my_program executable. The flags -Wall enables more warnings (which is good, and I personally use many more options to enable lots of warnings) and -g tells GCC to include debugging information.

  2. The second way is to compile the files separately into object files, and then link together those object files in a separate step:

    $ g++ main.cpp -c -Wall -g
    $ g++ event.cpp -c -Wall -g
    $ g++ main.o event.o -o my_program
    

    The new flag -c tells GCC to generate an object file with the same basename as the source file, so for main.cpp it will be named main.o. Then the last command takes those object files and links them together into the executable my_program.

If you have a lot of source files, the second way is preferred, as if you make changes to a single source file you don't have to compile all of them again just the one you changed. As it can be a lot of work writing these commands manually all the time, it's recommended to use tools such as make to automate these tasks.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I tryed that, and I got this: main.o||In function `main':| main.cpp|12|undefined reference to `build(int)'| ||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===| – Fred May 24 '13 at 02:21
  • @Fred Then you don't _link_ with the `event.cpp` file. – Some programmer dude May 24 '13 at 06:54
  • @Fred Updated my answer with information on how to compile and link multiple source files. – Some programmer dude May 24 '13 at 07:00
  • I use code blocks as IDE, it make all of those automaticcally. It compiles with mingw. I still don't know how to add the -c flag to it, however I will try your suggestions. Thanks for you attention. – Fred May 24 '13 at 11:54