0

I have class here

#include<cstdlib>
#include<time.h>

    struct between{
    double min;
    double max;
};

class Init{



public:
    static const int args=2;
    static between* b;

    static double function(double i[]){
        return abs(i[0]*i[1]);
        return (25*i[0]*i[0]-5*i[0]+3);
    }

    static double abs(double d){
        return (d>0?d:-d);
    }

};

and class which includes:

#include "Init.cpp"
#include<iostream>

using namespace std;
class Chunk{
public:
    double values[Init::args];
    double res;
    static Chunk* sex(Chunk* c1,Chunk* c2){
        Chunk* c=new Chunk();
        for(int a=0;a<Init::args;a++){
            double t=getRand();
            c->values[a]=c1->values[a]*t+c2->values[a]*(1.0-t);
        }
        return c;

    }

    static double getRand(){
        double d=rand()/(double)RAND_MAX;
        return d;
    }

    double getResult(){

        res=Init::function(values);
    }

    static Chunk* generateChunk(){
    Chunk* c=new Chunk();
        for(int a=0;a<Init::args;a++){
            double t=getRand();
            c->values[a]=Init::b[a].min*t+Init::b[a].max*(1.0-t);//ERROR HERE!
        }
    return c;
    }

};

And I get error:

/home/oneat/NetBeansProjects/wearethechampions/Chunk.cpp:33: undefined reference to `Init::b'

Any idea why?

oneat
  • 9,780
  • 15
  • 47
  • 66
  • Despite this error, there is something wrong with your files organization - `#include "Init.cpp"` is not quite valid, we usually don't include source files. They are compiled and then linked, but never included (although there are some cases one might want to do that, this is certainly not one of them). – Mateusz Grzejek Mar 30 '15 at 14:51

2 Answers2

2

Error is caused by undefined static variable in class Init.

You declare two such variables:

static const int args = 2;

This is declaration and in-class initialization - constant integers are allowed to be initialized inside class body. Such members does not require additional definition, unless you want to use them as lvalue.

static between* b;

This is only declaration, b doesn't get defined anywhere. In source file (.cpp), that contains definition of methods belonging to Init class, add following line (you usually want to zero-initialize all pointers):

between* Init::b = NULL; //In pre-C++11 code

or

between* Init::b = nullptr; //In C++11-compliant code
Community
  • 1
  • 1
Mateusz Grzejek
  • 10,635
  • 3
  • 30
  • 47
  • the second gives me: Init.cpp:15:23: error: invalid in-class initialization of static data member of non-integral type ‘between*’ – oneat Mar 30 '15 at 14:51
  • @Mateusz `nullptr` should be preferred if available – Alejandro Díaz Mar 30 '15 at 14:53
  • This line `between* Init::b = NULL;` should be placed in `.cpp` file, not inside the class. Please specify, where you added this definition to. Also, read my comment under your post and think if your files are organized properly. – Mateusz Grzejek Mar 30 '15 at 14:53
  • No, the declaration of `args` isn't a definition. It allows the variable to be used as an _rvalue_, but you'll still need a separate definition if you need to take its address or otherwise access it as an _lvalue_. – Mike Seymour Mar 30 '15 at 14:55
  • @AlejandroDíaz `nullptr` should not be just "preferred". Valid code should never use `NULL` to represent empty pointers. But `C++11` tag has not been added, so I can't assume availability of its features. – Mateusz Grzejek Mar 30 '15 at 14:56
  • @MateuszGrzejek which is why I said "if available", thought you might wanna add it to the answer(for completeness sake), however is good as it is. – Alejandro Díaz Mar 30 '15 at 14:59
  • You're right - answer should be as reliable as possible, suggestion applied. @MikeSeymour You're absolutely right, I was a little bit too fast and didn't point the difference between definition and in-class initialization. Fixed now, thanks for correction. – Mateusz Grzejek Mar 30 '15 at 15:04
1

You need to add in a CPP file :

between* Init::b = NULL ; 

You defined b in the header, but you don't have the body of the static object defined in any object.

EDIT (since you have .cpp files only)

Define the b value outside your class .

Meaning, after the Init class declaration,add the line :

between* Init::b=NULL;
MichaelCMS
  • 4,585
  • 1
  • 20
  • 28