0

I keep getting undefined references to static members every time I try to compile the following code:

main.cpp:

#include <iostream>
#include <iomanip>
#include "Obj1.h"
using namespace std;

int main(){
    double value;
    cout << "Enter shared val: ";
    cin >> value;
    obj1::initialize(value); //static member function called
    obj1 object;
    cout << "\nValue: " << object.getSharedVal();//prints static member var
}

Obj1.h:

#ifndef OBJ1_H
#define OBJ1_H

class obj1{
    private:
        static double sharedVal; 
        double val;
    public:  
        obj1(){
            val = 0;
        }
        void add(double d){
            val += d;
            sharedVal += d;
        }
        double getVal() const{
            return val;
        }
        double getSharedVal() const{
            return sharedVal;
        }
        static void initialize(double);
};

#endif

Obj1.cpp:

#include "Obj1.h"

double obj1::sharedVal = 0; //define static members outside class.

void obj1::initialize(double d){
    sharedVal += d;
}

When I try to compile main.cpp (with g++ main.cpp), I get the following errors:

/tmp/cc51LhbE.o: In function `main':
objectTester.cpp:(.text+0x45): undefined reference to `obj1::initialize(double)'
/tmp/cc51LhbE.o: In function `obj1::getSharedVal() const':
objectTester.cpp:(.text._ZNK4obj112getSharedValEv[_ZNK4obj112getSharedValEv]+0xc): undefined reference to `obj1::sharedVal'
collect2: error: ld returned 1 exit status

Why does this error appear?

Additionally: I'm using Geany Text Editor in Ubuntu 16.04, a text editor I am completely new to. I also notice in .h files for Geany, keywords such as private, public, and class are not boldfaced like .cpp files. Why is this?

Anonymous
  • 411
  • 1
  • 4
  • 14
  • 2
    You forgot to link with `Obj1.o`. – Barmar Feb 27 '18 at 01:56
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Barmar Feb 27 '18 at 01:56
  • 2
    g++ -o main main.cpp Obj1.cpp -std=c++11 – P. Hinker Feb 27 '18 at 02:39
  • @user4581301 Thanks, but that link doesn't help. I literally made a separate Obj1.cpp class with separate static member definitions just for this. – Anonymous Feb 27 '18 at 02:39
  • @P.Hinker Thanks, that worked. Never really had to link multiple .cpp files in the past before though, can you explain to me what that does for learning sake? – Anonymous Feb 27 '18 at 02:42
  • My apologies. Somehow I did not see the extra cpp file. Probably the Giver Of Data telling me it's time to shut down for the night. – user4581301 Feb 27 '18 at 02:43
  • you have build app using only once source, so linking has failed since it could not find declared symbol. With corrected command you are creating app whick will contain both sources. You should start use some build manager tool like cmake to handle this cases. – Marek R Feb 27 '18 at 02:45
  • The linker is complaining that it can't find the implementation for the obj1::initialize method. That implementation is contained in the Obj1.cpp file. If you compile things separately [g++ -c main.cpp and g++ -c Obj1.cpp] then you have the implementation in the Obj1.o file and the linker (ld) doesn't know how to satisfy the reference to that member function when it's trying to build the executable file. If you compile both files on the same g++ command then the linker (ld) can find it. – P. Hinker Feb 27 '18 at 02:46

0 Answers0