c++错误:对全局变量和对象数组使用未声明的标识符



我目前正在尝试创建一个列表,将初始化100项对象,但由于某种原因,我的全局变量和数组没有被编译器识别。如有任何意见,将不胜感激。如果你发现我做错了什么,我非常感谢你的批评。

列表头:

#ifndef List_hpp
#define List_hpp
#include <stdio.h>
#include <string>
#include <iostream>
#include "Item.hpp"
using namespace std;
class List
{
    private:
    static int itemcount;
    Item items[100];
    public :
    List(){
        this->itemcount = 0;
    };
    void addItem(string, string, int, double);
    void removeItem(string);
    void display();
}; //End List Class
#endif /* List_hpp */

CPP文件:

#include "List.hpp"
//This method will add a new item
void addItem(string name, string unit, int quantity, double price){
    Item newItem(name, unit, quantity, price);
    itemcount++;
}
void removeItem(string name){
}
void display(){
}

在类列表的CPP文件中,必须在每个函数实现之前放置类名,如:

void List::addItem...
void List::removeItem... 
void List::display..

我认为addItem的实现还没有完成。您没有将新创建的项目放入列表中。必须为Item类声明复制构造函数,或者使用动态分配。

最新更新