Getting multiple definition of 'size' and undefined reference to 'vect' errors, not sure how to solve, C++

Advertisements

Here is my code files

VectorNumbeList.cpp

#include <iostream>
#include "VectorNumberList.h" 
    void initialize() {
        vect = new int[10];}

VectorNumberList.h

#include <vector>
#include <cstdlib>
#include "NumberList.h"
extern int* vect;
size_t size_real;
void initialize(); 

NumberList.h

#ifndef NUMBER_LIST_H
#define NUMBER_LIST_H

#include <cstdlib>


class NumberList {
public:
    virtual void addNumberToList(int num) = 0;
    virtual void removeNumberFromFront() = 0;
    virtual void removeNumberFromBack() = 0;
    virtual int getNumberAt(size_t index) = 0;
    virtual size_t getSizeOfList() = 0;
    virtual void clear() = 0;

    void printNumbers();
    void computeNextFibonacciAndInsertAtEnd();
    void computeNFibonacci(size_t n);
    void copyListIntoMe(NumberList &numList);
    void reverseList();
    
};

#endif

runner.cpp

#include <iostream>
#include <string>
#include <vector>
#include "VectorNumberList.h"


int main() {
    initialize();
    return 0;
}

Makefile(runner is the folder name, different from runner.cpp)

CXX=g++
runner: runner.cpp VectorNumberList.cpp
    $(CXX) runner.cpp VectorNumberList.cpp -o runner


clean:
    rm runner *~

My error ends up being related to linking but I’m not sure as to how to solve it. Very confused, help would be much appreciated.
ERROR STATEMENT

g++ runner.cpp VectorNumberList.cpp -o runner
/tmp/ccJYylpB.o:(.bss+0x0): multiple definition of `size'
/tmp/ccLyM4kA.o:(.bss+0x0): first defined here
/tmp/ccJYylpB.o: In function `initialize()':
VectorNumberList.cpp:(.text+0x11): undefined reference to `vect'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'runner' failed
make: *** [runner] Error 1

I’ve tried modifying the include statements by removing them in runner.cpp or vectornumberlist.cpp, but I’ve had no luck. I’m very lost because I’m new to C++. It has something to do with the header file but I’m not sure on how to approach, besides removing the vect and size variables from the vectornumberlist.h file.

>Solution :

The error is mostly related to having multiple definitions of the size variable, as well as an undefined reference to the vect variable.

This is because you have declared these variables as extern in the header file VectorNumberList.h, but yet haven’t defined them in any source file.

You would need to remove the extern keyword from the declaration of vect.
Define the vect variable in VectorNumberList.cpp

int* vect;

In VectorNumberList.cpp, define the size variable

size_t size_real;

The updated VectorNumberList.cpp:

#include <iostream>
#include "VectorNumberList.h" 

int* vect;
size_t size_real;

void initialize() {
    vect = new int[10];
}

The updated VectorNumberList.h::

#ifndef VECTOR_NUMBER_LIST_H
#define VECTOR_NUMBER_LIST_H

#include <cstdlib>

extern int* vect;
extern size_t size_real;

void initialize();

#endif

I hope this will resolve your issues.

Leave a ReplyCancel reply