Compiler Error when creating a program about classes


I am trying to compile this program I created but I keep receiving compiler errors and I don't understand how to fix them. I created a Class called MyClass, it has private members and public members. Private Members:variable of type int named num.Another variable of type string named description.

public Members: Member functions: 1. Default Constructor: I Initialized num and description to 0 (Zero) with an empty string. 2. Constructor: Two input parameters(int and string types) and Initialize num and description using the parameters.

3. GetNum : No parameter, return the value of num member variable.
4. SetNum : 1 parameter of type int
set the value of num member variable equal to
the value of the parameter.
5. GetDesc : No parameter, return the value of description
member variable.
6. SetDesc : 1 parameter of type string
set the value of description member variable equal to
the value of the parameter.
7. Square : No parameter
return the square of the num member variable.
8. SquareRoot : No parameter
If num > 0, it Will return(integer portion only) the square
root of the num member variable, otherwise it will return 0.
9. Factorial : No parameter
If num > 0, it Will return the factorial of the num member
variable, otherwise it will return 1.
10. IsNegative : No parameter
Will return true(boolean) if the num member variable is < 0.
Will return false(boolean) if the num member variable is >=0.




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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

class MyClass
{
private:
int num = 0;
std::string description = 0;

public:
MyClass();

MyClass(std::string const& n, int num) : description(n){}
int GetNum();
int SetNum(int* Pointer);
string GetDesc();
char SetDesc(char* Pointer2);
int Square();
double SquareRoot();
int Factorial();
bool IsNegative();
};

MyClass::MyClass(){
//nothing
}

int GetNum()
{
int num;
return num;
}
void SetNum(int* Pointer)
{
int num;
*Pointer = num;
}
string GetDesc()
{
string description;
return description;
}
void SetDesc(string* Pointer2)
{
string description;
*Pointer2 = description;
}
int Square()
{
int num;
return(num * num);
}
int SquareRoot()
{
int num; 
if (num > 0)
{
return sqrt(num);
}
else
{
return 0;
}
}
int Factorial()

{
int num;
if (num > 0 )
{
for (int a = 1; a<=num; a++)
{
num = num * a;
return num;
}
}
else 
return 0;
}
bool IsNegative()
{
int num;
if (num < 0)
{
return true;
}
if (num >= 0)
{
return false;
}
else
{
return 0;
}
}
bool IsNegative()
{
int num;
if (num < 0)
{
return true;
}
if (num >= 0)
{
return false;
}
else
{
return 0;
}

void Display(MyClass b)
{
cout << b.GetNum() << endl;
cout << b.GetDesc() << endl;
cout << b.Square() << endl;
cout << b.SquareRoot() << endl;
cout << b.Factorial() << endl;
cout << b.IsNegative() << endl;
}

int main () 
{ 
bool flag = false;
char str1[100], str2[100];

cin >> str1 >> str2;
for (int i=0; i < strlen(str1); i++)
{
if (i==0 && str1[i] == '-')
continue;
if (!isdigit(str1[i]))
{
flag = true;
break;
}
}

if (!flag)
{
MyClass b(atoi(str1),str2); 
Display(b);
}
else
{
MyClass b; 
Display(b);

}

return 0;
}





The Compiler Error is:
myclass.cc: In function 'int main()':
myclass.cc:169:31: warning: comparison between signed and unsigned
for (int i=0; i < strlen(str1); i++)
^
myclass.cc:182:22: error: invalid user-defined conversion from 'int' to
MyClass b(atoi(str1),str2);
^
In file included from /usr/include/c++/4.8/string:52:0,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from myclass.cc:2:
Last edited on
There are two:
myclass.cc: In function 'int main()':
myclass.cc:169:31: warning: comparison between signed and unsigned
for (int i=0; i < strlen(str1); i++)

This is a warning, not error. The compiler asks: "Are you sure about this?"

The i is an int. The strlen() is size_t. The compiler will implicitly convert operands to same type before evaluation of <.

You could write:
for ( size_t i=0; i < strlen(str1); ++i )

myclass.cc:182:22: error: invalid user-defined conversion from 'int' to
MyClass b( atoi(str1), str2 );

Real error.
The atoi() is an int and the str2 is effectively char*.

Therefore, you do call function
MyClass::MyClass( int, char* )

Alas, there is no such function. Your MyClass has only
MyClass::MyClass( std::string const&, int )
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

class MyClass
{
private:
int num = 0;
std::string description = 0;

public:
MyClass();

MyClass(int n1, std::string const& n) : num(n1), description(n){}
int GetNum();
int SetNum(int* Pointer);
string GetDesc();
char SetDesc(char* Pointer2);
int Square();
double SquareRoot();
int Factorial();
bool IsNegative();
};

MyClass::MyClass(){
//nothing
}

int GetNum()
{
int num;
return num;
}
void SetNum(int* Pointer)
{
int num;
*Pointer = num;
}
string GetDesc()
{
string description;
return description;
}
void SetDesc(string* Pointer2)
{
string description;
*Pointer2 = description;
}
int Square()
{
int num;
return(num * num);
}
int SquareRoot()
{
int num; 
if (num > 0)
{
return sqrt(num);
}
else
{
return 0;
}
}
int Factorial()

{
int num;
if (num > 0 )
{
for (int a = 1; a<=num; a++)
{
num = num * a;
return num;
}
}
else 
return 0;
}
bool IsNegative()
{
int num;
if (num < 0)
{
return true;
}
if (num >= 0)
{
return false;
}
else
{
return 0;
}
}
bool IsNegative()
{
int num;
if (num < 0)
{
return true;
}
if (num >= 0)
{
return false;
}
else
{
return 0;
}

void Display(MyClass b)
{
cout << b.GetNum() << endl;
cout << b.GetDesc() << endl;
cout << b.Square() << endl;
cout << b.SquareRoot() << endl;
cout << b.Factorial() << endl;
cout << b.IsNegative() << endl;
}

int main () 
{ 
bool flag = false;
char str1[100], str2[100];

cin >> str1 >> str2;
for (int i=0; i < strlen(str1); i++)
{
if (i==0 && str1[i] == '-')
continue;
if (!isdigit(str1[i]))
{
flag = true;
break;
}
}

if (!flag)
{
MyClass b(atoi(str1),str2); 
Display(b);
}
else
{
MyClass b; 
Display(b);

}

return 0;
}


Here is my new code, but my compiler is issuing a new error message:
myclass.cc: In function 'int Factorial()':
myclass.cc:131:3: warning: control reaches end of non-void function [-Wreturn-type]
}
^
/tmp/ccIwu0FB.o: In function `Display(MyClass)':
myclass.cc:(.text+0x1be): undefined reference to `MyClass::GetNum()'
myclass.cc:(.text+0x1f0): undefined reference to `MyClass::GetDesc()'
myclass.cc:(.text+0x22c): undefined reference to `MyClass::Square()'
myclass.cc:(.text+0x257): undefined reference to `MyClass::SquareRoot()'
myclass.cc:(.text+0x282): undefined reference to `MyClass::Factorial()'
myclass.cc:(.text+0x2ad): undefined reference to `MyClass::IsNegative()'
collect2: error: ld returned 1 exit status.

I don't know what else I need to do to correct my code. Thank you for all your help!!
Line 82,98: You have two implementations of IsNegative().

Line 30,35,45,50,55,67,82: These are all member functions. They need MyClass:: in front of the function name.

Line 32,37,50,57,70,84: num is an unininitialized local variable (garbage). You want to refer to the member variable of the class. Get rid of the local declarations.

Line 85,89: You test if negative and return. Therefore the only remaining possibility is >= 0. No need for a separate test.

Lines 93-95: The else is totally useless. No way it can be reached.

Lines 82-97: The whole function can be rewritten as:
1
2
3
bool MyClass::IsNegative()
{ return (num < 0);
}


Some decent indentation would help make your program more readable.

Ok so I am receiving no more compiler errors, but there is a logic problem in my program when it produces an output. Please help me track down the illogical error in my program. Thank you so much.


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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
class MyClass
{
    private:
    int num = 0;
    std::string description = 0;
    
    public:
    MyClass();
    MyClass(int n1, std::string const& n) : num(n1), description(n){}  
    int GetNum();
    void SetNum(int* Pointer);
    string GetDesc();
    void SetDesc(string* Pointer2);
    int Square();
    double SquareRoot();
    int Factorial();
    bool IsNegative();
};

    MyClass::MyClass(){
    //nothing
    }
    
        int MyClass::GetNum()
            {
                return num;
            }
            void MyClass::SetNum(int* Pointer)
                {
                    *Pointer = num;
                }
                        string MyClass::GetDesc()
                {
                return description;
                    }
                        void MyClass::SetDesc(string* Pointer2)
                    { 
                        *Pointer2 = description;
                    }
                            int MyClass::Square()
                                {
                                    return(num * num);
                                }
                                double MyClass::SquareRoot()
                                {
                                    {
                                        if (num > 0)
                                        return sqrt(num);

                                        else
                                        return 0;
                                    }
                                }
    
                                    int MyClass::Factorial()
    
                                    {
                                        if (num > 0 )
                                        {
                                            for (int a = 1; a<=num; a++)
            
                                                num = num * a;
                                                return num;
                                        }
                                                else 
                                                return 0;
                                    }   

                                            bool MyClass::IsNegative()
                                            {return (num < 0);
                                            }   
void Display(MyClass b)
{
    cout << b.GetNum() << endl;
    cout << b.GetDesc() << endl;
    cout << b.Square() << endl;
    cout << b.SquareRoot() << endl;
    cout << b.Factorial() << endl;
    cout << b.IsNegative() << endl;
}

int main () 
{ 
    bool flag = false;
    char str1[100], str2[100];

    cin >> str1 >> str2;
    for (size_t i=0, len = strlen(str1); i < len; i++)
    {
        if (i==0 && str1[i] == '-')
            continue;
        if (!isdigit(str1[i]))
        {
            flag = true;
            break;
        }
    }

    if (!flag)
    {
        MyClass b(atoi(str1), str2); 
        Display(b);
    }
    else
    {
{
        MyClass b; 
        Display(b);
    }
    
    return 0;
}


The Sample input that I will give my program is
4
Classic
And the sample output should be
4
Classic
16
2
24
0
However the output my program gives is
4
Classic
16
2
-861720576
1

In you Factorial function you haven't used braces around num = num * a in your for statement, so the for loop doesn't execute. The side effect of this is that the variable a is undefined which gives you garbage ie -861720576
In you Factorial function you haven't used braces around num = num * a in your for statement, so the for loop doesn't execute.

That's incorrect.
http://www.cplusplus.com/doc/tutorial/control/
statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }.


Edit:
@OP - There are still a number of problems in your program.

Line 66: Factorial changes the value of num thereby invalidating the original value for subsequent use. Factorial should use a local variable so that the member variable num is not changed. Had you declared Factorial as a const function, the compiler would have detected this. This is why you're getting such a large number. Your termination condition in your for loop assumes num has not been changed. num get large very quickly and your loop runs millions of times making num larger and larger until it overflows.

1
2
3
4
5
6
7
8
9
10
11
int MyClass::Factorial() const
{   int temp = num;

    if (num > 0 )
    {   for (int a = 1; a<=num; a++)
            temp *= a;
        return temp;
    }
    else 
        return 0;
}   

Don't forget to add the const modifier to line 20.

line 3: You're missing the #include <string> header. You use std::string at line 9, which is fine, but at line 17 and elsewhere your references to string are not qualified with std::

Line 110: You have an extra {



Last edited on
Topic archived. No new replies allowed.