Hello! I'm having some trouble getting my program to compile.
The purpose of this program is:
1.) To generate hour/temperature values and store them in a struct.
2.) To write the values of these hour/temperature pairs to a file.
3.) Read the hour/temperature pairs from the same file.
4.) Calculate and display the mean value of the temperature values.
The code compiles fine when everything is one file. I'm running into trouble when I split the code up into several files. Using gcc, I get a compiler error message: "duplicate symbols for architecture_x86_64". According to what I've read on Stack Overflow, this means that something is being double-included that shouldn't be.
I'd like to understand how to resolve this linker issue and avoid future linker issues.
Please keep in mind that this code is hacky and for learning about fstream objects. I have "using namespace std" for convenience's sake; it gets tiring to write std:: over and over. The point of this program is to learn about file input/output and fstream objects. I'd like to learn more about the linker/compiler process as it pertains to headers/includes/source files.
I'd appreciate any comments/suggestions/feedback that would make the program faster or the code more debuggable/elegant.
ofstream ost{fileName};
if (!ost)
error("can't open output file");
vector<Reading> temps;
int hour = 0;
double temperature = 0;
//set Random seed
srand(time(NULL));
//Populate vector with temperature readings
for (int i = 0; i < 50; ++i){
hour = i;
temperature = (rand() % 100 + 1); //rand generator for doubles
temps.push_back(Reading{hour, temperature});
}
//Read into file raw_temps.txt
for (int i = 0; i < temps.size(); ++i)
ost << '(' << temps[i].hour << ", " << temps[i].temperature << ")\n";
while (ist >> ch1 >> hour >> ch2 >> temperature >> ch3){
temps.push_back(Reading{hour,temperature});
}
int sum = 0; //calculate mean temperature
for (int i = 0; i < temps.size(); ++i){
sum += temps[i].temperature;
temps2[i] = temps[i].temperature;
}
mean = sum / temps.size();
Ok cool, but if I move the function to one of the source files and use it in another one won't I have to include a .cpp file? And isn't that bad practice? Thanks for the help!
OHHHHHH. I figured it out. The problem was that I defined void error(string s) in my .h file instead of declaring a function prototype. As you correctly stated, the function definition needs to be in just one .cpp file to avoid double-include issues. Thank you!
So, solution:
1.) Make void error(string s) a function prototype in the .h file.
2.) Place the implementation in just one .cpp file