2017-02-22 15 views
-2

Я новичок в C++, и в настоящее время я пытаюсь написать программу, которая использует функции main для вызова, которые написаны отдельно, и я попытался написать определения для деклараций файла заголовка, и я получаю ошибки с некоторые функции, которые я пометил в коденет подходящей функции для вызова

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 и использовать функцию addProduct в store.hpp, и у меня возникла проблема с тем, как написать код для этой функции, которая находится в store.cpp, вы можете просто посмотреть на нее i прокомментировали ошибку там – 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

thankyou так много @doctorlove u действительно помог мне много по-прежнему иметь некоторые вопросы и ошибки – user1210000

+0

как писать для checkoutmember все еще есть проблема с checkoutmember заранее Клиент * c = Магазин :: getMemberFromID (mID); я изменился на это внутри контрольного пакета – user1210000

+0

получил его, чемqq снова – user1210000

Смежные вопросы