Compiler Error for Class

Hello wonderful people, I have written this code as per the instructions and I understand what I am doing (the code for the use of the class will be provided by the teacher when the work is collected).However, I keep getting a compiler error for the part of the code that says to return the value of the string. And also warning messages that say there isn't return statements. Will someone please help me? I could always use other suggestion to improve me code and/or learn some new things! 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
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
 #include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

/************************************************
 Implement a class per the following specification.
 Note: All the variable and function names are case sensitive. 
        Pay attention to the case.
 
 Name of the Class: MyClass
 Private Members:
    1.Variable 
        Name: num
        Type: int
    2.Variable 
        Name: description
        Type: string

 public Members:
  Member functions:
    1. Default Constructor: Initialize num and descrription to 0 (Zero) and 
                            empty string ("") respectively.
    2. Constructor: Take two input parameters(int and string types) and
                    Initialize num and descrription using the parameters. 
                    (Note: 1st parameter will be int type and 
                    2nd parameter will be string type)
    3. GetNum   : No parameter, Will return the value of num member variable.
    4. SetNum   : 1 parameter of type int
                    Will set the value of num member variable equal to 
                    the value of the parameter.
    5. GetDesc  : No parameter, Will return the value of description 
                    member variable.
    6. SetDesc  : 1 parameter of type string
                    Will set the value of description member variable equal to 
                    the value of the parameter.
    7. Square   : No parameter
                  Will 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.
***********************************************/
// WRITE YOUR CODE HERE, ADD MORE LINES AS NEEDED
//         ||
//         ||
//       \ || /
//        \||/
//         \/
class MyClass
{
    private:
        int num;
        string description;
    public:
        MyClass();
        MyClass(int n, string d);
        int GetNum();
        int SetNum(int x);
        int GetDesc();
        int SetDesc(string a);
        int Square();
        int SquareRoot();
        int Factorial();
        bool IsNegative();
};

MyClass::MyClass()
{
    num = 0;
    description = "";
}
MyClass::MyClass(int n, string d)
{
    num = n;
    description = d;
}
int MyClass::GetNum()
{
     return 0;
}
int MyClass::SetNum(int x)
{
    num = x;
}
int MyClass::GetDesc()
{
    return description;          //This is where the error is occurring
}
int MyClass::SetDesc(string a)
{
    description = a;
}
int MyClass::Square()
{
    return num*num;
}
int MyClass::SquareRoot()
{
    if(num > 0)
    {
        return sqrt(num);
    }
    else
    {
        return 0;
    }
}
int MyClass::Factorial()
{
    int factorial;
    if(num > 0)
    {
        for(int i = num; i > 0;i--)
        {
            factorial = factorial * i;
        }
        return factorial;
    }
    else
    {
        return 1;
    }
}
bool MyClass::IsNegative()
{
    if(num  < 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}





This is the compiler message:
myclass.cc: In member function ‘int MyClass::GetDesc()’:
myclass.cc:93:9: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘int’ in return
return description;
^
These are the warning messages:

myclass.cc: In member function ‘int MyClass::SetNum(int)’:
myclass.cc:90:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^

myclass.cc: In member function ‘int MyClass::SetDesc(std::string)’:
myclass.cc:98:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^

Thank you very much!!
Your mistake is actually quite obvious. Your getDesc() was declared as an int typed function. It should be a string

Like this:

Prototype:
 
string getDisc();


The other:
1
2
3
string MyClass::getDisc(){
return description;
}
Oh, Thank you so much for your help!!!! :D
The teacher send us two example inputs and outputs, so when enter then one works correctly the other one doesn't.

Sample input 1:
4
Classic
Sample output 1:
4
Classic
16
2
24
0

But I got:
0
Classic
16
2
-21984864
0

Could you possible help me out?
On the factorial, there's a problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int MyClass::Factorial()
{
//you need to initialize otherwise a random rubbish value would be calculated.
    int factorial = 1;
    if(num > 0)
    {
        for(int i = num; i > 0;i--)
        {
            factorial *= i;        
        }
        return factorial;
    }
    else
    {
        return 1;
    }
}
Last edited on
The output is almost right except for the 0 that I got should be a 4. Should I change:

int MyClass::GetNum()
{
return 0;
}

to

int MyClass::GetNum()
{
return num;
}

Because the function is suppose to print out the number that the user entered? Also, does the 0 really effect the output?
Last edited on
If I understood correctly, your getNum() function returns the number of the class right? Then you should just do:

1
2
3
int MyClass::GetNum(){
return num;
}


That's correct.Post main(), there could be something there.
Dude, you are a life saver!!! You helped me bring my F/D to a solid D almost C!!!!! THANK YOU SO MUCH!!!!!!!! I hope you have the best day ever, and all of your dreams come true!!!!!!!!!!!!! :D
You're welcome.
Last edited on
Topic archived. No new replies allowed.