Fundamental lack of knowledge

Hi,

I am getting the error:


/home/cristiano/NetBeansProjects/Cpp_Teste/GPA.cpp:12: undefined reference to `GPA::MATRIX'
/home/cristiano/NetBeansProjects/Cpp_Teste/GPA.cpp:12: undefined reference to `GPA::MATRIX'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* 
 * File:   main.cpp
 * Author: cristiano
 *
 * Created on May 12, 2010, 4:38 PM
 */

#include <stdlib.h>

#include "GPA.h"
/*
 * 
 */
int main(int argc, char** argv) {

    GPA gpa;
    gpa.start();

    return (EXIT_SUCCESS);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* 
 * File:   GPA.cpp
 * Author: cristiano
 * 
 * Created on May 12, 2010, 4:40 PM
 */

#include "GPA.h"


void GPA::load() {
    MATRIX = 2.1;
}
void GPA::start() {
    load();
}
//
GPA::GPA() {
}

GPA::GPA(const GPA& orig) {
}

GPA::~GPA() {
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* 
 * File:   GPA.h
 * Author: cristiano
 *
 * Created on May 12, 2010, 4:40 PM
 */

#ifndef _GPA_H
#define	_GPA_H

class GPA {
public:
    //
    static void load();
    static void start();
    //
    static double MATRIX;
    //
    GPA();
    GPA(const GPA& orig);
    virtual ~GPA();
private:

};

#endif	/* _GPA_H */ 


Is not possible to setup a static property?
When you have a static variable, you have to declare it in both, the header and any .cpp.
That means, you have to put double GPA::MATRIX; somewhere (GPA.cpp I suppose)
Good luck
You should define GPA::MATRIX in the GPA.cpp file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* 
 * File:   GPA.cpp
 * Author: cristiano
 * 
 * Created on May 12, 2010, 4:40 PM
 */

#include "GPA.h"

double GPA::MATRIX = 0.0;

void GPA::load() {
    MATRIX = 2.1;
}
void GPA::start() {
    load();
}
//
GPA::GPA() {
}

GPA::GPA(const GPA& orig) {
}

GPA::~GPA() {
}
Basically, the .cpp file is where you usually put all the DEFINITIONS. That is where you DEFINE your functions and any STATIC member variables.

The .h file is where you usually put your DECLARATIONS.

So you DECLARE what you have in the header file (.h) so that other people know what's available.

But you DEFINE what you declared in the source file (.cpp).

Things can be DECLARED many times but they must only be DEFINED exactly once.

So the header file can be included in many source files but your source files are compiled individually, exactly once.
Topic archived. No new replies allowed.