separating class declaration (header file) and implementation (sourcefile)

Discussion in 'C++' started by Giovanni Gherdovich, Aug 18, 2008.

  1. Hello,

    As you will see from the following code snippets,
    I'm trying to declare a function object in an header
    file and implement it in a (different) source file, but my compiler
    complains (he cannot find the implementation I provided,
    apparently).
    (the -c option tells the compiler to don't try to link,
    just compile).

    $ g++ -c addition.cpp
    $ g++ -c program.cpp
    program.cpp: In function `int main()':
    program.cpp:5: error: no matching function for call to
    `addition::addition(int, int)'
    addition.h:2: note: candidates are: addition::addition()
    addition.h:2: note: addition::addition(const
    addition&)

    here is the code:

    // ------- this is the file addition.h
    struct addition
    {
    int operator()(const int x, const int y) const;
    };
    // ------- end of the file addition.h

    // ------- this is the file addition.cpp
    #include "addition.h"

    int addition::eek:perator()(const int x, const int y) const
    {
    return x + y;
    }
    // ------- end of the file addition.cpp

    // ------- this is the file program.cpp
    #include "addition.h"

    int main()
    {
    double a = addition(1,2);
    }
    // ------- end of the file program.cpp

    Surely I'm missing something really obvious, but what?

    Regards,
    Giovanni Gh.
    Giovanni Gherdovich, Aug 18, 2008
    #1
    1. Advertising

  2. Giovanni Gherdovich

    Stefan Ram Guest

    Re: separating class declaration (header file) and implementation (source file)

    Giovanni Gherdovich <> writes:
    >I'm trying to declare a function object in an header
    >file and implement it in a (different) source file, but my compiler


    The error report has nothing to do with the separation into files.

    > double a = addition(1,2);


    »addition« is a class, not an instance. Try:

    addition add; double a = add(1,2);
    Stefan Ram, Aug 18, 2008
    #2
    1. Advertising

  3. Re: separating class declaration (header file) and implementation(source file)

    > -addition- is a class, not an instance

    Aargh!

    Thankyou, sorry for the stupid question.

    Regards,
    Giovanni
    Giovanni Gherdovich, Aug 18, 2008
    #3
  4. Giovanni Gherdovich

    Rolf Magnus Guest

    Re: separating class declaration (header file) and implementation (source file)

    Stefan Ram wrote:

    > Giovanni Gherdovich <> writes:
    >>I'm trying to declare a function object in an header
    >>file and implement it in a (different) source file, but my compiler

    >
    > The error report has nothing to do with the separation into files.
    >
    >> double a = addition(1,2);

    >
    > »addition« is a class, not an instance. Try:
    >
    > addition add; double a = add(1,2);


    Or: addition()(1,2);
    Rolf Magnus, Aug 18, 2008
    #4
  5. Giovanni Gherdovich

    mlimber Guest

    Re: separating class declaration (header file) and implementation(source file)

    On Aug 18, 1:22 pm, Giovanni Gherdovich
    <> wrote:
    > struct addition
    > {
    >   int operator()(const int x, const int y) const;};


    First, this could be a stand-alone function since it has no state, and
    second don't overlook std::plus:

    http://www.sgi.com/tech/stl/plus.html

    Cheers! --M
    mlimber, Aug 18, 2008
    #5
  6. Function objects and their instatiation. WAS: separating classdeclaration [...]

    Hello,
    thank you for your answers.

    mlimber
    > First, this could be a stand-alone function since it has no state,


    You are completely right. In the code I provided
    defining "addition" as a function object instead of
    an ordinary function is just stupid.

    But I wrote that code to isolate an error wich
    I had on such a function object...

    I usually write function objects without state
    when I have to pass them to algorithms, like
    the ones in <algorithm> or <numeric>.
    Since, in this kind of usage, I don't need
    to istantiate them and I think to them
    as "functions", sometimes I forgot that they
    are "classes", like above.

    And after discovering std::ptr_fun
    http://www.cplusplus.com/reference/misc/functional/ptr_fun.html
    I don't need to write such "fake classes"
    any more...

    > second don't overlook std::plus:
    > http://www.sgi.com/tech/stl/plus.html


    Thank you for the reference to the docs,
    but mine was only a toy example to reproduce
    an error I had in the code I'm writing.
    The addition was not my first concern.

    Rolf Magnus
    > Or: addition()(1,2);


    This _is_ interesting.

    What semantics has the line you wrote?
    I admit I was foo when posting my original question,
    and the first answer made me realize that a class
    _must_ be instatiated to do something useful.

    But you seem to use the function object as-it-is..
    Can you say more about this, or point to any
    doc/book/whatever?

    Thankyou.

    Regards,
    Giovanni Gh.
    Giovanni Gherdovich, Aug 19, 2008
    #6
  7. Re: Function objects and their instatiation. WAS: separating classdeclaration [...]

    Hello,

    > Rolf Magnus
    >
    > > Or: addition()(1,2);

    >
    > This _is_ interesting.
    >
    > What semantics has the line you wrote?
    > I admit I was foo when posting my original question,
    > and the first answer made me realize that a class
    > _must_ be instatiated to do something useful.
    >
    > But you seem to use the function object as-it-is..
    > Can you say more about this, or point to any
    > doc/book/whatever?


    I've just received an email from Mr. Ges. (in CC)
    that explain me the point that was unclair to me:

    SG:
    > Since addition::eek:perator() is a MEMBER function you have to create an
    > instance of addition first:
    >
    > double a = addition()(1,2); // equal to addition().operator()(1,2)
    >
    > Here "addition()" invokes the default constructor. The result is a
    > temporary addition instance upon you may call a member function.


    One last thing is unclair to me:
    when I write

    double a = addition()(1,2);

    what's the name of the instance of "addition"?
    Or is there a sort of "anonymous insance"?

    Regards,
    Giovanni Gherdovich
    Giovanni Gherdovich, Aug 19, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page