- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
LINK : ~~~\CSC 161-6ren">
我不熟悉使用头文件之类的东西,上学期我们在一个巨大的(可怕的 :p)文件中做了所有事情......
我是否在做不该做的事情?尝试运行该程序会产生以下结果:
1> LINK : ~~~\CSC 161\Accounting Assignment\Debug\Accounting Assignment.exe not found or not built by the last incremental link; performing full link
1>driver.obj : error LNK2005: "class std::basic_ifstream<char,struct std::char_traits<char> > welcomeFile" (?welcomeFile@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A) already defined in statistics.obj
1>~~~~\CSC 161\Accounting Assignment\Debug\Accounting Assignment.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
统计.h:
#ifndef _STATISTICS_INTERFACE_
#define _STATISTICS_INTERFACE_
...
#include<fstream>
using namespace std;
ifstream welcomeFile; //if I comment this out, it compiles
class Stats
{
...blah...
};
void welcome();
void pause();
void printFile(ifstream &inFile);
#endif
统计.cpp:
#include "statistics.h"
...working functions...
void welcome()
{
system("CLS");
welcomeFile.open("about.txt");
printFile(welcomeFile);
welcomeFile.close();
pause();
}
错误看起来像是某个东西试图被定义两次,但我认为 #ifndef 应该设置它以便它只定义尚未定义的东西?这是我声明 welcomeFile 的唯一地方......
最佳答案
因为你在头文件中定义了对象,违反了一个定义规则。
永远不要在头文件中定义对象!
header 保护防止 header 的内容多次包含在相同 translation unit 在预处理过程中。它们不会阻止内容包含在不同的翻译单元中。当您将此头文件包含在不同的翻译单元中时,这些单元中的每一个都会有此对象的定义。
编译器分别编译每个翻译单元以生成一个单独的目标文件 (.o),每个 .o 文件都将具有此目标定义的拷贝。当链接器在生成 .exe
时尝试链接到对象/符号名称时,它会发现同一对象/符号的多个定义,从而导致不知道要链接到哪个。为避免此问题,标准定义了一个称为 One defintion rule(ODR) 的规则。 ,禁止同一实体的多个定义。
如您所见,在头文件中包含对象定义并在多个翻译单元中包含该头文件违反了 ODR。
如果你想使用一个全局对象,你需要将它声明为extern
,并在一个且唯一的源文件中定义它。
关于c++ - 为什么我在声明 ifstream 时收到 "Already defined"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14516123/
最近我将 sonarqube 从 4.0 升级到 4.3.3。升级后当我尝试运行 maven 构建时出现错误: Failed to execute goal org.codehaus.mojo:son
在下面的类中: class TestClass { public: int staic testMember; }; int TestClass::testMember; 我遇到了以下编译器
我以前也遇到过这个错误,但这次有点不同。 当我向 Forge 推送更新时,Laravel Forge 说它部署失败,但它部署得很好。由于此错误,它只是没有对数据库结构进行任何更改: [Doctrine
此错误显然源于 xlsxwriter。我不确定它来自我的代码的哪一行,因为每次我尝试调试时,我的编辑器 Visual Studio 2019 都会崩溃。但是,在使用 VPN 和远程桌面连接时,我在笔记
我自己尝试将magento版本1.9.1升级到1.9.2.2 引用链接:http://www.hostknox.com/tutorials/magento/upgrade-with-connect-m
这是我的迁移表 increments('id'); $table->integer('role_id')->unsigned(); $table
注册表要求提供用户名和电子邮件地址。数据通过验证后,将添加到 accounts 表中。它存储用户名、密码和电子邮件地址等数据。如果用户名已被占用,用户将收到通知,并且不会将任何数据添加到表中。它被视为
规范: Laravel 版本:5.4PHP版本:7.0.9 Composer 版本 1.9.0XAMP 描述: 在 Connection.php 第 647 行: SQLSTATE[42S01]: 基
当我迁移一个表时,我看到了这个错误, SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'payments' already
问题现场: 原因: HttpServletRequest 的 getInputStream() 和 getReader() 都只能读取一次。 因为 我们使用@RequestBody 注解,读取body
mysql新手。尝试创建我在本教程中看到的存储过程:http://www.microshell.com/database/mysql/emulating-nextval-function-to-get
我只是尝试使用 TortoiseSVN 添加和提交大约 25,000 个文件到 VisualSVN。 提交对话框被锁定(我猜是预料之中的),但是在线程被阻塞一段时间后,我杀死了 Tortoise,希望
我有一个 scala 项目,当从命令行使用 SBT 时,它可以正常编译、运行和测试。但是,在intellij中构建项目时,项目中的每个类的事件日志中似乎都有此错误,导致构建失败: SendComman
当尝试将更改提交到存储库(我是唯一的用户)时,我收到错误 Path '/trunk/TemplatesLibrary/constraints/templates/TP145210GB01_Person
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin
我正在使用 Metro 2.0.1 作为安全连接到 WCF 服务的客户端应用程序。在我的单元测试中一切正常,但是当我在 Java 应用程序中调用该方法时,出现以下异常: Exception in th
我正在使用 azure-documentdb java SDK为了创建和使用“用户定义函数 (UDF)” 因此来自 official documentation我终于找到了如何创建 UDF 的方法(使
我有一个 JButton,按下时会启动倒计时器。当我按下按钮时,它开始,当我再次按下它时(按钮会说“停止”),它停止。但是,当我再次按下它再次开始计时时,我收到一条错误消息: Exception in
通过设置 innerHTML 插入的脚本,以及调用 element.cloneNode(true) 等其他一些情况将生成具有内部“已启动”标志集的脚本为真。 The first is a flag i
我是一名优秀的程序员,十分优秀!