Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method declared in Class but not defined

I have Class which declares a method but does not implement it. The method is not a virtual function. In the corresponding cpp file I did not find the definition of the same method. All other methods declared in the class were defined.

I compiled the code and it went fine. I was of the impression that cpp must mandate the definition of a declared method.

Appreciate if somebody could elaborate on the same. I am using cl compiler from VS2010.

like image 494
akrohit Avatar asked Feb 09 '12 11:02

akrohit


People also ask

Which class is a class in which one or more methods are declared but not defined?

An abstract method is a method that is declared, but contains no implementation. Abstract classes cannot be instantiated, and require subclasses to provide implementations for the abstract methods.

Can we declare a method outside class?

Yes you can definitely have functions outside of a class.

Can class be defined inside main?

Declared class inside the main() function in C++ Show activity on this post. In the following program, I have declared class inside the main() function. and It's working fine in G++ compiler. But, If I remove static keyword and compile it, compiler gives an error.

How are objects implemented in python?

User-defined method objects may be created when getting an attribute of a class (perhaps via an instance of that class), if that attribute is a user-defined function object or a class method object.


2 Answers

Your code will compile but it will give linking errors.

Building an executable of your project involves two stages:

  • Compilation
  • Linking

During Compilation the compiler merely translates the source code into object code by verifying the language semantics.
During Linking the linker actually looks up for the definitions of the symbols and creates an executable from multiple object files (created during compilation).

The compiler compiles the source code in each translation unit (.cpp + header files) separately and hence it assumes the definition should be present in some other source file. It is the Linker who tries to find references to the function definitions, and hence the missing definition will be reported by the linker.

Note that the linker needs to link only those symbols which are used by your program,
For ex: If your program declares a function, provides no definition & then never uses/calls the function anywhere, the linker does not need to embed the code for jumping to the address where the object code for the function resides at any function call site.
Given such a scenario the linker will simply never need to look up for the function definition at all. Hence the code will compile and link.

like image 62
Alok Save Avatar answered Oct 13 '22 07:10

Alok Save


It is a common technique to prevent assignment or copy. If you declare it but not define it, a linking error will occur if you try to use it i.e. prevents people using it inadvertently

like image 26
Ed Heal Avatar answered Oct 13 '22 07:10

Ed Heal