没有用于调用的匹配函数 我是C++新手,我现在正在尝试编写一个程序,它使用主调用函数,它是分开写的,我试图写头文件声明的定义,并且我在代码中的一些函数中得到了错误,这些代码在代码中已经标出了。
Store.hpp没有用于调用的匹配函数 我是C++新手,我现在正在尝试编写一个程序,它使用主调用函数,它是分开写的,我试图写头文件声明的定义,并且我在代码中的一些函数中得到了错误,这些代码在代码中已经标出了。,c++,function,header-files,C++,Function,Header Files,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*> member
#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
\ifndef商店\u水电站
#定义存储单元HPP
类产品;
类别客户;
#包括
#包括“Customer.hpp”
#包括“Product.hpp”
班级商店
{
私人:
病媒清单;
std::向量成员;
公众:
无效添加产品(产品*p);
无效添加成员(客户*c);
Product*getProductFromID(std::string);
客户*getMemberFromID(标准::字符串);
void productSearch(std::string str);
void addProductToMemberCart(标准::字符串pID,标准::字符串mID);
void checkout成员(std::string mID);
};
#恩迪夫
我在为该函数编写代码时遇到问题。请帮助我
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”
#包括“Store.hpp”
#包括“Product.hpp”
使用名称空间std;
字符串id;
void Store::addProduct(Product*p)//错误1无匹配函数
{
产品*p(标准::字符串id,标准::字符串t,标准::字符串d,双p,int qa);
库存。推回(p);
}
无效存储::添加成员(客户*c)
{
members.push_back(c->getAccountID());
}
产品*商店::getProductFromID(标准::字符串id)
{
对于(int i=0;igetIdCode()=id)
{
返回p;
}
}
返回NULL;
}
客户*商店::getMemberFromID(标准::字符串id)
{
对于(int i=0;igetAccountID()==id)
{
返回c;
}
}
返回NULL;
}
void std::Store productSearch(std::string str)
{
对于(int i=0;i 您的代码会发出大量警告和错误。
如果您发现自己处于这种情况,并且无法理解其中一个是什么意思,请尝试解决其他一些问题
您的主要问题在于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;
}
}
使用名称空间std;
字符串id;//getAccountID());//getIdCode()=id)//getIdCode()=id)//getAccountID()=id)
{
返回c;
}
}
返回NULL;
}
//void std::Store productSearch(std::string str)
无效存储::产品搜索(std::string str)//请发布一个.vittorio。我正在尝试将一个产品添加到vectory库存中,并使用store.hpp中的函数addProduct,我在如何为store.cpp中的函数编写代码方面遇到问题。您只需查看它,我已经在那里对错误进行了注释。您需要最小化您的示例-您确实发布了您的en轮胎代码。制作一个复制问题的最小示例并发布。哈哈,好的,我现在就这么做addProduct
的第一行应该做什么?(它做的事情是声明一个函数p
,它返回一个产品*
)非常感谢@doctorlove你真的帮了我很多,还有一些问题和错误需要为checkout编写会员还有问题checkout会员提前感谢客户*c=Store::getMemberFromID(mID);我在checkout会员中更改了这个,再次收到了QQ
#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
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;
}
}