Compilation errors

Sep 7, 2014 at 3:56am
Write your question here.
Hi,

I'm getting errors while compiling the below code.
please help me in resolving them..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

class MyClass
{

public:

    int value;

    MyClass(int val = 0) : value(val)
    {
        cout << "Ctor!!" << endl;
    }

    int Compare(MyCalss& x)
    {
        return (this->value > x.value);
    }

    ~MyClass()
    {
        cout << "Dctor!!" << endl;
    }
};

int main()
{
    MyClass m1(10);
    MyClass m2(20);

    if(m1.Compare(m2))
    {
        cout << "m1 is greater" << endl;
    }
    else
    {
        cout << "m2 is greater" << endl;
    }

    return 0;
}
Sep 7, 2014 at 4:16am
closed account (48T7M4Gy)
What does the error report say?
Sep 7, 2014 at 4:18am
Hi,

And the compiler errors are? Please post them in full -it saves us having to do "in brain " compiling & mind reading.

Some comments though:

I would rather not have line 2 - there is lots written why this is bad, put std::cout, std::endl etc instead.

Perhaps the MyClass::Compare could return a bool instead.

Neither of these are causing problems in your code though. What IDE & compiler are you using ?

Hope all is well
Sep 7, 2014 at 4:24am
closed account (48T7M4Gy)
You can run the program in this window by clicking in the top right hand corner where the gear symbol appears. It gives you a list of compile errors.

The first error is in line 16. Look carefully at line 16 first and repair the problem. Then see what happens. ;-)
Sep 7, 2014 at 5:03am
Here are the errors :

16:17: error: 'MyCalss' has not been declared In member function 'int MyClass::Compare(int&)': 18:33: error: request for member 'value' in 'x', which is of non-class type 'int' In function 'int main()': 32:21: error: no matching function for call to 'MyClass::Compare(MyClass&)' 32:21: note: candidate is: 16:9: note: int MyClass::Compare(int&) 16:9: note: no known conversion for argument 1 from 'MyClass' to 'int&'

I'm using codebolcks.

Changing the return type of function "Compare" to bool did not solve the problem.

Sep 7, 2014 at 5:15am
closed account (48T7M4Gy)
There's a problem in line 16 MyCalss
Sep 7, 2014 at 5:27am
1
2
3
4
int Compare(MyCalss& x)
    {
        return (this->value > x.value);
    }


I think that the result of this operation "this->value > x.value" is the 'bool' type. But the function "Compare" returns the 'int' type
Sep 7, 2014 at 5:40am
closed account (48T7M4Gy)
bool can have an integer value of 0 or 1.

In any case this is not causing the compiler error at line 16.

"Look carefully at line 16 first and repair the problem. Then see what happens. ;-)"
Last edited on Sep 7, 2014 at 5:41am
Sep 7, 2014 at 8:21am
changed to function return type to "bool"
bool Compare(MyCalss& x)
{
return (this->value > x.value);
}

still getting below compilation errors,

16:18: error: 'MyCalss' has not been declared In member function 'bool MyClass::Compare(int&)': 18:33: error: request for member 'value' in 'x', which is of non-class type 'int' In function 'int main()': 32:21: error: no matching function for call to 'MyClass::Compare(MyClass&)' 32:21: note: candidate is: 16:10: note: bool MyClass::Compare(int&) 16:10: note: no known conversion for argument 1 from 'MyClass' to 'int&'

Sep 7, 2014 at 10:27am
closed account (48T7M4Gy)
OK, I'll put you out of your misery. Your program has a typo.

Change MyCalss to MyClass in line 16 and there is a red x against that line.
Sep 7, 2014 at 10:59am
I use QtCreator IDE:
1
2
main.cpp:16: error: 'MyCalss' has not been declared
     int Compare(MyCalss& x)


This is because You have mistake in this word "MyCalss"
Topic archived. No new replies allowed.