Not compiling: saying unreferenced local variable!

I have two files, one cpp and one header. I have to report the circle's area, diameter, and circumference after asking the user to input radius. This is the error I get when I compile it:

1> Lab8.cpp
1>g:\lab8.cpp(36): warning C4101: 'circumference' : unreferenced local variable
1>g:\lab8.cpp(36): warning C4101: 'diameter' : unreferenced local variable
1>g:\lab8.cpp(36): warning C4101: 'area' : unreferenced local variable
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Does that mean I didn't use those variables in my program? I thought I did everything right.

This is my cpp file:

#include "Lab8.h"

int main() {
Circle c;
double radius, circumference, area, diameter;
double pi = 3.14159;
cout << "Please input radius: ";
cin >> radius;
c.setRadius(radius);

return 0;
}

//function to display values in passed Student object
void displayCircleData(Circle c) {
cout << "\nArea: " << c.getArea() << endl;
cout << "Diameter: " << c.getDiameter() << endl;
cout << "Circle: " << c.getCircumference() << endl;
}

// general behavior (access/calculation functions)
double Circle::getArea(){
return pi * radius * radius;
}

double Circle::getDiameter() {

return radius * 2;
}

double Circle::getCircumference() {
return 2 * pi * radius;
}

This is my header file:

#include <iostream>
using namespace std;

#ifndef LAB8_H // include
#define LAB8_H

// Circle class declaration
class Circle {
private:
double radius = 0.0;
double pi = 3.14159;

public:
double circumference, area, diameter;
//constructor
Circle(); // default constructor
Circle(double radius); //parameterized constructor

// mutators (aka setters)
void setRadius(double);

// accessors (aka getters) declared inline
double getRadius() const { return radius; }
double getDiameter();

// behavior (general access/calculation functions)
double getCircumference();
double getArea();

};
#endif
Does that mean I didn't use those variables in my program?

Exactly you declare them in main() but don't use them there. Just remove them and everything should be fine.
Thank you. I removed it, but now when I build it, it says:

1>Circle.obj : error LNK2019: unresolved external symbol "public: __thiscall Circle::Circle(void)" (??0Circle@@QAE@XZ) referenced in function _main
1>Circle.obj : error LNK2019: unresolved external symbol "public: void __thiscall Circle::setRadius(double)" (?setRadius@Circle@@QAEXN@Z) referenced in function _main
1>C:\Users\Haleema Mansoor\Desktop\ConsoleApplication3\Debug\ConsoleApplication3.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I would try to look for my error, but I don't know what that even means. This is the first time I'm programming with objects.
Make sure you have a definition for every function you've declared in the header file. I don't see definitions for the Circle constructors or the setRadius function in what you've posted.
If you're calculating the circle's various sizes on the fly when the relevant get function is called, do you need to have these data members? Or are you supposed to store the calculated values?

1
2
public:
double circumference, area, diameter;
I have made the changes you suggested. However, when I run the program and input the radius, nothing happens. I don't see the area, diameter and circumference. I have called the displayCircleData function as well. Here is my updated code.

This is Circle.cpp

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
#include "Circle.h"

// constructor (default)
Circle::Circle() {
}

//Circle method definition
//mutators (aka setters)
void Circle::setRadius(double radius) {
	
}
int main() {
	Circle c;
	double radius;
	double pi = 3.14159;
	cout << "Please input radius: ";
	cin >> radius;
	c.setRadius(radius);

	// display
	void displayCircleData(Circle c);

	return 0;
}

// function to display values in passed Circle object
void displayCircleData(Circle c) {
	cout << "\nArea: " << c.getArea() << endl;
	cout << "Diameter: " << c.getDiameter() << endl;
	cout << "Circumference: " << c.getCircumference() << endl;
}

// general behavior (access/calculation functions)
double Circle::getArea() {
		return pi * radius * radius;
}

double Circle::getDiameter() {
	
	return radius * 2;
}

double Circle::getCircumference() {
	return 2 * pi * radius;
}


This is Circle.h
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
#include <iostream>
using namespace std;

#ifndef CIRCLE_H 
#define CIRCLE_H

// Circle class declaration
class Circle {
private:
	double radius = 0.0;
	double pi = 3.14159;
public:
	Circle(); // default constructor
	Circle(double radius); // parameterized constructor

	// mutators (aka setters)
	void setRadius(double radius);

	// accessors (aka getters) declared inline
	double getRadius() const { return radius; }
	
	// behavior (general access/calculation functions)
	double getCircumference();
	double getArea();
	double getDiameter();
};
#endif 
Line 21 is a function declaration, not a function invocation.
void displayCircleData(Circle c);
Should be
displayCircleData(c);

You'll also need to actually do something inside Circle::setRadius()
Last edited on
Topic archived. No new replies allowed.