Search code examples
c++linker-errorsunresolved-external

Unresolved external symbol but the function is defined and implemented


I have a header file, defining the chunk class:

#pragma once
#include <vector>

#include "Tile.h"
#include "Numerics.h"
namespace boch {
    class chunk {
    public:
        chunk();
        static const uint defsize_x = 16;
        static const uint defsize_y = 16;
        std::vector<std::vector<tile*>> tilespace;

        

        tile* getat(vint coords);
        void fillc(tile t);
    };
}

Then, I defined the implementation of the class in Chunk.cpp file:

#include "Chunk.h"

boch::chunk::chunk() {
    tilespace = std::vector<std::vector<tile*>>(defsize_x);
    for (int x = 0; x < defsize_x; x++) {
        std::vector<tile*> temp = std::vector<tile*>(defsize_y);
        tilespace[x] = temp;
    }
}

void boch::chunk::fillc(tile t) {
    for (int x = 0; x < defsize_x; x++) {
        for (int y = 0; y < defsize_y; y++) {
            tilespace[x][y] = new tile(t);
        }
    }
}

boch::tile* boch::chunk::getat(vint coords) {
    return tilespace[coords.x][coords.y];
}

(vint is a typedef of boch::vector<int> which is the custom X,Y vector, if that helps)

Then, I use it in the main function in BochGrounds.cpp file:

#include <iostream>
#include "Layer.h"
#include "Gamegrid.h"

int main()
{
    boch::layer newlayer = boch::layer(boch::vuint(16, 16));
    boch::chunk newchunk = boch::chunk();
    boch::gamegrid newgrid = boch::gamegrid();

    newchunk.fillc(boch::tile());
    newgrid.addchunk(boch::cv_zero, &newchunk);
    newgrid.drawtolayer(&newlayer);
    newlayer.draw(std::cout);
}

Tile class defines the gamegrid class, chunk includes tile class, gamegrid includes chunk & entity (which includes tile as well). Layer class includes only tile. All header files have #pragma once directive. When trying to compile, I'm getting the following error:

LNK2019 unresolved external symbol "public: __cdecl boch::chunk::chunk(void)" (??0chunk@boch@@QEAA@XZ) referenced in function main

LNK2019 unresolved external symbol "public: void __cdecl boch::chunk::fillc(class boch::tile)" (?fillc@chunk@boch@@QEAAXVtile@2@@Z) referenced in function main

and as the result:

LNK1120 2 unresolved externals

Other StackOverflow answers suggest that the linker cannot see implementations of both fillc() and chunk constructor functions, but I cannot see why if it is even the problem here. Please help. (Linker settings haven't been changed, and are default for MVSC 2019)


Solution

  • Thanks sugar for the answer. I deleted both header and .cpp files and readded them, and it worked like a charm. I suppose I have added either header or .cpp file just by directly adding a new file to the header/source folder instead of adding it to the project (RMB click on the project > add new item).