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

  • Thread starter Giovanni Gherdovich
  • Start date
G

Giovanni Gherdovich

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.
 
S

Stefan Ram

Giovanni Gherdovich said:
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);
 
G

Giovanni Gherdovich

-addition- is a class, not an instance

Aargh!

Thankyou, sorry for the stupid question.

Regards,
Giovanni
 
R

Rolf Magnus

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


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

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

Or: addition()(1,2);
 
G

Giovanni Gherdovich

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...

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.
 
G

Giovanni Gherdovich

Hello,
Rolf Magnus


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top