C++ 指向未在作用域中声明的节点的指针数组

C++ 指向未在作用域中声明的节点的指针数组,c++,arrays,scope,C++,Arrays,Scope,我试图沿着指针数组为指针分配一个节点,但它一直告诉我我的数组没有在作用域中声明。我完全搞不懂任何帮助是如何或为什么会大有好处的!感谢您花时间回复 #include <iostream> #include "book.h" using namespace std; class bookstore { private: int amount = 5; int counting = 0; public: bookstore() {

我试图沿着指针数组为指针分配一个节点,但它一直告诉我我的数组没有在作用域中声明。我完全搞不懂任何帮助是如何或为什么会大有好处的!感谢您花时间回复

#include <iostream>
#include "book.h"

using namespace std;

class bookstore
{

private:

    int amount = 5;
    int counting = 0;
public:

    bookstore()

    {

        bookstore *store;
        store = new book*[amount];
        for(int i = 0; i < amount; i++)
        {
            store[i] = NULL;
        }
    }
    ~bookstore(){ delete[] store; }
    void addbook(string a,string b, string c, string d, string e)
    {
        if (counting == amount)
        {
            cout<<"Full House!"<<endl;
            return;
        }
        store[counting] = new book(a,b,c,d,e);
        counting++;
    }
    void print()
    {
        for(int i = 0; i < amount; i++)
        {
            cout<<store[i]->name<<" "<<store[i]->publisher<<" "<<store[i]->year<<" "<<store[i]->price<<" "<<store[i]->category<<endl;
        }
    }
};
#包括
#包括“book.h”
使用名称空间std;
班级书店
{
私人:
整数金额=5;
整数计数=0;
公众:
书店()
{
书店*商店;
门店=新书*[金额];
对于(int i=0;icout您的指针
store
是默认构造函数的本地指针。它看起来像是在查找数据成员。此外,您似乎在查找指针数组。如果是这样,您需要
bookstore
需要是指向指针的指针:

class bookstore
{
private:

    bookstore** store; // or bookstore* store 
    int amount = 5;
    int counting = 0;
并修复构造函数以使用:

bookstore()
{
    store = new book*[amount]; // or store = new book[amount]
    ....
请注意,您的类正在尝试管理动态分配的资源,因此您需要注意复制构造函数和赋值运算符(或者使类不可复制和不可赋值,或者实现它们。默认值是不可接受的。请参阅。)如果你真的在使用一个动态分配的指针数组,那么你也需要修复析构函数。目前,它只删除数组,而不删除其中指针指向的对象


更好的解决方案是使用一个管理资源的类,并具有所需的语义。每个类都处理单个的责任更容易。C++中的< /P>如果在卷曲括号内的函数中声明一个变量<代码> {…} /代码>,则在<代码> }以后不再存在。

@user3553272很高兴它对您有所帮助。您可以选择此答案(除非您想先等待可能的其他答案),它甚至会为您获得一些代表分数。有关更多信息,请参阅。