I keep getting errors and I don't know why..

#include <iostream>
using namespace std;
class IceCream{
private:
    int flavor[3]={0,0,0}, topping[3]={1,2,3}, scoops[3]={4,5,6};
public:
    int getFlavor(int);
    int getTopping(int);
    int getScoops(int);
    void setFlavor(int);
    void setTopping(int);
    void setScoops(int);
}flavorX, toppingX, scoopsX;
int IceCream::getFlavor(int choice){return flavor[choice];};

int main()
{
    cout << flavorX.getFlavor(1);
    system("PAUSE");
}

And here's the explanation:

error: ISO C++ forbids initialization of member 'flavor' [-fpermissive]
error: ISO C++ forbids initialization of member 'topping' [-fpermissive]
error: ISO C++ forbids initialization of member 'scoops' [-fpermissive]

in other words, your compiler does not support C++0x future for initializing class members.

Edited 4 Years Ago by Lucaci Andrew

You could declare them as global variables:

#include <iostream>
using namespace std;

int flavor[] = {0, 0, 0};
int topping[] = {1, 2, 3};
int scoops[] = {4, 5, 6};
class IceCream{
public:
    int getFlavor(int);
    int getTopping(int);
    int getScoops(int);
    void setFlavor(int);
    void setTopping(int);
    void setScoops(int);
}flavorX, toppingX, scoopsX;
int IceCream::getFlavor(int choice){return flavor[choice];}

int main(){
    cout << flavorX.getFlavor(0);
}

Line 5 uses "non-static data member initializers". This is a new feature of C++ (C++11) and many compilers do not support that feature yet. I compiled your code successfully with GCC 4.7.3, and I would guess that version 4.7.0 and above will compile it fine. However, version 4.6.3 does not compile this code, saying that "non-static data member initializers" is no implemented yet. So, that's that.

Without support for C++11, you're gonna need to make a constructor to initialize your non-static data members, as so:

class IceCream{
private:
    int flavor[3], topping[3], scoops[3];
public:

    IceCream() {
      flavor[0] = 0; flavor[1] = 0; flavor[2] = 0;
      topping[0] = 1; topping[1] = 2; topping[2] = 3;
      scoops[0] = 4; scoops[1] = 5; scoops[2] = 6;
    };

    int getFlavor(int);
    int getTopping(int);
    int getScoops(int);
    void setFlavor(int);
    void setTopping(int);
    void setScoops(int);
} flavorX, toppingX, scoopsX;

I know it is annoying, but that's the way it is.

This article has been dead for over six months. Start a new discussion instead.