Mağaza arka ucunu yöneten bir program yazmaya çalışıyorum (ör. Müşterileri ve ürünleri takip etme). Ocak ayında C++ öğrenmeye yeni başladım ve kesinlikle sınıflar ve ayrı başlık dosyaları ve benzerleri konusunda yeni başlayan biriyim. Birkaç tanımlanmamış tanımlayıcı hatası ve bir çift bilinmeyen tip adı hatası alıyorum.Tanımlanmamış Tanımlayıcılar ve Bilinmeyen tür adı 'sınıf'
Kodun pürüzlü olduğunun farkındayım, sadece bunun üstesinden gelmeye çalışıyorum ve bu etkileşimlerin nasıl gerçekleştiğine dair iyi bir tutuş alıyorum.
Ana.cpp, MIA'm çünkü eğitmenim tarafından oluşturulan bir test işlevi tarafından gizlenmiş.
Alınan hatalar şu şekildedir: http://imgur.com/KT9mT2l Bu hataların konumları, kod içinde yorumlarla sırayla işaretlenmiştir. Herhangi bir yardım için şimdiden çok teşekkür ederim, sadece bu konuda daha iyi olmak istiyorum.
Store.h
#ifndef Store_h
#define Store_h
#pragma once
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
class Store{
std::vector<Product*> products; //Error 1-2
std::vector<Customer*> customers; //Error 3-4
public:
Store();
Store(std::string name);
std::string getStorename();
void setStorename(std::string name);
void addProduct(int productNum, std::string productName);
Customer* getCustomer(int customerID); //Error 5
void addCustomer(int customerID, std::string name, bool credit);
Product* getProduct(int productNum); //Error 6
void takeShipment(int productNum, int quantity, double cost);
void makePurchase(int customerID, int productNum, int quantity);
void takePayment(int customerID, double amount);
void listProducts();
void listCustomers();
private:
std::string name;
};
#endif /* Store_h */
Store.cpp
#include <iostream>
#include "Store.h"
#include "Product.h"
#include "Customer.h"
using namespace std;
Store::Store(){
}
Store::Store(string name): name(name) {
}
void Store::setStorename(string name) {
this->name = name;
}
string Store::getStorename(){
return name;
}
void Store::addProduct(int productNum, string productName){
Product* prod = new Product(productNum, productName);
products.push_back(prod);
}
Product* Store::getProduct(int productNum){
for (int i = 0; i < products.size(); ++i) {
Product* a = products.at(i);
if (a->getProductNum() == productNum) {
return a;
}
}
throw runtime_error("Invalid Product.");
}
void Store::addCustomer(int customerID, string name, bool credit){
Customer* cust = new Customer(name, customerID, credit);
customers.push_back(cust);
}
Customer* Store::getCustomer(int customerID){
for (int i = 0; i < customers.size(); ++i) {
Customer* a = customers.at(i);
if (a->getID() == customerID) {
return a;
}
}
throw runtime_error("Invalid Customer.");
}
void Store::takeShipment(int productNum, int quantity, double cost){
for (int i = 0; i < products.size(); ++i) {
Product* a = products.at(i);
if (a->getProductNum() == productNum) {
}
}
throw runtime_error("Invalid Product.");
}
void Store::makePurchase(int customerID, int productNum, int quantity){
Customer* a = getCustomer(customerID);
a->processPurchase(productNum, product); //ERROR 'use of undeclared
// variable 'product'
}
void Store::takePayment(int customerID, double amount){
Customer* a = getCustomer(customerID);
a->processPayment(amount);
}
void Store::listProducts(){
for (int i = 0; i < products.size(); ++i) {
Product* prod = products.at(i);
prod->getInfo();
cout << endl;
}
}
void Store::listCustomers(){
for (int i = 0; i < customers.size(); ++i) {
Customer* prod = customers.at(i);
prod->getInfo();
cout << endl;
}
}
Product.h
#ifndef Product_h
#define Product_h
#pragma once
#include <stdio.h>
#include "Customer.h"
#include "Store.h"
#include <string>
#include <iostream>
#include <vector>
class Product{
public:
Product();
Product(int productNum, std::string productName);
void setDescription(std::string description);
std::string getDescription();
void setDefaultReturnPeriod(int days);
int getReturnPeriod();
int getNumberSold();
double getTotalCost();
void addShipment(int quantity, double cost);
double getPrice();
void processOrder(int quantity);
int getProductNum();
void getInfo();
protected:
std::string productName;
private:
int productNum;
int inventory;
int numSold;
int totalCost;
std::string description;
int defaultReturnDays;
int quantity;
double cost;
};
#endif /* Product_h */
Customer.h
#ifndef Customer_h
#define Customer_h
#pragma once
#include <stdio.h>
#include <string>
#include <iostream>
#include "Store.h"
#include <vector>
class Customer{
public:
Customer();
Customer(std::string name, int customerID, bool credit);
void setName(std::string name);
std::string getName();
int getID();
void setCredit(bool hasCredit);
bool getCredit();
double getBalance();
void processPayment(double amount);
void processPurchase(double amount, Product product); //Error 7
void getInfo();
private:
std::string memberName;
std::string name;
int customerID;
bool credit;
double balance;
std::vector<Product*> productsPurchased; //Error 8
};
#endif /* Customer_h */
(http://stackoverflow.com/questions/4757565/c-forward-declaration)' Product' ve 'Customer'. "Customer.h" de ayrıca "Product.h" öğesini de içerir - ya da belki de 'product' parametresini 'processPurchase (...)' değerine göre daha iyi alamazsınız. –
'Store.cpp',' Product' (Ürünün), aynı zamanda, aynı şekilde slurped-in Product.h' içeriğiyle karşılaşmadan önce, preprocessor-slurped-Store.h' içeriğini derlemeye başladığında ne olduğu hakkında hiçbir fikre sahip değildir. Örnek kullanımdan ziyade işaretçi kullanımı nedeniyle bilmek * gerekmiyor, ama en azından bilmesi gereken bir şey var * böyle bir şey *. Ona böyle bir bilgi vermedin. Forward-decls muhtemelen sorununuzu çözecektir. Btw, ilgisiz, "Customer.h" nin neden Store.h'de * çekilmesi gerektiği konusunda hiçbir sebep göremiyorum *. Aynı şekilde Product.h için. – WhozCraig
Programda başlatılmadan önce 'Müşteri' ve' Ürün' kullanıyorsunuz. Bu sınıfları prototiplemeye çalış. –