[try Beta version]
Not logged in

 
Not understanding Compiling faults

Sep 3, 2019 at 9:20am
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.

thanks for any help.


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
  #include<string>
#include<iostream>
using namespace std;

class Dog
{
	int age, weight;
	string colour;

public:

	void bark()
	{cout << "WOOF!" << endl;}

	void setAge(int yrs) { age = yrs; }
	void setWeight(int lbs) { weight = lbs; }
	void setColour(string clr) { colour = clr; }

	int getAge() { return age; }
	int getWeight() { return weight; }
	string getColour() { return colour; }

};

int Main()
{
	Dog fido;

	fido.setAge(3);
	fido.setWeight(15);
	fido.setColour("brown");

	cout << "Fido is a " << fido.getColour() << " dog" << endl;
	cout << "Fido is " << fido.getAge() << " years old" << endl;
	cout << "Fido weighs " << fido.getWeight() << " pounds" << endl;

	fido.bark();

	return 0;
}
Sep 3, 2019 at 9:39am
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.
Sep 3, 2019 at 11:20am
Hello euan,

In Addition to what salem c has said this program should help you understand a litter better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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.

Hope that helps,

Andy
Sep 3, 2019 at 2:48pm
althought your class is error prone, it was being used correctly here, no reason for the compiler to complain

you do have an «undefined reference to `main'», however. (case sensitive)
Sep 3, 2019 at 5:08pm
@euan,

Without a default constructor change line 7 to either:
int age = 0, weight = 0;
or
int age { }, weight { };
Sep 4, 2019 at 1:48am
Thanks for the help, but I'm still not getting this right. Adding =0 or{} gets rid of the original errors but still get.
Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

I got the program from C++ programming in easy steps, the publisher shows some printing errors but not on this page.


Sep 4, 2019 at 2:07am
On line 25, you wrote int Main() but meant int main() instead.
Last edited on Sep 4, 2019 at 2:07am
Sep 4, 2019 at 2:05pm
Thanks everyone for the help.
Topic archived. No new replies allowed.