gpt4 book ai didi

c++ - 在Days.obj中已经定义了奇怪的错误 “float percentOfOres”(?percentOfOres @@ 3MA)

转载 作者:行者123 更新时间:2023-12-02 10:52:57 25 4
gpt4 key购买 nike

我是C++的新手,正在编写一个简单的角色扮演采矿游戏。我只编码了几周,但遇到了一个错误,该错误已经处理了好几天,但仍然没有解决任何问题。我什至考虑过只是取消程序然后重试。我有4个文件:Game.cpp,header.h,nuggetsFound.cpp和days.cpp。我试图拥有一个可以在整个文件中更改的全局变量,但是,即使我什至没有重新定义什么内容,我仍然会遇到这个疯狂的错误,提示“float percentOfOres”(?percentOfOres @@ 3MA)已经在Days.obj中定义。其中所有我的变量和文件都会 pop 其中的几个,而且我快要疯了。我只有13岁,所以我不能总是得到我需要的所有帮助,所以如果你们能帮助我,我将非常感谢。谢谢! (我排除了nuggetsFound.cpp,因为它与days.cpp的情况基本相同。game.cpp中还有很多代码,但我只包含了我认为相关的代码。这是两个文件

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");
}
}

}

最佳答案

  • 任何全局的东西都需要声明,但是没有在头文件中定义。
  • 将头文件包含到您的.cpp文件中。
  • 不要将.cpp文件包括到其他.cpp文件中。
  • 在一个.cpp文件中声明头文件中的所有内容。

  • 声明说“这个东西存在于某处”,定义说“使这个东西存在”。 C++具有一定义规则,该规则指出整个程序中可能仅存在针对特定事物的一个定义。1

    如果将定义放在头文件中并将其包含在两个.cpp文件中,则该定义在程序中存在两次(每个.cpp文件一次),这就是导致错误的原因。

    以下是一些声明示例:
    void foo();       // function declaration
    extern float bar; // variable declaration

    以下是一些定义示例:
    void foo() { }       // function definition
    float bar; // global variable definition
    static float widget; // local variable definition

    因此,在您的.h文件中,您应该声明变量,但不要定义变量:
    extern float netWorth;
    extern float salary;
    extern float percentOfOres;

    然后在您的一个.cpp文件中,定义它们:
    float netWorth{ 0.0 };
    float salary{ 0.0 };
    float percentOfOres{ 0.0 };

    可以将仅在一个.cpp文件中使用的所有顶级变量(在函数外部声明的那些顶级变量)声明为 static。这些变量被认为是该编译单元的隐藏实现细节,在外部将不可见。

    也就是说,您可以在两个不同的.cpp文件中包含 static float foo;而不违反一定义规则。但是,请注意,每个文件的 foo都引用不同的变量!

    1此规则有一些异常(exception)。特别是,声明为 inline的函数可能存在于多个编译单元中,链接器将选择其中一个,而丢弃其他一个。

    关于c++ - 在Days.obj中已经定义了奇怪的错误 “float percentOfOres”(?percentOfOres @@ 3MA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61349750/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com