0

我这里有这个类(这是一个 .h 文件)

    #ifndef STORE_H_
    #define STORE_H_

    #include <iostream>
    #include <set>
    #include "list.h"
    #include "Album.h"
    #include "Customer.h"


class Store {

    public:
static const int currentYear = 2013;
Store();
void addAlbum(int barcode, int price, int year, int numPurchased, string title,                             string artist);
void removeAlbum(int barcode);
void addCustomer(int id, string name);
void createNewOrder(int id, int ordNum);
void addToOrder(int ordNum, int barcode);
void removeFromOrder(int ordNum, int barcode);
void payOrder(int ordNum);
string getRecommendations(int numOfRecommendations) const;
string getInventoryReport() const;
string getCustomersReport() const;
string getCustomerReport(int id) const;
string getRevenueReport() const;
~Store();

bool albumsCompare(Album album1, Album album2);
bool customerCompare(Customer customer1, Customer customer2);
std::set<Album> albums;
std::set<Customer> customers;

};

#endif /* STORE_H_ */

我正在尝试访问设置的“专辑”

std::set<Album> albums;

从这里(.cpp 文件)

void addAlbum(int barcode, int price, int year, int numPurchased, string title, string artist) {


    if(barcode <= 0){
        throw INVALID_BARCODE();
    }

    if(price <= 0){
        throw INVALID_PRICE{};
    }

    if(year > Store::currentYear){
        throw INVALID_YEAR();
    }

    if(numPurchased < 0){
        throw INVALID_NUM_PURCHASED();
    }

    if(Album::findBarcode(Store::albums, barcode) != Store::albums.end()){
        throw BARCODE_ALREADY_EXISTS();
    }
}

但我收到错误:无法调用成员函数'std::set::iterator Album::findBarcode(const std::set&, const int&)'

任何解决此问题的帮助将不胜感激!

这是 findBarcode 函数

#include "Album.h"




std::set<Album>::iterator Album::findBarcode (std::set<Album>& set,int& barcode){
 Album data = Album();
for (std::set<Album>::iterator i = set.begin(); i != set.end(); i++){
     Album data = *i;
     if(data.barcode == barcode){
            return i;
     }
}
return set.end();

}
4

2 回答 2

4

您正在定义一个非成员函数addAlbum,与您的同名成员函数无关。您需要将您的成员函数定义放在Store范围内:

void Store::addAlbum(int barcode, int price, int year, int numPurchased, string title, string artist)
{//  ^^^^^
  ....
}
于 2013-06-25T14:19:50.680 回答
-1

默认情况下,在 C++ 中,当我们创建一个类时,所有字段都是私有的。您还可以使用 struct 关键字创建您的类,您的所有字段和方法都将是公开的

于 2013-06-25T14:21:25.440 回答