Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 924951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:26:40+00:00 2026-05-15T19:26:40+00:00

I have an implementation for printing out enum values in c++ If I put

  • 0

I have an implementation for printing out enum values in c++

If I put all the code in a .h file, everything works nicely. If I separate out the function implementation into .cpp files, I get a linker error.

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

#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

#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   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:26:41+00:00Added an answer on May 15, 2026 at 7:26 pm

    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

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

    in the cpp file.

    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. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an implementation of lazyDataModel following this tutorial http://uaihebert.com/?p=1120 My code its a
For example: I have an implementation (say doSomething()) in Base class. I want all
I know that it's OK for a pure virtual function to have an implementation.
I have a c++ implementation file (my.cpp) that indirectly includes a header file (b.h)
I'm looking for some help with a Google Maps implementation. I have to code
I downloaded the JSON2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js and it does not have implementation for JSON2.stringify()
I have an implementation of a DefaultTableCellRenderer. When someone selects a row in the
I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
I have this implementation with fusion table layers where I try to use the
I don't know either my implementation have bugs or StringBuilder is going wrong. From

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.