Passing structures to a function [need assistance please!! D:]

I'm really lost on how to pass my structure to a function.

Basically I have the listed structures, and I need to pass them to a function for conversion. Can someone please help me?

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>		// contains cout/cin functions
#include <cmath>		// contains trig functions
using namespace std;
//______________________________________________________________________________
// Prototypes:
    //void convert_to_radians(char, int, int, int, double&, double&);
	void convert_to_radians(point);
//______________________________________________________________________________
// Main function:
int main() {

	const double RADIUS = 3958.89;

	struct coordinate {
		char direction;
		double degrees;
		double minutes;
		double seconds;
	} ;

	struct point {
		coordinate latitude;
		coordinate longitude;
		double alpha;
		double beta;
	} first, second;

	double theta;

	cout << "This program will calculate the distance between two coordinates on Earth." << endl
		<< "Please enter your coordinates in the following format:" << endl
		<< "	(N, S, E, or W) (Degrees) (Minutes) (Seconds)" << endl
		<< "	i.e. N 95 18 35" << endl
		<< "Enter the latitude of the first coordinate:" << endl;
	cin >> first.latitude.direction >> first.latitude.degrees >> first.latitude.minutes >> first.latitude.seconds;
	cout << "Enter the longitude of the first coordinate:" << endl;
	cin >> first.longitude.direction >> first.longitude.degrees >> first.longitude.minutes >> first.longitude.seconds;
	cout << "Enter the latitude of the second coordinate:" << endl;
	cin >> second.latitude.direction >> second.latitude.degrees >> second.latitude.minutes >> second.latitude.seconds;
	cout << "Enter the longitude of the second coordinate:" << endl;
	cin >> second.longitude.direction >> second.longitude.degrees >> second.longitude.minutes >> second.longitude.seconds;

	convert_to_radians(point);
	system("PAUSE");
    return 0;
}
//______________________________________________________________________________
// Function definitions:
    
//void calc_maximum(int Val1, int Val2, int Val3, int Val4, int& Max1, int& Max2) {
void convert_to_radians(point) {

	double degrees=0;

	cout << degrees << endl;

	degrees += first.latitude.degrees;

	cout degrees << endl;
	
}


I get the following errors:

1>------ Build started: Project: project1, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(7): error C2065: 'point' : undeclared identifier
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(7): error C2182: 'convert_to_radians' : illegal use of type 'void'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(43): error C2275: 'main::point' : illegal use of this type as an expression
1>          c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(21) : see declaration of 'main::point'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2065: 'point' : undeclared identifier
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2448: 'convert_to_radians' : function-style initializer appears to be a function definition
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I suppose I'm really just not sure what is going on exactly. I read the tutorial and it seems to work well for their case, but I'm not sure how to apply that to my program. If I move my structures outside the main function, I get a lot more errors.

Thanks for the assistance in advance!
Last edited on
In your implementation you have to name the struct. For example:
void convert_to_radians(point test){
I think that should work.
depending on what your struct contains you might want to pass it by reference rather than by value too
depending on what your struct contains you might want to pass it by reference rather than by value too

Why? The only reason I can think of is speed, and it doesn't look that is a factor here. What is your reason?
What exactly do you mean by pass it by reference instead of value?
why would you choose the slower option in any scenario? Computers being faster nowadays is no reason for not considering the best solution rather than just anything that works. There's no downside to passing by reference but there is an upside if the structure is large.
Last edited on
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>		// contains cout/cin functions
#include <cmath>		// contains trig functions
using namespace std;
//______________________________________________________________________________
// Prototypes:
    //void convert_to_radians(char, int, int, int, double&, double&);
	void convert_to_radians(point convert);
//______________________________________________________________________________
// Main function:
int main() {

	const double RADIUS = 3958.89;

	struct coordinate {
		char direction;
		double degrees;
		double minutes;
		double seconds;
	} ;

	struct point {
		coordinate latitude;
		coordinate longitude;
		double alpha;
		double beta;
	} first, second;

	double theta;

	cout << "This program will calculate the distance between two coordinates on Earth." << endl
		<< "Please enter your coordinates in the following format:" << endl
		<< "	(N, S, E, or W) (Degrees) (Minutes) (Seconds)" << endl
		<< "	i.e. N 95 18 35" << endl
		<< "Enter the latitude of the first coordinate:" << endl;
	cin >> first.latitude.direction >> first.latitude.degrees >> first.latitude.minutes >> first.latitude.seconds;
	cout << "Enter the longitude of the first coordinate:" << endl;
	cin >> first.longitude.direction >> first.longitude.degrees >> first.longitude.minutes >> first.longitude.seconds;
	cout << "Enter the latitude of the second coordinate:" << endl;
	cin >> second.latitude.direction >> second.latitude.degrees >> second.latitude.minutes >> second.latitude.seconds;
	cout << "Enter the longitude of the second coordinate:" << endl;
	cin >> second.longitude.direction >> second.longitude.degrees >> second.longitude.minutes >> second.longitude.seconds;

	convert_to_radians(point convert);
	system("PAUSE");
    return 0;
}
//______________________________________________________________________________
// Function definitions:
    
//void calc_maximum(int Val1, int Val2, int Val3, int Val4, int& Max1, int& Max2) {
void convert_to_radians(point convert) {

	double degrees=0;

	cout << degrees << endl;

	degrees += first.latitude.degrees;

	cout degrees << endl;
	
}
//______________________________________________________________________________ 


errors:

1>------ Build started: Project: project1, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(7): error C2065: 'point' : undeclared identifier
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(7): error C2146: syntax error : missing ')' before identifier 'convert'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(7): error C2182: 'convert_to_radians' : illegal use of type 'void'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(7): error C2059: syntax error : ')'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(43): error C2275: 'main::point' : illegal use of this type as an expression
1>          c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(21) : see declaration of 'main::point'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(43): error C2146: syntax error : missing ')' before identifier 'convert'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(43): error C2059: syntax error : ')'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2065: 'point' : undeclared identifier
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2146: syntax error : missing ')' before identifier 'convert'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2182: 'convert_to_radians' : illegal use of type 'void'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2374: 'convert_to_radians' : redefinition; multiple initialization
1>          c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(7) : see declaration of 'convert_to_radians'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2059: syntax error : ')'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2143: syntax error : missing ';' before '{'
1>c:\users\matt\dropbox\egr125\project\project1\project1\main.cpp(51): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Don't declare the struct inside main if you want to use it elsewhere. Struct declarations have the same rules as any other declaration
I moved it outside of my main function. I think the issue I'm having is with syntax, and I'm not too familiar with how pointers work so that my function will use a pointer to reference the memory address of the original structure? I think?
You don't need to use pointers. You can pass it by (const) reference if you want to save from copying
Topic archived. No new replies allowed.