-1

Possible Duplicate:
C++: undefined reference to static class member

My goal here is to make a container object that will hold objects. I've decided to use a vector of pointers to do this.

Apart from the container object, there will be an abstract base class, that will have a function print. This is a virtual function, as the derived classes will be able to override the function. Finally I create an object from this derived class and try to store it in the container.

Below is the header file that holds the definition of the abstract class

Element.h

#ifndef ELEMENT_H
#define ELEMENT_H

using namespace std;

//Abstract class with pure virtual functions
class Element{
public:
    virtual void print()=0;     
};
#endif

Below I'm trying to create a template class for the container. Along with member functions to manipulate the container like, reverse the order, print etc.

Elements.h

#include <vector>
#include <Element.h>

using namespace std;

//Creating a class which will hold the vector of pointers
class Elements{

    public:
    static vector<Element*> elements;

    static void addElement(Element*e){
        elements.push_back(e);
    }

    static unsigned int size() {
        return elements.size();
    }
    static void print_all() {
    for (int i=0;i<elements.size();i++){
        elements[i]->print ();

        }
    }
    static void reverse(){
        int i=0;
        int j=elements.size()-1;

        while(!(i==j)&&!(i>j)){
            Element*temp;
            temp=elements[i];
            elements[i]=elements[j];
            elements[j]=temp;
            i++;
            j--;
        }
    }
};

Below I am creating an instance of the abstract class Element along with a few member functions. The container that I'm trying to build will hold this kind of objects.

I.h

#include <iostream>
#include <istream>
#include <ostream>
#include <vector>
#include <Element.h>

using namespace std;

class I:public Element{
int myInteger;

public:
I();
I(int);
void setI(int);
int getI(void);
void print();
};

I::I(int inInteger){
setI(inInteger);}

void I::setI(int inInteger){
myInteger=inInteger;
}

int I::getI(){
return myInteger;
}

void I::print(){
   cout<<"\nThe value stored in the Integer:"<<getI();
}

Below I'm trying to create objects of type I. Enter a value in it, getting its output. Then 'pushing' them in the container. main.cpp

#include <iostream>
#include <istream>
#include <ostream>
#include <vector>
#include "Element.h"
#include "I.h"
#include "Elements.h"

using namespace std;

int main() {
int userInt;
Element*element;
cout<<"enter an integer";
cin>>userInt;
element = new I(userInt);
element->print();    

Elements::addElement(element);

Element*element2;
cout<<"enter an integer";
cin>>userInt;
element2=new I(userInt);
element2->print();

Elements::addElement(element2);

Elements::print_all();
Elements::reverse();
int i=Elements::size();
cout<<i;

}

I am using Codeblocks 10.05 to compile, using gcc GNU compiler. When I build the above main.cpp, it gives me the error: 'undefined reference to 'Elements::elements' in Elements.h in each of the funcions: addElement,size,....etc

This is the first time I have posted on this forum. Any help and/or comments are most welcome.

Community
  • 1
  • 1
mathmax
  • 37
  • 1
  • 8

2 Answers2

1

It's juste because you declared elements, but not defined it. In order to do this, just add a definition after the class :

vector<Element*> Elements::elements;

Plus, you should seperate your headers and definition in separate files (*.hpp and *.cpp) and do not use class with only public static members, there's namespaces for that.

SRLKilling
  • 42
  • 7
0

You have declared Elements::elements in your class declaration. It's a static data member, so now you have to define it in some source file. For example,

vector<Element::Element*> Elements::element;

This needs to be at file scope in one and only source file. Otherwise you'll be violating the one definition rule.

David Hammen
  • 32,454
  • 9
  • 60
  • 108