First of all, I've made this program working under one cpp file just fine, but the problem is to divide this program by each functions and header file - As my lab instructor told in class, I tried to include struct definition into header file, but I keep getting various error messages. My current code for header file is the following:
extern void threeLargest(Country countries[], Country fastGrowing[]);
extern void readData(Country countries[]);
extern void negGrowth(Country countries[]);
const int MAXCOUNTRIES = 229;
const int THREE = 3;
struct Country {
string name;
double pop1950;
double pop1970;
double pop1990;
double pop2010;
double pop2015;
double growthRate;
};
struct World {
int numCountries;
Country countries[MAXCOUNTRIES];
Country fastGrowing[THREE];
} myWorld;
For now, it gives me an error says the following ( I only brought some of them and you will see why):
In file included from lab10_0.cpp:1:0:
lab10.h:6:2: error: ‘string’ does not name a type
string name;
^
lab10_0.cpp: In function ‘void readData(Country*)’:
lab10_0.cpp:17:37: error: ‘struct Country’ has no member named ‘name’
getline(ifstr,myWorld.countries[i].name);
It seemed to me that the header file is not recognizing the string type, so does other cpp files using the header. So, I tried including
#include <string>
using namespace std;
at the beginning of the header file, but I get a whole different error message, saying
/tmp/cclU6znx.o:(.bss+0x0): multiple definition of `myWorld'
/tmp/ccQ69Fio.o:(.bss+0x0): first defined here
/tmp/cckXoPSG.o:(.bss+0x0): multiple definition of `myWorld'
/tmp/ccQ69Fio.o:(.bss+0x0): first defined here
/tmp/cctaCWNQ.o:(.bss+0x0): multiple definition of `myWorld'
/tmp/ccQ69Fio.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
I've done this separating files previously, but this time I have to include structure definition in header file and I got stuck here without a clue. Please advise. I actually tried many, many things while googling, separating header files into two, structure definition in one header file, and the functions in the other file, but still no luck.
Please advise.
p.s. I can post full program if needed.
====================================================================== ADDED SECTION AFTER MANY CONVERSATIONS AND HELPS FROM FOLKS I'm uploading code of the original cpp file I made at the beginning, wondering this might be easier for readers to observe what is my problem.
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
const int MAXCOUNTRIES = 229;
const int THREE = 3;
struct Country{
string name;
double pop1950;
double pop1970;
double pop1990;
double pop2010;
double pop2015;
double growthRate;
};
struct World{
int numCountries;
Country countries[MAXCOUNTRIES];
Country fastGrowing[THREE];
} myWorld;
void threeLargest(Country countries[], Country fastGrowing[]);
void readData(Country countries[]);
void negGrowth(Country countries[]);
int main() {
readData(myWorld.countries);
threeLargest(myWorld.countries,myWorld.fastGrowing);
negGrowth(myWorld.countries);
return 0;
}
void readData(Country countries[])
{
fstream ifstr;
ifstr.open("population.csv");
for (int i=0; !(ifstr.eof()) && i < MAXCOUNTRIES; i++) {
ifstr >> myWorld.countries[i].pop1950 >> myWorld.countries[i].pop1970
>> myWorld.countries[i].pop1990 >> myWorld.countries[i].pop2010
>> myWorld.countries[i].pop2015;
getline(ifstr,myWorld.countries[i].name);
myWorld.countries[i].growthRate = ((myWorld.countries[i].pop2015-myWorld.countries[i].pop1950)/myWorld.countries[i].pop1950)*100;}
ifstr.close();
}
void threeLargest(Country countries[], Country fastGrowing[])
{
myWorld.fastGrowing[THREE].growthRate = { };
for (int i=0; i < MAXCOUNTRIES; i++) {
if (myWorld.countries[i].growthRate > myWorld.fastGrowing[0].growthRate) {
myWorld.fastGrowing[2].growthRate = myWorld.fastGrowing[1].growthRate;
myWorld.fastGrowing[2].name = myWorld.fastGrowing[1].name;
myWorld.fastGrowing[1].growthRate = myWorld.fastGrowing[0].growthRate;
myWorld.fastGrowing[1].name = myWorld.fastGrowing[0].name;
myWorld.fastGrowing[0].growthRate = myWorld.countries[i].growthRate;
myWorld.fastGrowing[0].name = myWorld.countries[i].name;}
else if (myWorld.countries[i].growthRate > myWorld.fastGrowing[1].growthRate) {
myWorld.fastGrowing[2].growthRate = myWorld.fastGrowing[1].growthRate;
myWorld.fastGrowing[2].name = myWorld.fastGrowing[1].name;
myWorld.fastGrowing[1].growthRate = myWorld.countries[i].growthRate;
myWorld.fastGrowing[1].name = myWorld.countries[i].name;}
else if (myWorld.countries[i].growthRate > myWorld.fastGrowing[2].growthRate) {
myWorld.fastGrowing[2].growthRate = myWorld.countries[i].growthRate;
myWorld.fastGrowing[2].name = myWorld.countries[i].name;}
}
cout << "The fastest growing country is " << myWorld.fastGrowing[0].name << ", which grew by "
<< fixed << setprecision(2) << myWorld.fastGrowing[0].growthRate << "% between 1950 and 2015.\n"
<< "The 2nd fastest growing country is " << myWorld.fastGrowing[1].name << " which grew by "
<< fixed << setprecision(2) << myWorld.fastGrowing[1].growthRate << "% between 1950 and 2015.\n"
<< "The 3rd fastest growing country is " << myWorld.fastGrowing[2].name << " which grew by "
<< fixed << setprecision(2) << myWorld.fastGrowing[2].growthRate << "% between 1950 and 2015.\n";
}
void negGrowth(Country countries[])
{
cout << "The following countries' population shrunk between 1950 and 2015:" << endl;
for (int i=0; i < MAXCOUNTRIES; i++) {
if (myWorld.countries[i].growthRate < 0)
cout << myWorld.countries[i].name << " shrunk by " << fixed << setprecision(2) << myWorld.countries[i].growthRate << "%." << endl;}
}
======================================================== 2ND TIME EDITING / MY HEADER FILE LOOKS LIKE THE FOLLOWING:
#ifndef LAB10_H
#define LAB10_H
#include <string>
const int MAXCOUNTRIES = 229;
const int THREE = 3;
struct Country {
std::string name;
double pop1950;
double pop1970;
double pop1990;
double pop2010;
double pop2015;
double growthRate;
};
struct World {
int numCountries;
Country countries[MAXCOUNTRIES];
Country fastGrowing[THREE];
};
extern World myWorld;
extern void threeLargest(Country countries[], Country fastGrowing[]);
extern void readData(Country countries[]);
extern void negGrowth(Country countries[]);
#endif
As I mentioned in some of you guy's comment, with extern World myWorld
in header file, I could see the structure definition on header file started working, but it gets me few lines of errors saying undefined reference to 'myWorld'
. So, I tried including World myWorld
in all cpp files (mostly each cpp file contains a function) and finally I could compile the program.
HOWEVER, the program is not working correctly - variables are not stored correctly, and none of the calculation is right.
I mean, I didn't expect this process would be this much painful, giving me tons of headaches.
Please advise :(