3 文件项目中未定义引用中的 C++ 命名空间

c++ namespaces in a 3 file project undefined reference

本文关键字:C++ 命名空间 引用 未定义 文件 项目      更新时间:2023-10-16

我正在阅读斯蒂芬·普拉塔(Stephen Prata C++的入门。我刚刚被介绍到命名空间的概念。我创建了一个简单的三文件项目来实现我的命名空间。我还没有实现很多代码,但足以编译它。但是,我收到以下错误:未定义对"SALES::setSales(SALES::Sales&, double const*, int)"的引用。我想我没有正确声明/包含某些内容。这是我的三个文件。请帮助并提前感谢您阅读本文。

//sales.h
#ifndef SALES_H_
#define SALES_H_
namespace SALES
{
    const int QUARTERS = 4;
    struct Sales
    {
        double sales[QUARTERS];
        double average;
        double max;
        double min;
    };
    // copies the lesser of 4 or n items from the array ar
    // to the sales member of s and computes and stores the
    // average, maximum, and minimum values of the entered items;
    // remaining elements of sales, if any, set to 0
    void setSales(Sales & s, const double ar[], int n);
    // gathers sales for 4 quarters interactively, stores them
    // in the sales member of s and computes and stores the
    // average, maximum, and minimum values
    void setSales(Sales & s);
    // display all information in structure s
    void showSales(const Sales & s);
}
#endif
//sales.cpp
#include <iostream>
#include "sales.h"
using namespace std;
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
void setSales(SALES::Sales & s, const double ar[], int n)
{
}
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minimum values
void setSales(SALES::Sales & s)
{
}
// display all information in structure s
void showSales(const SALES::Sales & s)
{
}
//main.cpp
#include <iostream>
#include "sales.h"
using namespace std;
using namespace SALES;
int main()
{
    Sales Bill;
    const double Bill_sales[] = {1200.23,1400.01,2357.45,4530.58};
    setSales(Bill,Bill_sales,QUARTERS);
    Sales Mary;
    setSales(Mary);
    return 0;
}

呃,尝试向函数添加SALES::。例如

void SALES::setSales(SALES::Sales & s) { }