Compilation errors

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;
}
closed account (48T7M4Gy)
What does the error report say?
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
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. ;-)
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.

closed account (48T7M4Gy)
There's a problem in line 16 MyCalss
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
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
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&'

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.
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.