3 文件项目中的 c++ 命名空间未定义引用
c++ namespaces in a 3 file project undefined reference
我正在学习 Stephen Prata 的 C++ Primer。我刚刚了解到名称空间的概念。我创建了一个简单的三文件项目来实现我的命名空间。我还没有实现太多代码,但足以编译。但是,我收到以下错误:对 `SALES::setSales(SALES::Sales&, double const*, int)' 的未定义引用。我想我没有 declared/included 正确的东西。这是我的三个文件。请帮助并提前感谢您阅读本文。
//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) { }
我正在学习 Stephen Prata 的 C++ Primer。我刚刚了解到名称空间的概念。我创建了一个简单的三文件项目来实现我的命名空间。我还没有实现太多代码,但足以编译。但是,我收到以下错误:对 `SALES::setSales(SALES::Sales&, double const*, int)' 的未定义引用。我想我没有 declared/included 正确的东西。这是我的三个文件。请帮助并提前感谢您阅读本文。
//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) { }