c++ - Double 已经在 main.obj 中定义

标签 c++ visual-c++

我现在 stuck on a problem.它要求打印某种商店库存管理的每月销售额,然后要求用户输入 1-12 之间的数字以查看特定月份的总数。

我必须向您提供自从我在 Visual Studio 中执行此操作以来我所拥有的(创建了一个空白/空项目,因此没有“stdafx.h”文件。它应该在那里吗?):

销售.h:

#pragma once

#ifndef DEPTS
#define DEPTS 2
#endif

#ifndef STORES
#define STORES 2
#endif

#ifndef MONTHS
#define MONTHS 12
#endif

using namespace std;

double storeMonthlySales[STORES][MONTHS][DEPTS] = {
    {
        {1.1, 1.2}, {1.3, 1.4}, {1.5, 1.6}, {1.7, 1.8}, {1.9, 2.0}, {2.1, 2.2},
        {2.1, 2.2}, {2.3, 2.4}, {2.5, 2.6}, {2.7, 2.8}, {2.9, 3.0}, {3.1, 3.2}
    },
    {
        {3.1, 3.2}, {3.3, 3.4}, {3.5, 3.6}, {3.7, 3.8}, {3.9, 4.0}, {4.1, 4.2},
        {2.1, 2.2}, {2.3, 2.4}, {2.5, 2.6}, {2.7, 2.8}, {2.9, 3.0}, {3.1, 3.2}
    }
};

void printMonthlySales(double ss[DEPTS][MONTHS][STORES], int mon);

销售.cpp:

#include "Sales.h"
#include <iostream>

using namespace std;

void printMonthlySales(double ss[DEPTS][MONTHS][STORES], int mon)
{
    if ((mon < 1) || (mon > MONTHS))
    {
        cout << "\nMonth must be a number (1 = January, through 12 = December). Try again. \n" << endl;
    }
    else
    {
        double store_total = 0;
        double dept_total = 0;
        double total = 0;

        cout << "\t\t";
        for (int h = 0; h < DEPTS; h++)
        {
            cout << "Department " << h + 1 << "\t\t";
        }
        cout << "Store Total" << endl;
        for (int i = 0; i < STORES; i++)
        {
            cout << "Store " << i << "\t\t";
            for (int j = 0; j < DEPTS; j++)
            {
                double d = ss[j][mon][i];
                cout << d << "\t\t";
                store_total += d;
                total += d;
            }
            cout << endl;
        }
        cout << "Department Total\t";
        for (int i = 0; i < DEPTS; i++)
        {
            for (int j = 0; j < STORES; j++)
            {
                double d = ss[j][mon][i];
                cout << d << "\t\t";
            }
        }
        cout << total << "\t\t" << endl;
    }
}

这就是我目前拥有的 main.cpp。目前还没有完成,但我只是确保我走在正确的轨道上:

ma​​in.cpp:

#include "Sales.h"
#include <algorithm>
#include <iostream>
#include <sstream>

using namespace std;

int main(void)
{
    printMonthlySales(storeMonthlySales, 1);
    string a;
    cin >> a;
    return 0;
}

所以到目前为止,我应该为第 1 个月(1 月)的 storeMonhtlySales 打印 MonthlySales。 “string a”和“cin >> a”只是为了防止终端窗口自动关闭(同样,我在 Visual Studio 中这样做)。

但是,我在编译这些时遇到了两个错误:

LNK2005 "double (* storeMonthlySales)[12][2]" (?storeMonthlySales@@3PAY1M@1NA) already defined in main.obj
LNK1169 one or more multiply defined symbols found

我很困惑为什么他们说 printMonthlySales 是“已经定义的”,因为我看不出这里有什么问题。查看 Sales.obj 和 main.obj 文件什么也没做。

最佳答案

当您包含一个文件时,它实际上被复制并粘贴到包含文件中。 header 中的所有内容现在都是正在编译的文件的一部分。 #pragma once , 和其他形式的包含保护防止文件在 translation unit 中被多次包含.

但是……

翻译单元是分开编译的,所以它们之间没有连续性。 B.cpp 无法知道 A.cpp 已经包含给定的 header 。事实上,想一想如果一个大项目中只有一个文件会很糟糕#include <string> .在 C++ 离开办公室并进入贝尔实验室的走廊之前,它就会被认为是浪费时间而被丢弃。

所以... A.cpp 和 B.cpp 都包含 header.h。他们都有自己的 header.h 中所有内容的拷贝。两者都编译得很好,但链接器不知道如何处理竞争定义。

解决方案:

在 sales.h 中:

extern double storeMonthlySales[STORES][MONTHS][DEPTS];

extern 是某处的 promise storeMonthlySales将被定义,以便编译器可以继续执行它的工作。如果未定义,链接器会报错。

在 Sales.cpp 中:

double storeMonthlySales[STORES][MONTHS][DEPTS] = {
    {
        {1.1, 1.2}, {1.3, 1.4}, {1.5, 1.6}, {1.7, 1.8}, {1.9, 2.0}, {2.1, 2.2},
        {2.1, 2.2}, {2.3, 2.4}, {2.5, 2.6}, {2.7, 2.8}, {2.9, 3.0}, {3.1, 3.2}
    },
    {
        {3.1, 3.2}, {3.3, 3.4}, {3.5, 3.6}, {3.7, 3.8}, {3.9, 4.0}, {4.1, 4.2},
        {2.1, 2.2}, {2.3, 2.4}, {2.5, 2.6}, {2.7, 2.8}, {2.9, 3.0}, {3.1, 3.2}
    }
};

这是定义。它仅存在于 Sales.cpp 中。任何包含 Sales.h 的文件都可以使用它

顺便提防using namespace std; .这会给你带来麻烦。

关于c++ - Double 已经在 main.obj 中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54584784/

相关文章:

c# - Application.Run(form) 永不返回(在使用 System::Management 之后)

visual-studio - 为什么我需要安装 MSVC++ redist。?

c++ - 使用 MPI 在超立方体中广播

c++ - RPC、套接字和性能注意事项

使用模板和 nullptr 的 C++ 泛型编程

c++ - 何时在空实例上调用成员函数会导致未定义的行为?

c++ - 创建可测试代码

c++ - 需要了解良好的 C++ 反射 API(用于 RuntimeType Identification -RTTI 和运行时调用)

c++ - int 测试(无符号整数 i = -1);为什么编译器不生成警告?

c++ - 如何使用函数 : C++ 创建线程