未声明的标识符&未知的类型名称' class'

时间:2016-04-01 04:31:15

标签: c++ class declaration

我正在尝试编写一个管理商店后端的程序(即跟踪客户和产品)。我刚刚在1月份开始学习c ++,我绝对是关于类和单独的头文件等的初学者。我收到一些未声明的标识符错误和一些未知的类型名称错误。

我非常清楚代码是粗糙的,我只是想解决这个问题并很好地掌握这些交互是如何发生的。

main.cpp是MIA,因为它由我的教师创建的测试函数隐藏。

收到的错误如下: http://imgur.com/KT9mT2l 这些错误的位置在代码中按顺序按注释标记。非常感谢你提前寻求任何帮助,只是想在这方面做得更好。

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 */

1 个答案:

答案 0 :(得分:2)

当编译器处理这些行时:

std::vector<Product*> products;              //Error 1-2
std::vector<Customer*> customers;            //Error 3-4

它对ProductCustomer一无所知。您可以转发声明它们,让编译器知道它们是类。

Store的定义开始之前添加以下行。

class Product;
class Customer;