简体   繁体   English

将代码分为.h和.cpp文件时的链接器错误

[英]Linker Error when separating code into .h and .cpp files

I have an implementation for printing out enum values in c++ 我有一个用于在C ++中打印出枚举值的实现

If I put all the code in a .h file, everything works nicely. 如果我将所有代码都放在.h文件中,则一切正常。 If I separate out the function implementation into .cpp files, I get a linker error. 如果将函数实现分离为.cpp文件,则会收到链接器错误。

Here is my main file 这是我的主文件

#include <iostream>
#include <vector>
#include "Day.h"
using namespace std;

int main(){
    initializeDayNames();
    Day a = Clubs;
    cout << a;
}

Here is the .h file 这是.h文件

#ifndef __Day__
#define __Day__

#include <iostream>
#include <vector>
#include <string>
using namespace std;

enum Day {Clubs, Hearts, Diamonds, Spades} ;

vector<string> DayNames = vector<string>();

ostream & operator<<(ostream & out, Day cs);

void initializeDayNames();

#endif

and the .cpp file 和.cpp文件

#include <iostream>
#include "Day.h"
#include<string>
#include<vector>
using namespace std;

void initializeDayNames(){
    DayNames.push_back("Clubs");
    DayNames.push_back("Hearts");
    DayNames.push_back("Diamonds");
    DayNames.push_back("Spades");
}


ostream & operator<<(ostream & out, Day cs){
    out << DayNames[cs];
    return out;
}

What am I doing wrong here 我在这里做错了什么

The specific error is 具体错误是

Error   1   error LNK2005: "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > DayNames" (?DayNames@@3V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@A) already defined in Day.obj main.obj

and

Error   2 fatal error LNK1169: one or more multiply defined symbols found   

You are initializing two copies of 您正在初始化的两个副本

vector<string> DayNames = vector<string>();

because you included the header twice. 因为您两次包含了标题。

You should replace it with 您应该将其替换为

extern vector<string> DayNames;

in the h file and 在h文件中

vector<string> DayNames = vector<string>();

in the cpp file. 在cpp文件中。

Also you seem to have two copies of 你似乎也有两个副本

ostream & operator<<(ostream & out, Day cs);

The reason your header guards isn't helping this case is that when you include the headers, you basically duplicate the definitions in all your files that you have included the header. 标头防护无法解决这种情况的原因是,当您包含标头时,您基本上会在包含标头的所有文件中重复定义。

In C, when you declare a variable, you basically instantiate it/allocate static space for it. 在C语言中,声明变量时,基本上是实例化它/为其分配静态空间。 When you put the variable declaration in the header, what you are effectively doing is allocating static storage space for the variable in two different objects, giving you your error. 当您将变量声明放在标头中时,实际上是在为两个不同的对象中的变量分配静态存储空间,这给您带来了错误。

您在DayNames声明中缺少extern

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM