首先,我已經使這個程序在一個cpp文件下工作得很好,但問題是將這個程序除以每個函數和頭文件 - 正如我的實驗室教師告訴在類中,我試圖將結構定義包含到頭文件中,但我不斷收到各種錯誤消息。我現在的頭文件的代碼如下:在頭文件中的結構定義中提出的字符串聲明
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;
現在,它給了我一個錯誤,說了這樣的(我只給他們帶來的一些,你會明白爲什麼):
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);
它在我看來,頭文件不能識別字符串類型,其他使用頭的cpp文件也是如此。所以,我想包括
#include <string>
using namespace std;
在頭文件的開頭,但我得到一個完全不同的錯誤信息,說
/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
我以前做過這種分離的文件,但這次我必須在頭文件中包含結構定義,並且我在這裏沒有任何線索。請指教。實際上,我在Google上搜索時嘗試了很多很多事情,將頭文件分成兩部分,一個頭文件中的結構定義以及另一個文件中的函數,但仍然沒有運氣。
請指教。
p.s.如果需要,我可以發佈完整的程序。
============================================== ======================== 添加部分後多次會議和幫助來自我的朋友 我上傳原始cpp文件的代碼,我在開始時,想知道這可能會讓讀者更容易觀察我的問題。
#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;}
}
=========================================== ============= 第二次編輯/我的頭文件看起來像下面這樣:
#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
正如我在你們中的一些人的評論中提到,與extern World myWorld
在頭文件中,我可以看到頭文件上的結構定義開始工作,但它讓我幾行錯誤說undefined reference to 'myWorld'
。所以,我試着在所有的cpp文件中包含World myWorld
(大多每個cpp文件都包含一個函數),最後我可以編譯這個程序。
然而,程序工作不正常 - 變量存儲不正確,沒有任何計算是正確的。
我的意思是,我沒有想到這個過程會很痛苦,讓我頭痛。
請指教:(
誰告訴你「使用空間std對象;」 [不知道他在說什麼](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)。使用'std :: string'等... –
@SamVarshavchik哦,那將是我,但仍然沒有運氣。我剛剛嘗試過。無論如何感謝:) – mjay
嗯,你並沒有完全想到這一切。有人告訴過你這個指令,或者告訴你,這就是你對此的瞭解。不管是誰,他們都是錯的。正如鏈接中所解釋的,這是一種糟糕的編程習慣。 –