Const static arrays and classes

Jun 3, 2013 at 11:04pm
Hi

I'm trying to create a const static array of string for a pure virtual base class. Can anyone see what I'm doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
  class Item
{
private:
protected:
	string sellername;
	int price;
	static const string mediumar [ ] = {"Oils","Watercolour", "Other"};
public: 
	Item(string sellername1, int price1);
virtual void display()= 0;

};
Jun 4, 2013 at 1:34am
Are you using a C++11 compiler?
The syntax in line 7 is only supported in C++11.
Jun 4, 2013 at 2:03am
hmm good question. I am using g++ on linux and installed it a couple of weeks ago so don't think that will be a problem. Will check tho.
Jun 5, 2013 at 12:31am
I'm using g++ 4.6. Should that make a difference? All i want to do is make a const array of string that a class can use. How else could I initialize the array cos the compiler is still throwing out errors?

/tmp/ccWbqy0Y.o: In function `Painting::display()':
qu7.cpp:(.text+0x103): undefined reference to `Item::mediumar'
collect2: ld returned 1 exit status
Last edited on Jun 5, 2013 at 12:35am
Jun 5, 2013 at 8:01am
put mediumar outside Item (maybe in anamespace).

You don't need static
Jun 5, 2013 at 2:41pm
What if I only want the Item class to access it? and I want to share the array among the item class?

I only say this because the exercise i'm trying to do requires this,
Jun 5, 2013 at 2:49pm
1
2
3
4
5
6
7
8
9
10
11
12
// header file item.h
class item
{
    protected:
        std::string sellername;
        int price;
        static const std::string mediumar [] ;
    public:
        item( std::string sellername1, int price1);
        virtual ~item() {} // *** added
        virtual void display() const = 0 ; // *** const added
};


1
2
// implementation file item.cc (.cpp,.cxx)
const std::string item::mediumar []  = {"Oils","Watercolour", "Other"} ;
Topic archived. No new replies allowed.