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

时间:2016-11-25 02:52:20

标签: c++ struct header-files

首先,我已经让这个程序在一个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文件包含一个函数),最后我可以编译该程序。

但是,程序运行不正常 - 变量未正确存储,并且没有一个计算是正确的。

我的意思是,我没想到这个过程会非常痛苦,给我带来了许多令人头痛的问题。

请告知:(

3 个答案:

答案 0 :(得分:1)

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

在您的情况下,您定义了{strong> var myObject = new MyObject {Grant_Type = "TypeA", Username = "Hello", Password = "World"}; var request = new HttpRequestMessage(HttpMethod.Post, "/path/to/post/to") { Content = myObject.ToFormUrlEncodedContent() }; var client = new HttpClient {BaseAddress = new Uri("http://www.mywebsite.com")}; var response = await client.SendAsync(request); myWorld类型的全局变量。

struct World

我想在你的实现文件(.cpp)中有类似的东西:

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

发生的事情是cpp预处理器读取每个cpp文件,并在实际编译之前将文本#include "myheader.h" ... World myWorld; 替换为该文件的完整内容。这意味着cpp编译器会为每个.cpp看到类似的内容:

#include "myheader.h"

现在你可以看到同一个变量的两个声明,就像de cpp compiler。

您需要删除其中一个声明,通常是标题的声明。

当包含相同文件{。}}的许多文件.cpp时,这是最糟糕的,所有这些文件都将定义一个具有相同名称的全局变量。

当您需要在多个.cpp文件中使用相同的全局变量时,可以在头文件中包含该变量的定义,但使用修饰符struct World { int numCountries; Country countries[MAXCOUNTRIES]; Country fastGrowing[THREE]; } myWorld; ... World myWorld; ,如:

myheader.h

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

答案 1 :(得分:0)

你的头文件中是否有包含警戒?它看起来像这样:

#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 *

答案 2 :(得分:0)

除了删除&#34; myWorld&#34;的第一个答案。从结构的末尾添加&#34; extern World myWorld&#34;,你需要在&#34; one&#34;中创建myWorld对象。您的cpp文件包含&#34; World myWorld&#34;。

以下是流程:

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