Help with classes!

Pages: 123
Apr 16, 2013 at 6:07am
I need to create a class to store distances. I've stored the distances in the private section of the class (at least I believe I did) and I believe I got the GetDistance() line right. I want it to show the points and the distance between them. Using the coordinates given x1 = 12 - y1=20 and x2 = 40 - y2=50 to get the distance with the formula: sqrt((x1-x2)^2) + ((y1-y2)^2). I've been looking at my code for the past few hours and I can't figure out what else to do!

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

class Distance {
	double setx1, sety1, setx2, sety2, length;
public:	
	Distance(double x1, double x2, double y1, double y2){
		set_x1(12.0);
		set_y1(20.0);
		set_x2(40.0);
		set_y2(50.0);
	}
	double GetDistance() {return sqrt((x1-x2)^2) + ((y1-y2)^2);}

	double get_x1() { return x1; }
	double get_x2() { return x2; }
	double get_y1() { return y1; }
	double get_y2() { return y2; }
}
	
void main(){
	Distance	(12.0, 20.0, 40.0, 50.0);
	
	double length;

	length = GetDistance();

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

	system("pause");
return 0;
}
Apr 16, 2013 at 6:37am
.... and I believe I got the GetDistance() line right.


No. Can you look up the ^ operator to see what it does? Not what you think it does - I am sure.

Line 22 should be:

int main() {

always & forever - you have return 0; on line 34 the 2 contradict each other don't they?

setx1 is a bad name for a variable, but a good name for a function.

Lines 16 -19 refer to x1 etc - which are out of scope here. That should have attracted a compile error - if you have any of those then post them in full.

line 23 - 30 - you need to create an object in order to use a class.

Have a read of the tutorial on this site - top left of this page.

Hope all goes well.
Apr 16, 2013 at 6:42am
What is the purpose of the member 'length' in your class?

You do not have "set_??" member functions in your class. You don't even need them in constructor.

Your constructor accepts parameters, but you don't use them.

operator^ is binary xor, not power. Replace a^2 with a*a. Better yet, look up std::hypot, if you do use sufficiently C++11 compliant compiler.
Apr 16, 2013 at 2:40pm
The errors I get are:
1>Lab 8.cpp(22): error C2628: 'Distance' followed by 'void' is illegal (did you forget a ';'?)
1>Lab 8.cpp(22): error C3874: return type of 'main' should be 'int' instead of 'Distance'
1>Lab 8.cpp(27): error C3861: 'GetDistance': identifier not found
1>Lab 8.cpp(29): error C2065: 'a' : undeclared identifier
1>Lab 8.cpp(29): error C2228: left of '.get_x1' must have class/struct/union
1> type is ''unknown-type''
1>Lab 8.cpp(29): error C2065: 'a' : undeclared identifier
1>Lab 8.cpp(29): error C2228: left of '.get_y1' must have class/struct/union
1> type is ''unknown-type''
1>Lab 8.cpp(30): error C2065: 'a' : undeclared identifier
1>Lab 8.cpp(30): error C2228: left of '.get_x2' must have class/struct/union
1> type is ''unknown-type''
1>Lab 8.cpp(30): error C2065: 'a' : undeclared identifier
1>Lab 8.cpp(30): error C2228: left of '.get_y2' must have class/struct/union
1> type is ''unknown-type''
1>Lab 8.cpp(34): 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(9): error C3861: 'set_x1': identifier not found
1>Lab 8.cpp(10): error C3861: 'set_y1': identifier not found
1>Lab 8.cpp(11): error C3861: 'set_x2': identifier not found
1>Lab 8.cpp(12): error C3861: 'set_y2': identifier not found
1>Lab 8.cpp(14): error C2065: 'x1' : undeclared identifier
1>Lab 8.cpp(14): error C2065: 'x2' : undeclared identifier
1>Lab 8.cpp(14): error C2065: 'y2' : undeclared identifier
1>Lab 8.cpp(16): error C2065: 'x1' : undeclared identifier
1>Lab 8.cpp(17): error C2065: 'x2' : undeclared identifier
1>Lab 8.cpp(18): 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(19): error C2065: 'y2' : undeclared identifier
Last edited on Apr 16, 2013 at 2:57pm
Apr 16, 2013 at 2:43pm
Kariss wrote:
The errors I get are:
1>Lab 8.cpp(22): error C2628: 'Distance' followed by 'void' is illegal (did you forget a ';'?)
1>Lab 8.cpp(22): error C3874: return type of 'main' should be 'int' instead of 'void'
Apr 16, 2013 at 3:13pm
Okay, here is my updated code. It got rid of most of the errors but I still have some. I hope I'm on the right track now.... What am I missing to fix these issues?
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
#include <iostream>
#include <cmath>
using namespace std;

class Distance a{
	double set_x1, set_y1, set_x2, set_y2;
public:	
	Distance(double x1, double x2, double y1, double y2); {
		a.set_x1(12.0);
		a.set_y1(20.0);
		a.set_x2(40.0);
		a.set_y2(50.0);
	}
	double GetDistance() {return sqrt ((pow (double) (x1-x2),2.0) + (pow( (double) (y1-y2),2.0));}

	double get_x1(12.0) { return x1; }
	double get_x2(20.0) { return x2; }
	double get_y1(40.0) { return y1; }
	double get_y2(50.0) { return y2; }
}
	
int main(); 
{
	Distance	(12.0, 20.0, 40.0, 50.0);
	
	double length;

	length = GetDistance();

	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;
}


I fixed the sqrt line and added an a to the end of distance for the object. Now I only get 2 errors:
1>Lab 8.cpp(5): error C2470: 'a' : looks like a function definition, but there is no parameter list; skipping apparent body
1>Lab 8.cpp(23): error C2447: '{' : missing function header (old-style formal list?)
Apr 16, 2013 at 3:57pm
You misunderstood the semicolon thing. Remove the semicolon from the end of line 22 and put it at the end of line 20 instead:
20
21
22
};
	
int main()
Apr 16, 2013 at 4:00pm
1>Lab 8.cpp(5): error C2470: 'a' : looks like a function definition, but there is no parameter list; skipping apparent body

Why is the 'a' even there in the first place? It shouldn't be. Is it a typo?
Last edited on Apr 16, 2013 at 4:01pm
Apr 16, 2013 at 4:08pm
The a is there because that is how the instructor said to create the object.
"class Distance a; // create a Distance object
Double length;

// set the values into the object;
a.setx1(12.0)
a.sety1(20.0)
a.setx2(40.0)
a.sety2(50.0)"

But from what I've seen in the past labs his instructions are a bit misleading and sometimes don't really make sense, in the first lab I was asked to use a loop, but it doesn't need a loop nor could it use one and I got a 100 on it... so I'm not sure where the loop thing came from. I just can't figure out how to make this work.
Last edited on Apr 16, 2013 at 4:09pm
Apr 16, 2013 at 4:11pm
Kariss wrote:
The a is there because that is how the instructor said to create the object.
"class Distance a; // create a Distance object
You are not supposed to create the object in-between the class definition like that. You are supposed to create the object in main, not in the class definition.

This was your original (correct) code:
5
6
7
8
class Distance {
	double setx1, sety1, setx2, sety2, length;
public:	
    //... 
This is what you changed it to (incorrect):
5
6
7
8
class Distance a{ // <-- that "a" is not supposed to be there
	double set_x1, set_y1, set_x2, set_y2;
public:	
    //... 
Last edited on Apr 16, 2013 at 4:12pm
Apr 16, 2013 at 4:13pm
So take lines 5-19 and put them in main?
Apr 16, 2013 at 4:15pm
No! Just remove the "a" from line 5.

1
2
3
4
5
6
7
8
9
10
11
class ExampleClass
{ //this is a definition of a class
    int example;
public:
    void DoStuff(){}
}; //semi-colon here is needed

int main()
{
    ExampleClass my_variable_name; //this is creating an object
}
Last edited on Apr 16, 2013 at 4:16pm
Apr 16, 2013 at 4:20pm
These are the errors I get when I move the a to the main:
1>Lab 8.cpp(6): error C2447: '{' : missing function header (old-style formal list?)
1>Lab 8.cpp(25): error C2079: 'a' uses undefined class 'Distance'
1>Lab 8.cpp(25): error C2078: too many initializers
1>Lab 8.cpp(25): warning C4244: 'initializing' : conversion from 'double' to 'int', possible loss of data
1>Lab 8.cpp(29): error C3861: 'GetDistance': identifier not found
1>Lab 8.cpp(31): error C2039: 'get_x1' : is not a member of 'System::Int32'
1> c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.0\mscorlib.dll : see declaration of 'System::Int32'
1>Lab 8.cpp(31): error C2039: 'get_y1' : is not a member of 'System::Int32'
1> c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.0\mscorlib.dll : see declaration of 'System::Int32'
1>Lab 8.cpp(32): error C2039: 'get_x2' : is not a member of 'System::Int32'
1> c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.0\mscorlib.dll : see declaration of 'System::Int32'
1>Lab 8.cpp(32): error C2039: 'get_y2' : is not a member of 'System::Int32'
1> c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.0\mscorlib.dll : see declaration of 'System::Int32'
Apr 16, 2013 at 4:29pm
I did not say "move a to main", I said "just delete 'a' and nothing else"
Apr 16, 2013 at 4:41pm
Sorry! I just removed the 'a'. This is the code as it stands now and I'm receiving the errors that follow. I'm having trouble figuring out why it can't find the identifiers.
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
#include <iostream>
#include <cmath>
using namespace std;

class Distance;
{
	double set_x1, set_y1, set_x2, set_y2;
public:	
	Distance(double x1, double x2, double y1, double y2); {
		set_x1(12.0);
		set_y1(20.0);
		set_x2(40.0);
		set_y2(50.0);
	}
	double GetDistance() {return sqrt ((pow (double) (x1-x2),2.0) + (pow( (double) (y1-y2),2.0));}

	double get_x1(12.0) { return x1; }
	double get_x2(20.0) { return x2; }
	double get_y1(40.0) { return y1; }
	double get_y2(50.0) { return y2; }
};
	
int main()
{
	class Distance	(12.0, 20.0, 40.0, 50.0);
	
	double length;

	length = GetDistance();

	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;
}


Errors:
1>Lab 8.cpp(6): error C2447: '{' : missing function header (old-style formal list?)
1>Lab 8.cpp(25): error C2514: 'Distance' : class has no constructors
1> Lab 8.cpp(5) : see declaration of 'Distance'
1>Lab 8.cpp(29): error C3861: 'GetDistance': identifier not found
1>Lab 8.cpp(31): error C3861: 'get_x1': identifier not found
1>Lab 8.cpp(31): error C3861: 'get_y1': identifier not found
1>Lab 8.cpp(32): error C3861: 'get_x2': identifier not found
1>Lab 8.cpp(32): error C3861: 'get_y2': identifier not found

I'm sorry for my beginner's issues and I thank you for your patience. It means a lot to find help on stuff this difficult for me!
Apr 16, 2013 at 4:44pm
Remove the semicolon on line 5 - nobody told you to put that there. Also remove the semicolon on line 9 - remember, only the semicolon ;
Last edited on Apr 16, 2013 at 4:44pm
Apr 16, 2013 at 5:05pm
Done. How can I get them to find the identifiers? It should reference the gets above main, right?
Apr 16, 2013 at 5:12pm
Those are methods belonging to an object, not global functions. You call a method like so:

 
  myObect.myMethod();


Otherwise, how would the compiler know which instance of Distance you wanted to get the values from?

Talking of which... line 25 is not doing what you think it's doing. You declare an instance of a class the same way as you declare any other variable.
Apr 16, 2013 at 5:17pm
So it would be

 
Distance.getx_1();


Correct?
Apr 16, 2013 at 5:19pm
It would be, if Distance were an object. But you've ignored the last part of my post.
Pages: 123