Bulb::Bulb(int[4] pins){
    pinr = pins[0];

}

此代码是我创建的名为bulb的类的构造函数块,VSC引发错误
Expected a ')' with this piece of code on the int[4] pins
                                                      ^

最佳答案

让我们假设简单的类结构:

class Bulb {
    int pinr;

public:
    Bulb(int*); // first method
    Bulb(int[]); // second method
};

第一种方法:正确声明参数,int[] pin无效。
Bulb::Bulb(int pin[]) { // dynamically fixing array size + correct way
    pinr = pin[0];
    std::cout << pinr << std::endl;
}

第二种方法:作为指针传递,这里没有问题。
Bulb::Bulb(int* pin) { // as a pointer
    pinr = pin[0];
    std::cout << pinr << std::endl;
}

和驱动程序代码:
int main(void) {
    int tArr[] = {1, 2, 3, 4, 5};
    Bulb b(tArr); // outputs 1 by both methods

    return 0;
}

调试程序将为您提供:

调试第一种方法:

c&#43;&#43; - VSC throw 预期为 &#39;)&#39;-LMLPHP

调试第二种方法:

c&#43;&#43; - VSC throw 预期为 &#39;)&#39;-LMLPHP

关于c++ - VSC throw 预期为 ')',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62205369/

10-10 13:17