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!
.... 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.
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
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'
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?
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?)
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.
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.
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
}
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'
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.
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!