A
Alexander Dong Back Kim
Dear all,
I used to use C++ programming language at all time but moved to C# and
Java. Few days ago, I restarted studying about C++ with a very
beginner's mind. I wrote a simple class and gcc couldn't compile the
class. Any hints that I'm missing?
Header File:
#ifndef __Calc_h__
#define __Calc_h__
class Calc
{
private:
int a;
int b;
public:
Calc();
Calc(int a, int b);
~Calc();
int plus();
int minus();
int multi();
int divide();
};
#endif
Source File:
#include "Calc.h"
Calc::Calc()
{
this.a = 0;
this.b = 0;
}
Calc::Calc(int a, int b)
{
this.a = a;
this.b = b;
}
Calc::~Calc()
{
this.a = 0;
this.b = 0;
}
int Calc:lus()
{
return this.a + this.b;
}
int Calc::minus()
{
return this.a - this.b;
}
int Calc::multi()
{
return this.a * this.b;
}
int Calc::divide()
{
if (this.b != 0)
{
return this.a / this.b;
}
else if (a != 0)
{
return this.b / this.a;
}
else
{
return 0;
}
}
GCC command:
$gcc Calc.cpp -lstdc++
Error Message:
Calc.cpp: In constructor 'Calc::Calc()':
Calc.cpp:6: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:7: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In constructor 'Calc::Calc(int, int)':
Calc.cpp:12: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:13: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In destructor 'Calc::~Calc()':
Calc.cpp:18: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:19: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In member function 'int Calc:lus()':
Calc.cpp:24: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:24: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In member function 'int Calc::minus()':
Calc.cpp:29: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:29: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In member function 'int Calc::multi()':
Calc.cpp:34: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:34: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In member function 'int Calc::divide()':
Calc.cpp:39: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:41: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:41: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:45: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:45: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
I think those error messages mean 'a' and 'b' are not members of Calc
class. Why is that?
Thanks,
I used to use C++ programming language at all time but moved to C# and
Java. Few days ago, I restarted studying about C++ with a very
beginner's mind. I wrote a simple class and gcc couldn't compile the
class. Any hints that I'm missing?
Header File:
#ifndef __Calc_h__
#define __Calc_h__
class Calc
{
private:
int a;
int b;
public:
Calc();
Calc(int a, int b);
~Calc();
int plus();
int minus();
int multi();
int divide();
};
#endif
Source File:
#include "Calc.h"
Calc::Calc()
{
this.a = 0;
this.b = 0;
}
Calc::Calc(int a, int b)
{
this.a = a;
this.b = b;
}
Calc::~Calc()
{
this.a = 0;
this.b = 0;
}
int Calc:lus()
{
return this.a + this.b;
}
int Calc::minus()
{
return this.a - this.b;
}
int Calc::multi()
{
return this.a * this.b;
}
int Calc::divide()
{
if (this.b != 0)
{
return this.a / this.b;
}
else if (a != 0)
{
return this.b / this.a;
}
else
{
return 0;
}
}
GCC command:
$gcc Calc.cpp -lstdc++
Error Message:
Calc.cpp: In constructor 'Calc::Calc()':
Calc.cpp:6: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:7: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In constructor 'Calc::Calc(int, int)':
Calc.cpp:12: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:13: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In destructor 'Calc::~Calc()':
Calc.cpp:18: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:19: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In member function 'int Calc:lus()':
Calc.cpp:24: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:24: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In member function 'int Calc::minus()':
Calc.cpp:29: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:29: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In member function 'int Calc::multi()':
Calc.cpp:34: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:34: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp: In member function 'int Calc::divide()':
Calc.cpp:39: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:41: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:41: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:45: error: request for member 'b' in 'this', which is of non-
class type 'Calc* const'
Calc.cpp:45: error: request for member 'a' in 'this', which is of non-
class type 'Calc* const'
I think those error messages mean 'a' and 'b' are not members of Calc
class. Why is that?
Thanks,