首先,我已经使该程序在一个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
我之前已经完成了将文件分离的操作,但是这次我必须在头文件中包含结构定义,而我却毫无头绪。请指教。实际上,我在进行谷歌搜索时尝试了很多事情,将头文件分为两个,一个头文件中的结构定义,另一个文件中的功能,但是还是没有运气。
请指教。
ps。如果需要,我可以发布完整程序。
================================================== ====================
经过多次对话并从中受益后添加了部分
我正在上传最初创建的原始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[]);
万一
正如我在某些人的评论中提到的那样,在头文件中使用
extern World myWorld
时,我可以看到头文件上的结构定义开始起作用,但是却出现了几行说undefined reference to 'myWorld'
的错误。因此,我尝试在所有cpp文件中都包含World myWorld
(大多数每个cpp文件都包含一个函数),最后我可以编译该程序了。但是,程序无法正常运行-变量未正确存储,并且所有计算均不正确。
我的意思是,我没想到这个过程会如此痛苦,让我头疼不已。
请指教 :(
最佳答案
正如编译器指出的那样,问题是变量多重声明。在头文件中,您只需要定义类型和宏,而不必定义变量(外部声明除外)。
在您的情况下,您定义了myWorld
类型的struct World
全局变量。
struct World {
int numCountries;
Country countries[MAXCOUNTRIES];
Country fastGrowing[THREE];
} myWorld;
我想在您的实现文件(.cpp)中,您会看到以下内容:
#include "myheader.h"
...
World myWorld;
发生的情况是,cpp预处理程序读取每个cpp文件,并在实际编译之前用该文件的完整内容替换文本
#include "myheader.h"
。这意味着cpp编译器对于每个.cpp都会看到类似以下内容的内容:struct World {
int numCountries;
Country countries[MAXCOUNTRIES];
Country fastGrowing[THREE];
} myWorld;
...
World myWorld;
现在,您也可以看到同一变量的两个声明,就像de cpp编译器一样。
您需要删除这些声明之一,通常是标头的声明。
当有很多文件.cpp包含相同的文件
myheader.h
时,这是最糟糕的,它们都将定义一个具有相同名称的全局变量。当需要在多个.cpp文件中使用相同的全局变量时,可以在头文件中包含该变量的定义,但要使用修饰符
extern
,例如:struct World {
int numCountries;
Country countries[MAXCOUNTRIES];
Country fastGrowing[THREE];
};
extern World myWorld;
这将使所有.cpp文件都知道变量
myworld
的存在。变量的实际定义必须仅在一个.cpp文件中,否则在链接阶段会出现错误。关于c++ - 在头文件的结构定义中进行的字符串声明,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40797034/