- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我已经让这个程序在一个 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/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!