Alexander Dong Back Kim wrote:
> 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__
>
Other than the fact that "this" is a pointer, and you should use
"this->" instead of "this.", or just drop the "this" completely, your
include guard is also incorrect.
ISO/IEC 14882:2003 17.4.3.1.2/1 specifies that identifiers containing a
double underscore are reserved to the implementation. Also, don't
change it to _Calc_h_, since identifiers with a leading underscore
followed by an uppercase letter are reserved to the implementation in
the global namespace.
> 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::plus()
> {
> 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::plus()':
> 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,
>