Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > separating class declaration (header file) and implementation (sourcefile)

Reply
Thread Tools

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

 
 
Giovanni Gherdovich
Guest
Posts: n/a
 
      08-18-2008
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: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.
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      08-18-2008
Giovanni Gherdovich <(E-Mail Removed)> 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);

 
Reply With Quote
 
 
 
 
Giovanni Gherdovich
Guest
Posts: n/a
 
      08-18-2008
> -addition- is a class, not an instance

Aargh!

Thankyou, sorry for the stupid question.

Regards,
Giovanni
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      08-18-2008
Stefan Ram wrote:

> Giovanni Gherdovich <(E-Mail Removed)> 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);

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      08-18-2008
On Aug 18, 1:22*pm, Giovanni Gherdovich
<(E-Mail Removed)> 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:lus:

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

Cheers! --M
 
Reply With Quote
 
Giovanni Gherdovich
Guest
Posts: n/a
 
      08-19-2008
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:tr_fun
http://www.cplusplus.com/reference/m...l/ptr_fun.html
I don't need to write such "fake classes"
any more...

> second don't overlook std:lus:
> 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.
 
Reply With Quote
 
Giovanni Gherdovich
Guest
Posts: n/a
 
      08-19-2008
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: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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Separating a class definition into header and source files rajkirangrandhi@gmail.com C++ 4 03-18-2005 03:30 PM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Abstract Data Types - Separating Interface from Implementation Anon Email C++ 9 12-26-2003 02:03 AM



Advertisments