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. |
|
|
myclass.cc: In function 'int main()': myclass.cc:169:31: warning: comparison between signed and unsigned for (int i=0; i < strlen(str1); i++) |
i
is an int
. The strlen()
is size_t
. The compiler will implicitly convert operands to same type before evaluation of <
.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 ); |
atoi()
is an int
and the str2
is effectively char*
.MyClass::MyClass( int, char* )
MyClass::MyClass( std::string const&, int )
|
|
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!! |
|
|
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. |
|
|
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 |
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. |
statement is executed. As usual, it can be either a single statement or a block enclosed in curly braces { }. |
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.
|
|
const
modifier to line 20.#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::