I am trying to run this program and do not understand why it will not run. I am getting 'Dog::age' is uninitialized and 'Dog::weight' is uninitialized.
You should really have a default constructor to initialise the class properly at the moment you instantiate it.
> I am getting 'Dog::age' is uninitialized and 'Dog::weight' is uninitialized.
The ints you need to init in a constructor.
The string has it's own ctor, so it doesn't complain about that.
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
#pragma warning(disable : 4700) // <--- Used to supress the compiler error, so the compile will not stop. Uned in VS - 2017.
int main()
{
int age, weight; // <--- Run then reverse the comments.
//int age{}, weight{};
std::cout << "\n Age = " << age << '\n';
std::cout << " Weight = " << weight << std::endl;
// <--- These lines are optional.
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0;
}
When I ran the program in VS - 2017 I had to add line 6 to allow the compiler to compile with out giving an error about uninitialized variables.
Reverse the comments on lines 10 and 11 to see the difference.
It is a good idea, if nothing more than knowing that the variables you define have a value, to initialize your variables when defined. This is mostly for bools, chars, any type of int and floating point variables. Strings, vectors and other type of containers have no size when defined and do not need initialized unless you want to put something in these type of variables to start with.
In the case of the class that you have and as salem c has said this should be done in the default ctor that you need to write. This way when the object is constructed the numeric variable are given a value.