我将通过Stephen Prata的C ++ Primer。我刚刚介绍了名称空间的概念。我创建了一个简单的三个文件项目来实现我的命名空间。我还没有实现太多的代码,但足够它应该编译。但是,我收到以下错误:未定义引用`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;
}
答案 0 :(得分:0)
呃,尝试将SALES::
添加到函数中。例如,
void SALES::setSales(SALES::Sales & s) { }