-1

I am new to c++ and am writing a simple role playing mining game. I have only been coding for a couple weeks, but I've encountered an error that I've been working on for several days and have still come up with nothing. I've even considered just scrapping my program and trying it again. I have 4 files: Game.cpp, header.h, nuggetsFound.cpp, and days.cpp. I'm trying to have a global variable that I can change throughout files, however, even though I haven't even been redefining or anything, I continue to encounter this crazy error that says "float percentOfOres" (?percentOfOres@@3MA) already defined in Days.obj. Several of these pop up for all my variables and files, and I'm going insane. I'm only 13, so I can't always get all of the help I need, so I would really appreciate it if you guys can help me. Thanks! (I excluded nuggetsFound.cpp because it's basically the same situation as days.cpp. There's also a lot more code in game.cpp but I just included what I thought was relevant. These are the two files

game.cpp
#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include "Header.h"
using namespace std;
float netWorth{ 0.0 };
float salary{ 0.0 };
float percentOfOres{ 0.0 };

int main()
{
    cout << "Welcome to Miner Man!\n";
    cout << "Press any key and press enter to continue";days.cpp

#include "game.cpp"


using namespace std;

void days()
{
    for (int iii{ 1 }; iii <= 5; iii++)
    {
        cout << "Net Worth: " << netWorth << "\tSalary: " << salary << 
"\tPercent of Ores: " << percentOfOres;
        cout << "\n";
        cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n";
        cout << "CCCCCCCCCCCCC\n";
        cout << "CCCCCCCC\n";
        cout << "CCCC            ///////\n";
        cout << "CCC             |O  O |\n";
        cout << "CC              | __  |\n";
        cout << "C              asssssssa\n";
        cout << "C            a  sssssss  a\n";
        cout << "C           a   sssssss   a\n";
        cout << "           a    sssssss   a\n";
        cout << "C         h     sssssss   a\n";
        cout << "C p      h      sssssss    \n";
        cout << "C  p    h       l     l\n";
        cout << "CC  p  h      p l     l\n";
        cout << "CCC   p p p p___l     l___\n";
        cout << "CCCCC       L___l     l___l\n";
        cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n";
        Sleep(500);
        system("cls");

        cout << "Net Worth: " << netWorth << "\tSalary: " << salary << 
"\tPercent of Ores: " << percentOfOres;
        cout << "\n";
        cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n";
        cout << "CCCCCCCCCCCCC\n";
        cout << "CCCCCCCC\n";
        cout << "CCCC  p         ///////\n";
        cout << "CCC  p          |O  O |\n";
        cout << "CC  p           | __  |\n";
        cout << "C   phhhhhhaaaa sssssssa\n";
        cout << "C   p           sssssss  a\n";
        cout << "C    p          sssssss   a\n";
        cout << "C     p         sssssss   a\n";
        cout << "C               sssssss   a\n";
        cout << "C               sssssss    \n";
        cout << "C               l     l\n";
        cout << "CC              l     l\n";
        cout << "CCC          ___l     l___\n";
        cout << "CCCCC       L___l     l___l\n";
        cout << "CCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n";
        Sleep(500);
        system("cls");
    }
}

#pragma once
#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <cstdlib>
#include <ctime>
#include "data.hpp"
using namespace std;



void days();
void endOfDayNugs();


void endOfDayNugs()
{
    srand((unsigned)time(0));
    int silverFoundToday = rand() % 25;

    srand((unsigned)time(0));
    int goldFoundToday = rand() % 10;

    srand((unsigned)time(0));
    int diamondsFoundToday = rand() % 5;

    if (silverFoundToday == 0)
    {
        cout << "Sorry, you found no Silver today\n";
        Sleep(3000);
        system("cls");
    }
    else if (silverFoundToday > 0)
    {
        for (int sss{ 1 }; sss < 3; sss++)
        {
            cout << "You found " << silverFoundToday << " silver!\n";
            cout << "\n/SSSSSS\\\n";
            Sleep(500);
            system("cls");
        }

        if (percentOfOres == 0)
        {
            cout << "You don't make any money of your silver yet, but                 continue working towards that promotion!";
            Sleep(5000);
            system("cls");
        }
        else if (percentOfOres > 0)
        {
            cout << "You made " << percentOfOres * silverFoundToday <<         "dollers off of your silver\n";
            netWorth += percentOfOres * silverFoundToday;
            Sleep(5000);
            system("cls");
        }
    }
    if (goldFoundToday == 0)
    {
        cout << "Sorry, you found no gold today\n";
        Sleep(3000);
        system("cls");
    }
    else if (goldFoundToday > 0)
    {
        for (int ggg{ 1 }; ggg < 3; ggg++)
        {
        cout << "You found " << goldFoundToday << " gold!\n";
        cout << "\n/GGGGGG\\\n";
        Sleep(500);
        system("cls");
    }

    if (percentOfOres == 0)
    {
        cout << "You don't make any money of your gold yet, but continue working towards that promotion!";
        Sleep(5000);
        system("cls");
    }
    else if (percentOfOres > 0)
    {
        cout << "You made " << percentOfOres * goldFoundToday << "dollers off of your gold\n";
        netWorth += percentOfOres * goldFoundToday;
        Sleep(5000);
        system("cls");
    }
}
if (diamondsFoundToday == 0)
{
    cout << "Sorry, you found no diamonds today\n";
    Sleep(3000);
    system("cls");
}
else if (diamondsFoundToday > 0)
{
    for (int ddd{ 1 }; ddd < 3; ddd++)
    {
        cout << "You found " << diamondsFoundToday << " diamonds!\n";
        cout << "\n/DDDDDD\\\n";
        Sleep(500);
        system("cls");
    }

    if (percentOfOres == 0)
    {
        cout << "You don't make any money of your diamonds yet, but continue working towards that promotion!";
        Sleep(5000);
        system("cls");
    }
    else if (percentOfOres > 0)
    {
        cout << "You made " << percentOfOres * diamondsFoundToday << "dollers off of your diamonds\n";
        netWorth += percentOfOres * diamondsFoundToday;
        Sleep(5000);
        system("cls");
    }
}

}

john papp
  • 13
  • 3
  • You haven't given any code, so we can't identify the exact problem, but my guess is that you have defined a variable in a header file. This causes the variable to defined in each compilation unit that the header is included into, and during linking this will cause an error. You must instead declare the variable `extern` in the header and then define it in _one_ compilation unit. For example, in `header.h` you might have `extern float percentOfOres;` and then in one of the .cpp files you would have `float percentOfOres;`. – cdhowie Apr 21 '20 at 17:47
  • If you haven't seen it already, there is a list of [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that you may find helpful. – Eljay Apr 21 '20 at 17:49
  • @cdhowie I’ve tried everything including that although I might have done it wrong. If you could hit me up I could send you some code and it would be super appreciated – john papp Apr 21 '20 at 17:54
  • @johnpapp Paste the relevant code into your question. – cdhowie Apr 21 '20 at 17:58
  • @cdhowie I updated my post – john papp Apr 21 '20 at 18:18
  • @johnpapp What's in `header.h` and `nuggetsFound.cpp`? Also, `#include "game.cpp"` is very wrong. You don't include compilation units in other compilation units. – cdhowie Apr 21 '20 at 18:21
  • @cdhowie frankly I have no idea what i'm doing. Like I said, i'm new to this. Thanks for your help though. – john papp Apr 21 '20 at 18:32
  • Basically you need to never include a .cpp file in another. Instead, put the common stuff in a header. However, you can't _define_ anything in the header, only _declare_. Note that `float x;` defines `x` and makes it global. `extern float x;` declares that `x` exists _somewhere else_ but does not define it. `static float x;` defines `x` but makes it local to the current compilation unit. – cdhowie Apr 21 '20 at 18:54
  • Thank you I’ll try that – john papp Apr 21 '20 at 18:56

1 Answers1

1
  • Any global things need to be declared but not defined in your header file.
  • Include the header file into your .cpp files.
  • Do not include .cpp files into other .cpp files.
  • Declare each thing in the header file in one of your .cpp files.

A declaration says "this thing exists somewhere" and a definition says "make this thing exist." C++ has the one-definition rule, which states that only one definition for a particular thing may exist in the whole program.1

If you put a definition in a header file and include it into two .cpp files, then the definition exists twice in the program (once in each .cpp file) and this is the cause of your error.

Here are some examples of declarations:

void foo();       // function declaration
extern float bar; // variable declaration

Here are some examples of definitions:

void foo() { }       // function definition
float bar;           // global variable definition
static float widget; // local variable definition

So, in your .h file you should declare your variables but not define them:

extern float netWorth;
extern float salary;
extern float percentOfOres;

Then in one of your .cpp files, you define them:

float netWorth{ 0.0 };
float salary{ 0.0 };
float percentOfOres{ 0.0 };

Any top-level variables (those declared outside of a function) that are only used in one .cpp file can be declared static. These variables are considered a hidden implementation detail of that one compilation unit and will not be visible outside.

That is to say, you can have static float foo; in two different .cpp files without violating the one-definition rule. However, be aware that each file's foo refers to a different variable!


1 There are some exceptions to this rule. In particular, functions declared inline may be present in multiple compilation units, and the linker will pick one of them and discard the others.

cdhowie
  • 158,093
  • 24
  • 286
  • 300