Constructing Objects and Calling Member functions - c++

Here is my code
#include <iostream>
using namespace std;
class MyTestClass
{
int MyTestIVar;
public:
MyTestClass(void);
int firstCallMethod(void);
int secondCallMethod(void);
};
MyTestClass::MyTestClass(void)
{
MyTestIVar = 4;
}
int MyTestClass::firstCallMethod(void)
{
return secondCallMethod();
}
int MyTestClass::secondCallMethod(void)
{
return MyTestIVar;
}
int main(int argc, char *argv[])
{
MyTestClass mTC;
cout << mTC.firstCallMethod() << endl;
return 0;
}
If use use
MyTestClass mTC();
instead it will disallow me to call any member functions and display this error
./experiment.cpp: In function ‘int main(int, char**)’:
./experiment.cpp:31:14: error: request for member ‘firstCallMethod’ in
‘mTC’, which is of non-class type ‘MyTestClass()’
I read the posts on value-initialize etc, but this error still doesn't seem logical to me. Why would this affect member functions?
And help greatly appreciated :-)

MyTestClass mTC();
Does not declare an object of the MyTestClass class, as you think.
It Actually, declares a function by the name of mTC which does not take any parameters and returns an MyTestClass object.
This is known as the Most Vexing Parse in c++.

You have stumbled upon the most vexing parse.
The line
MyTestClass mTC();
is parsed as a function prototype of a function named mTC which has no arguments and returns an instance of MyTestClass.

Related

Why can I use the constructor without an object declaration?

Why can I use the constructor without a previous object declaration? And, if the previous works, why do I get an error when using k()?
Here is my code:
#include <iostream>
using namespace std;
class Test
{
int i, x;
public:
Test(int ii = 0)
{
cout<<"WOW!";
cout<<i<<" ";
i=ii;
}
void k()
{
cout<<i<<" ";
}
};
int main()
{
Test();
k(); ///k was not declared in this scope
return 0;
}
Why can I use the constructor without a previous object declaration?
A constructor is used to create a new object. So it can't be called on a previous object.
And, if the previous works, why do I get an error when using k()?
Because k() is a non-static method of Test, so it needs a Test object to call it on. You can't call it as a standalone function.
int main()
{
Test t;
t.k();
return 0;
}
You can create instance of the class because the name of the class can be found with name lookup from the current (the global in this case) namespace, and the class has been defined.
You haven't declared a free function by the name k, so you cannot call such function.

Error after variable used as a function parameter in C?

Can I use a variable as a function parameter after creating the variable while creating the function in C++ or another programming languages?
For example something like below. The code gets error but I wonder if it is possible to do this:
#include <iostream>
using namespace std;
int a = 0;
int dondur(a){
return a;
}
int main(int argc, char **argv)
{
int b=20;
cout << dondur(b);
return 0;
}
Up to the fact, that your function should read
int dondur(int a){
return a;
}
this is legal. The parameter 'int a' hides the global variable. The expected output is therefore 20 and the global variable a=0 remains unchanged.

Syntax to get function pointers in C++11

I am trying to understand function pointers and I have the following test code:
#include <iostream>
using namespace std;
class Test {
public:
void test_func() {
cout << "Test func called.";
}
};
void outer_test_func(Test &t, void (Test::*func)()) {
(t.*func)();
}
int main(int argc, char *argv[]) {
auto t = Test();
outer_test_func(t, &Test::test_func);
}
This works. But from what I understand Test::test_func and &Test::test_func both result in pointers. So why can't I use the former instead of the latter? If I try it g++ complains.
But from what I understand Test::test_func and &Test::test_func both result in pointers.
Test::test_func is not a valid syntax for creating pointer to member function, nor pointer to data members. It has never been a valid C++ syntax.
From cppreference (emphasis mine),
A pointer to non-static member object m which is a member of class C can be initialized with the expression &C::m exactly. Expressions such as &(C::m) or &m inside C's member function do not form pointers to members.

what is the difference when using (); vs ; when creating an object in c++?

in c++, whats the difference between writing something like
myclass myobject();
//and
myclass myobject;
also i'm new to stack overflow so if i'm doing something wrong just tell me.
When you write:
myclass myobject();
You may think you're creating a new object of type myclass, but you actually declared a function called myobject, that takes no parameters, and has a return-type of myclass.
If you want to see that for sure, check this code:
#include <stdio.h>
#include <iostream>
using namespace std;
class myclass
{ public: int ReturnFive() { return 5; } };
int main(void) {
myclass myObjectA;
myclass myObjectB(); // Does NOT declare an object
cout << myObjectA.ReturnFive() << endl; // Uses ObjectA
cout << myObjectB.ReturnFive() << endl; // Causes a compiler error!
return 0;
}
prog.cpp: In function ‘int main()’:
prog.cpp:18:23: error: request for member ‘ReturnFive’ in ‘myObjectB’, which is of non-class type ‘myclass()’
cout << myObjectB.ReturnFive() << endl;
^~~~~~~~~~
The difference is same as,
int a; and int a();
I am pretty sure that you understand now. Just for the sake of answer, I am explaining it below.
int a; // -> a is a variable of type int
int a(); // -> a is a function returning int with void paramters

Function "was not declared in this scope"

I'm new to C++ and get a beginner's mistake:
myclass.cpp: In function ‘int main()’:
myclass.cpp: 14:16: error: ‘func’ was not declared in this scope
This is the code:
#include <iostream>
using namespace std;
class MyClass{
public:
int func(int);
};
int MyClass::func(int a){
return a*2;
}
int main(){
cout << func(3);
}
I hope you can help me.
int main(){
cout << func(3);
}
func is not a global function; it is a member function of the class. You need an instance of the class to access it.
For example:
int main()
{
MyClass obj;
std::cout<< obj.func(3);
}
func is a member function, so it must be invoked through an object. For example:
int main()
{
MyClass obj;
std::cout << obj.func(3); // 6
}
In your example, you treated it as a free function, so the compiler looked for a function with that name. Since it could not find it, it issued a compiler error.
func is a member function of MyClass. To call it, you need an object of MyClass type to invoke it on:
int main(){
MyClass m; // Create a MyClass object
cout << m.func(3);
}
Alternatively, you could make func a static member function, which means that it is not associated with any particular instance of the class. However, you would still need to qualify its name as belonging to the MyClass class:
class MyClass{
public:
static int func(int);
};
int MyClass::func(int a){
return a*2;
}
int main(){
cout << MyClass::func(3);
}