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");
}

Recommended Answers

All 4 Replies

Line 5 is wrong. you do the initialization of the varibles in the constructor.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.