2017-02-22 105 views
-2

我是新来的c + +和我目前正试图编写一个程序,使用主要调用函数,这是单独编写的,我试图写头文件声明的定义,我得到错误我已经标注在代码中的一些功能没有匹配的函数调用

Store.hpp

#ifndef STORE_HPP 

#define STORE_HPP 
class Product; 
class Customer; 
#include<string> 

#include "Customer.hpp" 
#include "Product.hpp" 
class Store 

{ 

private: 

std::vector<Product*> inventory; 

std::vector<Customer*> members; 


public: 

void addProduct(Product* p); 

void addMember(Customer* c); 

Product* getProductFromID(std::string); 

Customer* getMemberFromID(std::string); 

void productSearch(std::string str); 

void addProductToMemberCart(std::string pID, std::string mID); 

void checkOutMember(std::string mID); 

}; 

#endif 

我无法编写代码该功能帮助我

store.cpp

#include <iostream> 
#include <string.h> 
#include "Customer.hpp" 
#include "Store.hpp" 
#include "Product.hpp" 
using namespace std; 

string id; 

void Store::addProduct(Product* p)  //error 1 no matching function 
{ 

    Product* p(std::string id, std::string t, std::string d, double p, int qa); 
    inventory.push_back(p); 
} 

void Store:: addMember(Customer* c) 
{ 
    members.push_back(c->getAccountID()); 
} 

Product* Store::getProductFromID(std::string id) 
{ 
    for(int i = 0; i < inventory.size(); i++) 
    { 
     Product* p=inventory.at(i); 
     if(p->getIdCode()= id) 
     { 
      return p; 
     } 
} 
    return NULL; 
} 
Customer* Store:: getMemberFromID(std::string id) 
{ 
    for(int i = 0; i < members.size(); i++) 
    { 
     Customer* c = members.at(i); 
     if(c->getAccountID() == id) 
     { 

      return c; 
     } 
    } 
    return NULL; 
} 
void std::Store productSearch(std::string str) 
{ 
    for(int i = 0; i < inventory.size(); i++) 
    { 
     if(inventory[i] == str) 
     { 
      Product stud(inventory[i],inventory[i+1],inventory[i+2],inventory[i+3],inventory[i+4]); 
cout<<getIdCode(); 

cout<<getTitle(); 

cout<<getDescription(); 

cout<<getPrice(); 

cout<<getQuantityAvailable(); 
     } 
    } 
} 
void addProductToMemberCart(std::string pID, std::string mID) 
{ 
    cout<<"adding to cart"<<endl; 
    getMemberFromID(mID)->addProductToCart(pID); 

} 

void checkOutMember(std::string mID) 
{ 
    Customer* c=getAccountID(mID) 
    mID=getMemberFromID(std::string mID); 
    if(mID=="NULL") 
    { 
     cout<<mID<<"is not found"<<endl; 
    } 

} 

customer.hpp

#ifndef CUSTOMER_HPP 

#define CUSTOMER_HPP 

#include<vector> 

#include "Product.hpp" 

class Customer 

{ 

private: 

std::vector<std::string> cart; 

std::string name; 

std::string accountID; 

bool premiumMember; 

public: 

Customer(std::string n, std::string a, bool pm); 

std::string getAccountID(); 

//std::vector getCart(); 

void addProductToCart(std::string); 

bool isPremiumMember(); 

void emptyCart(); 

}; 

#endif 

product.hpp

#ifndef PRODUCT_HPP 

#define PRODUCT_HPP 

#include<vector> 

class Product 

{ 

private: 

std::string idCode; 

std::string title; 

std::string description; 

double price; 

int quantityAvailable; 

public: 

Product(std::string id, std::string t, std::string d, double p, int qa); 

std::string getIdCode(); 

std::string getTitle(); 

std::string getDescription(); 

double getPrice(); 

int getQuantityAvailable(); 

void decreaseQuantity(); 

}; 

#endif 
+3

请发表[MCVE。 –

+0

vittorio我正在尝试将产品添加到vectory库存中,并在store.hpp中使用函数addProduct,并且我遇到了如何编写store.cpp中该函数的代码的问题,您可以只看它已经评论了那里的错误 – user1210000

+0

你需要尽量减少你的例子 - 你从字面上发布了你的整个代码。制作一个重现问题并发布的简单示例。 –

回答

1

你代码发出大量警告和错误的立场。 如果您发现自己处于这种情况下,并且无法弄清楚其中的一种含义,请尝试修复其中的一些。

你的主要问题是addProduct,但也有其他

using namespace std; 

string id; //<---- what's this for? 

void Store::addProduct(Product* p)  //error 1 no matching function 
{ 

    //Product* p(std::string id, std::string t, std::string d, double p, int qa); 
    //<--- This line had the error and isn't needed 
    inventory.push_back(p); 
} 

void Store::addMember(Customer* c) 
{ 
// members.push_back(c->getAccountID()); //<--- this errors too 
    members.push_back(c); 
} 

Product* Store::getProductFromID(std::string id) 
{ 
    for (size_t i = 0; i < inventory.size(); i++) 
    { 
     Product* p = inventory.at(i); 
     //if (p->getIdCode() = id) //<-- be careful with = and == 
     if (p->getIdCode() == id) //<--- 
     { 
      return p; 
     } 
    } 
    return NULL; 
} 

Customer* Store::getMemberFromID(std::string id) 
{ 
    for (size_t i = 0; i < members.size(); i++) 
    { 
     Customer* c = members.at(i); 
     if (c->getAccountID() == id) 
     { 
      return c; 
     } 
    } 
    return NULL; 
} 

//void std::Store productSearch(std::string str) 
void Store::productSearch(std::string str) // <---- note this change too 
{ 
    for (size_t i = 0; i < inventory.size(); i++) 
    { 
     //if (inventory[i] == str) //<<--------! 
     if (inventory[i]->getDescription() == str) 
     { 
      //Product stud(inventory[i], inventory[i + 1], inventory[i + 2], inventory[i + 3], inventory[i + 4]); 
      // This is five Products from the inventory, not the i-th product in your invetory 
      Product stud(*inventory[i]);//<---- I assume     
      cout << stud.getIdCode(); 
      cout << stud.getTitle(); 
      cout << stud.getDescription(); 
      cout << stud.getPrice(); 
      cout << stud.getQuantityAvailable(); 
     } 
    } 
} 

void Store::addProductToMemberCart(std::string pID, std::string mID)//<--- note note std::Store addProductToMemberCart 
{ 
    cout << "adding to cart" << endl; 
    getMemberFromID(mID)->addProductToCart(pID); 

} 

void Store::checkOutMember(std::string mID)//<--- 
{ 
    //Customer* c = getAccountID(mID);//<<---? 
     //mID = getMemberFromID(std::string mID); //<---? 
    Customer* c = getMemberFromID(mID); //Just this? 
    if (c == NULL)//<---rather than "NULL" but nullptr might be better 
    {    // or not using pointers at all 
     cout << mID << "is not found" << endl; 
    } 

} 
+0

谢谢你这么多@doctorlove你真的帮了我很多仍然有一些问题和错误 – user1210000

+0

如何编写checkoutmember客户* c = Store :: getMemberFromID(mID);我已经改成这个结帐员 – user1210000

+0

里面再次得到了thanqq – user1210000

相关问题