Classes and Objects

I keep getting errors when I debug this code, but I don't understand why it's telling me this. Especially the parts about not finding or undeclared identifier.


1>------ Build started: Project: Lab 8, Configuration: Debug Win32 ------
1> Lab 8.cpp
1>Lab 8.cpp(32): error C2628: 'Distance' followed by 'int' is illegal (did you forget a ';'?)
1>Lab 8.cpp(32): error C3874: return type of 'main' should be 'int' instead of 'Distance'
1>Lab 8.cpp(39): error C3861: 'GetDistance': identifier not found
1>Lab 8.cpp(43): error C2146: syntax error : missing ';' before identifier 'cout'
1>Lab 8.cpp(42): error C3861: 'get_x1': identifier not found
1>Lab 8.cpp(42): error C3861: 'get_y1': identifier not found
1>Lab 8.cpp(44): error C2146: syntax error : missing ';' before identifier 'cout'
1>Lab 8.cpp(43): error C3861: 'get_x2': identifier not found
1>Lab 8.cpp(43): error C3861: 'get_y2': identifier not found
1>Lab 8.cpp(47): error C2664: 'Distance::Distance(const Distance &)' : cannot convert parameter 1 from 'int' to 'const Distance &'
1> Reason: cannot convert from 'int' to 'const Distance'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Lab 8.cpp(23): error C2065: 'x1' : undeclared identifier
1>Lab 8.cpp(23): error C2065: 'x2' : undeclared identifier
1>Lab 8.cpp(23): error C2065: 'y2' : undeclared identifier
1>Lab 8.cpp(26): error C2065: 'x1' : undeclared identifier
1>Lab 8.cpp(27): error C2065: 'x2' : undeclared identifier
1>Lab 8.cpp(28): error C2440: 'return' : cannot convert from 'double (__cdecl *)(double)' to 'double'
1> There is no context in which this conversion is possible
1>Lab 8.cpp(29): error C2065: 'y2' : undeclared identifier


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

// Declare Class
class Distance {
// Private
	double setx1, sety1, setx2, sety2;
public:	
//Constructor
	Distance(double x1, double x2, double y1, double y2){
		setx1 = x1;
		setx2 = x2;
		sety1 = y1;
		sety2 = y2;
	}
//Calculate and return distance between points
double GetDistance(){ return sqrt((x1-x2)^2) + ((y1-y2)^2); }

//Accessor Funtions
double get_x1() { return x1; }
double get_x2() { return x2; }
double get_y1() { return y1; }
double get_y2() { return y2; }
}

int main(){
//Pass Distance values to Main 
	Distance(5, 5, 99.5, 142.42);

	int length;
	
	//Calculate Distance
	length = GetDistance();

	//Output
	cout << "Point 1: x = " << get_x1() << "\t" "y = " << get_y1() << "\n"
	cout << "Point 2: x = " << get_x2() << "\t" "y = " << get_y2() << "\n"
	cout << "Distance is " << length << "\n";

	system("pause");
return 0;
}
You forgot the semicolon on line 25.
Topic archived. No new replies allowed.