0

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.

scurry200
  • 3
  • 2
  • 5
    Start by removing `using namespace std;` see [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). For example [std::size](https://en.cppreference.com/w/cpp/iterator/size) is in the Standard Library and you have a `size_t size;` now we have confusion. – Richard Critten May 07 '23 at 23:20
  • Why don't you just make `vect` and `size` class members of some class? Or better yet make a std::vector a class member of some class. – drescherjm May 07 '23 at 23:23
  • 1
    Your `size` collides with https://en.cppreference.com/w/cpp/iterator/size because of `using namespace std;` – jabaa May 07 '23 at 23:23
  • I've edited accordingly but still having the same problems. g++ runner.cpp VectorNumberList.cpp -o runner /tmp/cccX213C.o:(.bss+0x0): multiple definition of `size_real' /tmp/cciGIRuB.o:(.bss+0x0): first defined here /tmp/cccX213C.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 – scurry200 May 07 '23 at 23:31

2 Answers2

1

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.

orabis
  • 2,749
  • 2
  • 13
  • 29
0

VectorNumberList.h has

extern int* vect;

But this is just a declaration, not a definition. You need the corresponding definition:

int* vect;

in one of your cpp files. Then it will link without errors.

TonyK
  • 16,761
  • 4
  • 37
  • 72