【问题标题】:Declaring an array inside a class. C++在类中声明一个数组。 C++
【发布时间】:2020-02-15 16:39:30
【问题描述】:

我想创建一个初始化数组的类和一个函数push,它将一个元素添加到数组而不是打印它。 作为初学者,我知道初始化有问题,除了函数push 之外,一切正常,它不打印数组。 我的课怎么了?

#include <iostream>

class pile{
    public :
    int n;
    int p[]={};
    pile(int m){
        n=m;
        std::cout<<"Contructor is up .. ";
    }

    int init(int n){
        int z=n-1;
        for(int i;i<=z;i++){
            std::cin>>p[i];
        }
    }

    int push(int n, int x){
        int y=n-1;
        int p[]={};
        p[n]=x;
        for(int u=0;u<=n;u++){
            std::cout<<p[u]<<" ";
        }
    }


};

int main(){
    int a;
    std::cout<<"How many integers does your array got ?  >> ";
    std::cin>>a;

    pile p1(a);
    std::cout<<"\nEnter your array's integers >> ";
    p1.init(a);
    int j;
    std::cout<<"\nInteger that you want to add to the array ? >> ";
    std::cin>>j;
    std::cout<<"\nThe new array is >> ";
    p1.push(a,j);

    return 0;
}

【问题讨论】:

  • 你想要的是std::vector,除非你在编译时知道你需要多少元素。
  • 我也投票给std::vector,我想指出空数组在C++中是非法的
  • 不要使用 C 风格的数组。如果您在编译时知道尺寸,请使用std::array,否则使用std::vector

标签: c++ arrays function class


【解决方案1】:

我想创建一个初始化数组的类,

具有值初始化的数组成员的类示例:

struct foo {
    int member[10] = {};
};

我的课怎么了?

这是错误的:

int p[]={};

首先,成员可能不是未指定长度的数组,即使它具有初始化程序。其次,没有变量可以是零长度数组。

还有一个函数 push 将元素添加到数组而不是打印它

无法将元素添加到数组中。数组在其生命周期内具有恒定数量的元素。

您可能正在寻找的是一种动态分配数组的数据结构,并在添加更多元素时将元素复制到一个新的更大的数组中。除了这个简洁的描述之外,数据结构还有更多内容。有一个标准容器实现了这种“可调整大小的数组”数据结构:std::vector

【讨论】:

    【解决方案2】:

    所以,有时使用内存的东西会更好,这就是为什么你的代码可能看起来像下面的代码:

    #include <iostream>
    using namespace std; // std:: namespace usage is not needed after that
    
    class pile{
    
        int n; // it's a not best practice to declare this as public
        int* p; // better way to hide this fields include them in to private
    
    public :
    
        pile(int m){
            n=m;
            p = new int[n];
            cout<<"Constructor is up .. ";
        }
        ~pile(){
          if(p != nullptr){
            delete p;
          }
        }
    
        void init(int n){
            for(int i = 0;i < n; i++){
                cin>>p[i];
            }
        }
    
        int push(int j, int x){
            int oldValue = p[j];
            if(j >= 0 && j < n) { // this checking is necessary
              p[j]=x;
            } else if (j == n) {
              // you need to reset memory
              // there are different ways to do that, for example
              int* q = new int[n + 1];
              int i = 0;
              for(; i < n; i++) {
                q[i] = p[i];
              }
              q[i] = x;
              delete p;
              p = q;
              n++;
            }
            // you are forget the return statement
            return oldValue;
        }
    
        friend ostream& operator<<(ostream& os, const pile& pil);
    };
    
    // it's most common way to use standard output to draw your class content
    ostream& operator<<(ostream& os, const pile& pil) {
      for(int i = 0; i < pil.n; i++){
        os << pil.p[i] << " ";
      }
      return os;
    };
    // also you may override >> operator to swap your init fun.
    
    int main(){
        int a;
        cout<<"How many integers does your array got ?  >> ";
        cin>>a;
    
        pile p1(a);
        cout<<"\nEnter your array's integers >> ";
        p1.init(a);
        int j;
        cout<<"\nInteger that you want to add to the array ? >> ";
        cin>>j;
        p1.push(a,j);
        cout<<"\nThe new array is >> ";
        cout << p1 << endl; // looks very nice for now
    
        return 0;
    }
    

    【讨论】:

    • 如果您复制或移动类,您的代码中似乎存在一些错误,我同意评论者的观点:使用库中的向量/数组
    猜你喜欢
    • 2020-03-02
    • 2016-12-09
    • 1970-01-01
    • 2010-10-30
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多