2

The following code compiles and runs perfectly,

#include <iostream>

class sam {
    public:
        void func1();
        int func2();
};

int main() {
    sam s;
}

Should it not produce a error for the lack of class member definition?

josh
  • 13,793
  • 12
  • 49
  • 58

5 Answers5

8

If you don't call the member functions, they don't have to be defined. Even if you call them, the compiler won't complain since they could be defined in some other compilation unit. Only the linker will complain. Not defining functions is accepted and common to force an error for undesired behavior (e.g. for preventing copying).

Philipp
  • 48,066
  • 12
  • 84
  • 109
4

Yes it is perfectly valid to not define a class member function if it is not used. That is also true for non-member functions. A definition is required for virtual functions, though. But pure virtuals can omit definitions if they aren't used.

"Used", btw, does not include refering to the function within sizeof. In other words, this is still valid:

sizeof (s.func2()); // still not used!
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Though, its usually not valid for virtual member functions. – nos Jul 19 '10 at 18:09
  • @sbi: Haha. And hm, I just noticed if I run out of votes that I can no longer even up-vote comments. :| @Gollum: On SO, you can mark inline code by surrounding it with backticks: \`code\` results in `code`. I escaped the former backticks with a backslash. – GManNickG Jul 19 '10 at 18:15
  • @GMan: Yeah I know this well enough, I was just joking. (Sometimes I find I have used up all my votes for the day when I come back from my lunch break. `:(` A really bad day is when I have hit the rep cap by that time, too. Then there's nothing to do but working until the evening. ``) – sbi Jul 19 '10 at 18:19
  • @GMan, thanks :), and looking at your(with @sbi) conversation, I feel the need of `SO-Meet&Greet-Event` – Ramadheer Singh Jul 19 '10 at 18:33
  • @GMan ohh! i learn something new about this SO engine everyday. Looks like SO needs a standard spec – Johannes Schaub - litb Jul 19 '10 at 18:48
  • @Gollum actually there was a SO developer meeting. But it was too far away from FF/M germany for me to join -.- – Johannes Schaub - litb Jul 19 '10 at 18:55
  • @Gollum: I'd come if you pay for the flight. `:-x` – sbi Jul 19 '10 at 20:44
  • @Johannes: Say, you didn't know you can run out of votes? Reeeaaallllyy? Didn't you do your duties eagerly enough?! – sbi Jul 19 '10 at 20:51
  • Oh, and I noticed a bug: "@sbi)" doesn't showing up in my responses, while "@sbi:" does. – sbi Jul 19 '10 at 20:52
  • @sbi ohh nono i meant about teh backslashes! xD – Johannes Schaub - litb Jul 19 '10 at 21:10
  • 1
    @Johannes: Thanks to a recently learned feature of SO I now know that it took you more than 15mins to find this excuse. `:)` – sbi Jul 19 '10 at 22:57
2

You can define it elsewhere (like another file)

void Sam::func1()
{
    // do stuff here
}
Michael J
  • 7,631
  • 2
  • 24
  • 30
1

From MSDN Linker Error LNK2019

cpx
  • 17,009
  • 20
  • 87
  • 142
0

You did define declared it. You just didn't add implementation provided a function prototype. The linker may display a warning if you do not have a function definition (for instance, Borland C++ Builder 5 does not).

  • 1
    The terminology is wrong, here. He *declared* them, but certainly didn't *define* them (which would be giving them an implementation). – GManNickG Jul 19 '10 at 18:02
  • See this: [What is the difference between a definition and a declaration?](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632) – sbi Jul 19 '10 at 18:06
  • 1
    The terminology is correct now (though strictly speaking there's no such thing as a prototype.) But now you're just back to his question. :) *Why* is it legal to only declare functions, and never define them? – GManNickG Jul 19 '10 at 18:07
  • @GMan: These aren't prototypes? http://opencbp.sourceforge.net/en_US.ISO8859-1/books/opencbook/func.prototypes.html –  Jul 19 '10 at 18:09
  • 1
    declaration can only help the code compile, it's linking phase when the **linker** looks for actual definition for that declaration(prototype), and it does so only if it encounters some code piece using that function. You don't get charged for what you do not use. – Ramadheer Singh Jul 19 '10 at 18:11
  • 1
    @0A0D: I mean since we were talking technical terminology, that "prototype" is an unofficial way of saying "declaration without definition". The language isn't technically defined in terms of prototypes, because saying "declaration" is enough. – GManNickG Jul 19 '10 at 18:13
  • @GMan: Sure, but it is still a prototype nonetheless. Just like there is no such thing as an Apple Class but I can create it (there are classes though just as there are declarations without definition) :) –  Jul 19 '10 at 18:17