简体   繁体   English

创建新数组时发生C ++错误:表达式必须具有指向对象类型的指针

[英]C++ Error when creating new array: Expression must have pointer to object type

I am trying to create a new array with positive values only taken from a already created array, and when I am looping through the original array, the index has an error "Expression must have pointer to object type" I tried doing research on the error, and everyone's situation is different when getting this error so I am on my own with this. 我试图创建一个仅从已经创建的数组中获取正值的新数组,并且当我遍历原始数组时,索引出现错误“表达式必须具有指向对象类型的指针”,我尝试对此错误进行研究,并且在遇到此错误时每个人的情况都不同,所以我自己一个人做。 Here is my code: 这是我的代码:

int foo::createNewArray() const {
    int newarray[50];
    int oldarray = oldarray[values];
    int size = (sizeof(oldarray));

    for (int i = 0; i > size; i++){
        if (oldarray[i] > 0)
            newarray[i] = oldarray[i];
    }

The "i" above is what has the error. 上面的“ i”是有错误的。 the oldarray[values] is declared in a seperate class file. oldarray [values]在单独的类文件中声明。 Here is the small section of the code where it comes from. 这是代码的一小部分。

        class foo{
        int oldarray[1];
        enum unit {values};

        public:
        int createNewArray() const;
};

Here you shadow oldarray array with a local int variable: 在这里,您使用局部int变量对oldarray数组进行了阴影oldarray

int oldarray = oldarray[values];

From that point on, until end of block, oldarray means an int, and then rest of the code does not make much sense with that. 从那时起,直到代码块结束, oldarray表示一个int,然后其余的代码对此没有多大意义。

The problem is because oldArray needs to be an int*, and not just an int. 问题是因为oldArray需要是一个int *,而不仅仅是一个int。 You are currently setting oldarray to the first value in the array, and not pointing it at the root of the array. 当前,您正在将oldarray设置为数组中的第一个值,而不是将其指向数组的根。 So something like int* oldArray = newArray will let you iterate through oldArray using the index operator. 因此,像int * oldArray = newArray这样的东西将允许您使用索引运算符遍历oldArray。

class Foo
{
    int* oldArray;
    int size;

public:
    int* CreateNewArray() const
    {
        int* newArray = new int[size];

        int current = 0;
        for( int index = 0; index < size; index++)
        {
            if(oldArray[index] > 0)
            {
                newArray[current] = oldArray[index];
                current++;
            }
        }

        return newArray;
    }
};

I apologize for haphazardly posting this without compiling. 对于随意发布而不进行编译,我深表歉意。 Although this solution may be closer to the metal than would be advised, it is still a valid solution to your problem, assuming oldArray and size are set before this method is called. 尽管此解决方案可能比建议的方案更接近金属,但如果在调用此方法之前已设置oldArray和size,则它仍然是解决问题的有效方法。

Here are some comments line by line of the problems with this code. 这是此代码问题的逐行注释。

class foo{
    int oldarray[1]; //Do you really want an array of size 1?  Why not just an integer?
    enum unit {values};//An enumeration that only enumerates one thing?  Are you sure you don't really want const int VALUES = 0;  I feel like you don't really even want an enum

    public:
    int createNewArray() const; 
};

int foo::createNewArray() const {
    int newarray[50];  //Magic numbers are bad, what if sizeof(oldarray) > 50?
    int oldarray = oldarray[values];  //Re declaring oldarray as a single integer and assigning it oldarray[values] as its value.
    int size = (sizeof(oldarray));  //is this oldarray an integer or an array of integers???

    for (int i = 0; i > size; i++){  //don't you want i < size??? if size > 0, this loop will never get run.
        if (oldarray[i] > 0) //probably grabbing the correct oldarray(Compilers are smart), but not getting expected values because the array version of oldarray wasn't initialized properly.
            newarray[i] = oldarray[i];
    }

I believe what you're attempting to do is the following: 我相信您要执行的操作如下:

int* foo::createNewArray() const {
    const int SIZE = sizeof(oldarray);
    int *newArray = int[SIZE];
    for(int i = 0; i < SIZE; i++) {
        if(oldarray[i] > 0) {
            newArray[i] = oldarray[i];
        } else {
            newArray[i] = 0;//In most environments this is unnecessary, but it is safer and good style
        }
    }

    return newArray;
}

Note, that even this code will only work if oldarray is within the scope of this code(not great style, passing it in as an argument to createNewArray would be better, but okay) and was instantiated correctly so that sizeof(oldarray) is the size of the array and not the size of an integer, or perhaps an integer pointer, I forget. 请注意,即使oldcode在此代码的范围内,该代码也才有效(不是很好的样式,将它作为参数传递给createNewArray会更好,但是可以)并且已正确实例化,因此sizeof(oldarray)是我忘记了数组的大小,而不是整数或整数指针的大小。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++表达式必须具有指向对象类型错误的指针 - C++ expression must have pointer to object type error C++ 表达式必须具有指向对象的类型 - C++ Expression must have pointer-to-object type C ++类向量错误,表达式必须具有指针类型 - C++ Class vector error, expression must have pointer type 函数内部的 C++ 错误:表达式必须具有指向对象的类型 - C++ error inside function: expression must have pointer-to-object type C ++“表达式必须具有指向对象类型的指针”和“下标需要数组或指针类型”是什么意思? - What does C++ mean by "expression must have pointer-to-object type" and "subscript requires array or pointer type"? 错误表达式必须有指向 object 类型的指针 - Error expression must have pointer to object type 创建数组时表达式必须有常量值错误 C++ - Expression must have a constant value error when creating an array C++ 对于c ++中的数组,什么是“表达式必须具有指向对象的指针类型”? - What is “expression must have pointer-to-object type” for arrays in c++? C ++中的学生成绩管理系统,表达式必须具有指向对象类型的指针 - Student Results Management System in C++, expression must have pointer to object type 表达式必须具有类类型:c ++将实例变量对象作为指针传递(点表示法参数) - expression must have class type: c++ passing instance variable object as a pointer (dot notation parameter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM