gpt4 book ai didi

c++ - 头文件中结构定义中的字符串声明

转载 作者:行者123 更新时间:2023-11-28 05:24:40 26 4
gpt4 key购买 nike

首先,我已经让这个程序在一个 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

我之前已经完成了这个分离文件,但这次我必须在头文件中包含结构定义,我毫 headless 绪地卡在这里。请指教。我实际上在谷歌搜索时尝试了很多很多东西,将头文件分成两个,一个头文件中的结构定义,另一个文件中的函数,但仍然没有运气。

请指教。

附注如果需要,我可以发布完整的程序。

============================================= =======================经过许多对话和人们的帮助后添加的部分我正在上传我一开始制作的原始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 。所以,我尝试在所有 cpp 文件中包含 World myWorld(大多数每个 cpp 文件都包含一个函数),最后我可以编译程序。

但是,程序运行不正常 - 变量存储不正确,计算也不正确。

我的意思是,我没想到这个过程会如此痛苦,让我头疼不已。

请指教:(

最佳答案

正如编译器指出的那样,问题是变量多重声明。在头文件中,您只需定义类型和宏,而不是变量(外部声明 除外)。

在您的例子中,您定义了 struct World 类型的 myWorld 全局变量。

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 编译器一样。

您需要删除这些声明之一,通常是 header 的声明。

当有许多文件 .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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com