C++ Programming - Objects and Classes - Discussion
Discussion Forum : Objects and Classes - General Questions (Q.No. 24)
24.
Which of the following statements is correct about the program given below?
class Bix
{
public:
static void MyFunction();
};
int main()
{
void(*ptr)() = &Bix::MyFunction;
return 0;
}
Discussion:
19 comments Page 1 of 2.
Dipak Dhondge said:
1 decade ago
Compiler errors mean the compiler could not translate the source code you provided into object code. It usually means you have a syntactic or semantic error in your own program that you have to resolve before your program exhibits the behavior you're intending it to have.
Linker errors mean the linker could not build an executable program from the object code you provided. It usually means your program does not properly interface with its own dependencies or with the outside world (e.g. External libraries).
Linker errors mean the linker could not build an executable program from the object code you provided. It usually means your program does not properly interface with its own dependencies or with the outside world (e.g. External libraries).
Legndery said:
9 years ago
Linker Error because when the compiler is searching for the original function to link to at void (*ptr)() = &Bix::MyFunction; it cannot find it. because the function is not defined.
this below example works perfectly:
#include<iostream>
using namespace std;
class Bix
{
public:
static void MyFunction();
};
int main()
{
void (*ptr)() = &Bix::MyFunction;
(*ptr)();
return 0;
}
void Bix::MyFunction(){
cout<<"yay"<<endl;
}
this below example works perfectly:
#include<iostream>
using namespace std;
class Bix
{
public:
static void MyFunction();
};
int main()
{
void (*ptr)() = &Bix::MyFunction;
(*ptr)();
return 0;
}
void Bix::MyFunction(){
cout<<"yay"<<endl;
}
(2)
Vinay said:
1 decade ago
The linker error comes because the class is only declared and the definition is not there for the static member function. If we give a definition for member function then the linker error will be resolved by adding the following code.
void Bix::MyFunction()
{
std::cout << "I am a static function of Class Bix"
}
void Bix::MyFunction()
{
std::cout << "I am a static function of Class Bix"
}
Abhi said:
9 years ago
The linker error comes because the class is only declared and the definition is not there for the static member function.
If we give a definition for member function then the linker error will be resolved by adding the following code.
void Bix::MyFunction()
{
std::cout << "I am a static function of Class Bix"
}
If we give a definition for member function then the linker error will be resolved by adding the following code.
void Bix::MyFunction()
{
std::cout << "I am a static function of Class Bix"
}
Uday said:
1 decade ago
If you receive a linker error, it means that your code compiles fine, but that some function or library that is needed cannot be found. This occurs in what we call the linking stage and will prevent an executable from being generated. Many compilers do both the compiling and this linking stage.
Divya said:
10 years ago
:: this is a scope resolution operator. The scope resolution operator helps to identify and specify the context to which an identifier refers, particularly by specifying a namespace.
Srinivas said:
1 decade ago
Linker not able to find the definition for static function so throwing an an error. Not having a definition is not a semantic error so compiler was not finding it.
Bhanu said:
1 decade ago
Linker error is coming because we have redefine static function outside the class.
void Bix::Myfunction(); // Will solve linker problem.
void Bix::Myfunction(); // Will solve linker problem.
Java said:
1 decade ago
What is linker error ? because on being compiled this program shows no error and no output.
Jay said:
1 decade ago
We can't use * as pointer in CPP but instead of this we can use reference keyword.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers