我正在学习Stephen Prata的书“ C ++ Primer Plus”中的C ++,并且目前正在进行名称空间练习。这是锻炼程序应该如何工作的:
第一个文件应该是包含名称空间的头文件。 第二个文件应该是扩展了 为三个原型函数提供定义的名称空间。 第三个文件应声明两个Sales对象。它应该使用 setSales()的交互式版本,可为一个结构提供值 和setSales()的非交互式版本,以提供以下值: 第二种结构。它应该显示两个的内容 使用showSales()构建结构。
我写了两个cpp文件和一个头文件。我预先为我的意大利面条代码表示歉意。请记住,我只是一个初学者。 这是我的所有文件。
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 lessoer 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 SALES;
void setSales(Sales & s, const double ar[], int n) {
double tmp;
double tmp_ar[n];
//making temporary copy of an array
for(int i = 0; i < QUARTERS; i++) {
tmp_ar[i] = ar[i];
}
//bubble sort
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(tmp_ar[i] > tmp_ar[j]){
tmp = tmp_ar[i];
tmp_ar[i] = tmp_ar[j];
tmp_ar[j] = tmp;
}
}
}
//filling s.sales with 4 lesser elements
for(int i = 0; i < QUARTERS; i++) {
s.sales[i] = tmp_ar[i];
}
//finding and assigning minimum
tmp = s.sales[0];
for(int i = 1; i < QUARTERS; i++) {
if(s.sales[i] < s.sales[i - 1])
tmp = s.sales[i];
}
s.min = tmp;
tmp = s.sales[0];
//finding and assigning maximum
tmp = s.sales[0];
for(int i = 1; i < QUARTERS; i++){
if(s.sales[i] > s.sales[i - 1])
tmp = s.sales[i];
}
s.max = tmp;
//calculating average
double tmp_sum = 0;
for(int i = 0; i < QUARTERS; i++) {
tmp_sum += s.sales[i];
}
s.average = tmp_sum / QUARTERS;
}
void setSales(Sales & s) {
using std::cin;
using std::cout;
using std::endl;
cout << "Input sales for each quarter" << endl;
double input;
for(int i = 0; i < QUARTERS; i++) {
cout << "Input sales for " << i + 1 << " quarter: ";
while(!(cin >> input)) {
cout << "Not an integer. Try again..." << endl;
}
s.sales[i] = input;
}
//temporary value for maximum and minimum
double tmp;
//finding and assigning minimum
tmp = s.sales[0];
for(int i = 1; i < QUARTERS; i++) {
if(s.sales[i] < s.sales[i - 1])
tmp = s.sales[i];
}
s.min = tmp;
tmp = s.sales[0];
//finding and assigning maximum
tmp = s.sales[0];
for(int i = 1; i < QUARTERS; i++){
if(s.sales[i] > s.sales[i - 1])
tmp = s.sales[i];
}
s.max = tmp;
//calculating average
double tmp_sum = 0;
for(int i = 0; i < QUARTERS; i++) {
tmp_sum += s.sales[i];
}
s.average = tmp_sum / 4;
}
void showSales(const Sales & s) {
using std::cin;
using std::cout;
using std::endl;
for(int i = 0; i < QUARTERS; i++)
cout << "Quarter " << i + 1 << ": " << s.sales[i] << endl;
cout << "Minimum: " << s.min << endl;
cout << "Maximum: " << s.max << endl;
cout << "Average: " << s.average << endl;
}
main.cpp
#include <iostream>
#include "sales.h"
using namespace SALES;
int main() {
Sales year_one, year_two;
setSales(year_one);
showSales(year_one);
int arr_size = 4;
double arr[arr_size] = {400., 500., 200., 1000.};
setSales(year_two, arr, arr_size);
showSales(year_two);
return 0;
}
编译时出现错误:
sloth@sloth-commodore64:~/Programming/C++/src/Chapter9Exercise/ex4$ g++ main.cpp sales.cpp
/tmp/ccl6q4PO.o: In function `main':
main.cpp:(.text+0x34): undefined reference to `SALES::setSales(SALES::Sales&)'
main.cpp:(.text+0x43): undefined reference to `SALES::showSales(SALES::Sales const&)'
main.cpp:(.text+0x152): undefined reference to `SALES::setSales(SALES::Sales&, double const*, int)'
main.cpp:(.text+0x15e): undefined reference to `SALES::showSales(SALES::Sales const&)'
collect2: error: ld returned 1 exit status
我也做了另一个main.cpp只是为了测试。我的代码版本既不使用多个文件也不使用名称空间,并且似乎可以正常工作。
我真的很困惑为什么它不起作用。我同时搜索了stackoverflow和Google,但似乎找不到解决方法。
编辑: 多亏@Justin和@OznOg,我找到了个人。在文件〜/ sales.h /中,我添加了一个带有SALES名称空间的前缀。现在〜/ sales.h /看起来像这样:
sales.h
#include <iostream>
#include "sales.h"
using namespace SALES;
void SALES::setSales(Sales & s, const double ar[], int n) {
double tmp;
double tmp_ar[n];
//making temporary copy of an array
for(int i = 0; i < QUARTERS; i++) {
tmp_ar[i] = ar[i];
}
//bubble sort
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(tmp_ar[i] > tmp_ar[j]){
tmp = tmp_ar[i];
tmp_ar[i] = tmp_ar[j];
tmp_ar[j] = tmp;
}
}
}
//filling s.sales with 4 lesser elements
for(int i = 0; i < QUARTERS; i++) {
s.sales[i] = tmp_ar[i];
}
//finding and assigning minimum
tmp = s.sales[0];
for(int i = 1; i < QUARTERS; i++) {
if(s.sales[i] < s.sales[i - 1])
tmp = s.sales[i];
}
s.min = tmp;
tmp = s.sales[0];
//finding and assigning maximum
tmp = s.sales[0];
for(int i = 1; i < QUARTERS; i++){
if(s.sales[i] > s.sales[i - 1])
tmp = s.sales[i];
}
s.max = tmp;
//calculating average
double tmp_sum = 0;
for(int i = 0; i < QUARTERS; i++) {
tmp_sum += s.sales[i];
}
s.average = tmp_sum / 4;
}
void SALES::setSales(Sales & s) {
using std::cin;
using std::cout;
using std::endl;
cout << "Input sales for each quarter" << endl;
double input;
for(int i = 0; i < QUARTERS; i++) {
cout << "Input sales for " << i + 1 << " quarter: ";
while(!(cin >> input)) {
cout << "Not an integer. Try again..." << endl;
}
s.sales[i] = input;
}
//temporary value for maximum and minimum
double tmp;
//finding and assigning minimum
tmp = s.sales[0];
for(int i = 1; i < QUARTERS; i++) {
if(s.sales[i] < s.sales[i - 1])
tmp = s.sales[i];
}
s.min = tmp;
tmp = s.sales[0];
//finding and assigning maximum
tmp = s.sales[0];
for(int i = 1; i < QUARTERS; i++){
if(s.sales[i] > s.sales[i - 1])
tmp = s.sales[i];
}
s.max = tmp;
//calculating average
double tmp_sum = 0;
for(int i = 0; i < QUARTERS; i++) {
tmp_sum += s.sales[i];
}
s.average = tmp_sum / QUARTERS;
}
void SALES::showSales(const Sales & s) {
using std::cin;
using std::cout;
using std::endl;
for(int i = 0; i < QUARTERS; i++)
cout << "Quarter " << i + 1 << ": " << s.sales[i] << endl;
cout << "Minimum: " << s.min << endl;
cout << "Maximum: " << s.max << endl;
cout << "Average: " << s.average << endl;
}
此更改程序正常运行后。