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

String Declaration made in Structure definition in header file

首先,我已经让这个程序在一个 cpp 文件下运行得很好,但问题是按每个函数和头文件划分这个程序 - 正如我的实验室讲师在 class 中所说的那样,我试图将结构定义包含到头文件中,但我不断收到各种错误消息。我当前的头文件代码如下:

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

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

请指教

p.s。如果需要,我可以 post 完整的程序。

============================================= ========================= 经过许多对话和人们的帮助后添加的部分 我上传的是我一开始做的原始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 文件都包含一个函数),最后我可以编译程序。

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

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

请指教:(

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

在您的例子中,您定义了 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编译器一样。

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

当有很多文件 .cpp 包含同一个文件时,这是最糟糕的 myheader.h,所有这些文件都将定义一个同名的全局变量。

当您需要在多个 .cpp 文件中使用相同的全局变量时,您可以在 header 文件中包含该变量的定义,但要使用修饰符 extern,例如:

struct World {
    int numCountries;
    Country countries[MAXCOUNTRIES];
    Country fastGrowing[THREE];
};
extern World myWorld;

这将允许所有 .cpp 文件知道变量的存在 myworld。并且变量的真正定义必须在一个.cpp文件中,否则你会在链接阶段出错。

你的头文件有包含保护吗?它看起来像这样:

#ifndef LAB10_H
#define LAB10_H

// everything else goes in here

#endif /* LAB10_H */

"LAB10_H" 可以是您的项目独有的任意文本,因此按照惯例,它通常是大写的文件名,下划线代替点。

#ifndef LAB10_H
#define LAB10_H

#include <string>

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 {
  std::string name; // avoiding "using namespace std;"
  double pop1950;
  double pop1970;
  double pop1990;
  double pop2010;
  double pop2015;
  double growthRate;
};

struct World {
  int numCountries;
  Country countries[MAXCOUNTRIES];
  Country fastGrowing[THREE];
} myWorld;    

#endif /* LAB10_H *

除了从结构末尾删除 "myWorld" 并添加 "extern World myWorld" 的第一个答案外,您还需要使用 [= 在 cpp 文件的 "one" 中创建 myWorld 对象25=].

流程如下:

  1. .h 中的结构定义
  2. .cpp 中的对象声明
  3. extern .h 中的对象