C++ Compiler Warnings for Unused Variables in a Constructor

Feb 19, 2020 at 9:09pm
Hello,

I am writing a program that uses a class with dynamically allocated data member in it.
In the class constructor for the program, I get errors that the variables are not used.

"warning: unused variable 'name'
warning: unused variable 'value'"

1
2
3
4
5
Class::Class()
{
    char *name = NULL;
    int value = 0;
}


However, in my main I definitely create an instance of the class, and definitely use the data.

The program works, but I am still concerned about having warnings, as my instructor tends to treat any compiler warnings as a failure.

Does anyone have any suggestions as to how to go about fixing my constructor?


Last edited on Feb 19, 2020 at 9:10pm
Feb 19, 2020 at 9:18pm
Consider this function:
1
2
3
4
5
void example()
{
    char *name = NULL;
    int value = 0;
}

What does this function do with these two variables: name and value?
Nothing.

Those are local automatic variables that are created within the body of the function and automatically destroyed at the end of the body of the same function.

What does change, if we leave them out?
1
2
3
void example()
{
}

Nothing.

Your class may very well have some member variables that you do use, but these are not those variables.
Feb 19, 2020 at 9:22pm
Warnings aren't as big of an issue as errors. They aren't the same as errors.

The reason you are getting a warning is because name and value are local variables to the scope of the constructor. Since they are local variables, they will stop existing after the constructor is completed.

What is your purpose in doing this? Do you have data members of Class called name and value as well? If so, this is actually WRONG. If you want to set the data members, you have to do this:

1
2
3
4
5
Class::Class()
{
    name = nullptr; //Use nullptr instead of NULL if your compiler supports it
    value = 0;
}


Or better yet, get rid of the default constructor and simply initialize the data members with the values directly.
Last edited on Feb 19, 2020 at 9:53pm
Feb 19, 2020 at 9:24pm
1
2
3
4
5
Class::Class()
{
    char *name = NULL;
    int value = 0;
}


Is this your constructor? Does your class already have member variables char* and int?
It looks like you are defining them again in your constructor. thy this...

1
2
3
4
5
Class::Class()
{
    name = NULL;
    value = 0;
}
Feb 19, 2020 at 9:24pm
Thank you, I forgot to remove the char/int, they are declared in the class definition.

And we use C++98, so I don't think nullptr is included, and I'm not at all familiar with defining it myself via class or template, or whatever.

Thanks for the assistance.
Last edited on Feb 19, 2020 at 9:27pm
Feb 19, 2020 at 9:26pm
Warnings aren't an issue. They aren't the same as errors.

What? Warnings are issues that should be fixed, especially when learning the language.

In this case those warnings are seriously hinting that something needs looking into.

Feb 19, 2020 at 9:53pm
What? Warnings are issues that should be fixed, especially when learning the language.


My bad. I meant to say that warnings are not the same as errors. OP seems to be equating the two (although you can set your compiler to treat warnings as errors).
Feb 19, 2020 at 9:59pm
Treating warnings as errors is lesser evil than ignoring them completely.


C++98 should support member initializer list syntax:
1
2
3
4
5
Class::Class()
 : name( NULL ),
   value( 0 )
{
}

See https://www.learncpp.com/cpp-tutorial/8-5a-constructor-member-initializer-lists/
Feb 19, 2020 at 9:59pm
At this stage of the OP's learning curve, warnings should be considered the same as an error.

And IMO all warnings should be fixed, period.
Topic archived. No new replies allowed.