2

I'm trying to use a struct to output two values from a function to then be used in the main. I am using a header file to call the functions to be used. The code is compiling but the values I'm getting are not correct. I don't know if I'm declaring the struct correct in my .h file or where something is getting used incorrectly. At the moment, my .h file looks like:

#ifndef LINEAR_DISPERSION_SOLVER_H #define LINEAR_DISPERSION_SOLVER_H //Function for dispersion relation equation double f (double L, double T, double g, double d); //Function for derivative of linear dispersion relation double df(double L, double T, double g, double d); //Wave parameter struct definition struct wave_parameters { double kn; double w; }; //Linear Dispersion Solver function wave_parameters linear_dispersion(); #endif 

And a portion of my .cpp (not main) looks like:

#include <iostream> #include <cstdlib> #include <cmath> #include <iomanip> using namespace std; struct wave_parameters { double kn; double w; }; float pi = 3.1415927; double f(double L, double T, double g, double d) { return (g*g); } double df(double L, double T, double g, double d) { return (1 + ((g*T*T*d)/(L*L)) } struct wave_parameters linear_dispersion () { .... Deleted code ..... int choice; cout << "Enter the depth of water ---> "; cin >> d; cout << "Enter 1 to solve for wave number or 2 to solve for frequency --> "; cin >> choice; //Calling the wave struct to fill with values struct wave_parameters wave; if (choice == 1) { cout << "Enter the value for period ---> "; cin >> T; .... Deleted code .... wave.kn = k; wave.w = omega; return wave; } 

My main then includes the .h file and calls the .cpp file using:

 struct wave_parameters wave; kn = wave.kn; 

Is this the correct method to outputting multiple variables and then using a header file? As you can see, I've declared my struct twice (once in my .h and once in my .cpp file) but I was doing this as i was getting errors otherwise. Any help is really appreciated!

2 Answers 2

4

Definitely only declare the struct once, ideally in the header. The CPP should then include the header in order to get access to the type.

When using the type, no need to write "struct wave_parameters wave", you don'tneed to put int "struct".

Does main look somethiing like:

#include "LinearDispersionSolver.h" int main(int argc, char** argv) { wave_parameters wave; wave = linearDisperssion(); double kn = wave.kn; } 

Otherwise - the code looks kinda ok.

Sign up to request clarification or add additional context in comments.

1 Comment

Whoops that did it! I was missing the wave = linear_dispersion(); line! Thanks for your help.
1

You don't show that you are including your .h in your .cpp - you should not need to declare the structure twice, so I'd recommend starting by getting that fixed. Include the .h and remove the declaration in the .cpp.

The functions in the header should really have the extern modifier.

Then in your main, I don't see how this code can work

struct wave_parameters wave; kn = wave.kn; 

You seem to be declaring an uninitialised structure and then accessing one of its values. I was expecting to see a call to the linear_dispersion function.

1 Comment

Thanks, this is what the person before you said as well. Those were exactly the problems, it's working now!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.