0
votes

I want to test the following lines but I get a LNK2019 error, pointing an error in my constructor. If the constructor implementation is moved from the source file to the header, it works...

dVector is a typedef of std::vector

error LNK2019: unresolved external symbol "public: __thiscall Grid::Grid(int,class std::vector > &,class std::vector > &,class std::vector > &)"

//main.cpp
#include <Math/PDE/Grid.h>
#include <vector>
int main(){

dVector oLowerBounds(2);
dVector oUpperBounds(2);
int lNumberOfStates(2);
dVector oMArkovianStates(2);
std::vector<int> oGridSizes(2);
Grid * pGrid=  new Grid(lNumberOfStates, oLowerBounds, oUpperBounds, oGridSizes);
return 0;
}

//Grid.h

#ifndef GRID_H
#define GRID_H
#pragma once

#include <Math/Matrix/Matrix.h>
#include<vector>

class Grid
{
public:

Grid(int inGridDimension,
     dVector &inLowerBounds,
     dVector &inUpperBounds,
     std::vector<int> &inGridSizes);

double getGridElement(int index1, int index2){return mGrid[index1][index2];};
void buildUniformGrid();
bool getIsUniformGrid(int index){return mIsUniformGrid[index];};
int getGridSize(int index);
~Grid(void);
private:
int mGridDimension;
std::vector<bool>   mIsUniformGrid;
std::vector<double> mLowerBounds;
std::vector<double> mUpperBounds;
std::vector<int> mGridSizes;
std::vector<std::vector<double>> mGrid;
};

#endif

//Grid.cpp

#include <Math/PDE/Grid.h>



Grid::Grid(int inGridDimension,
std::vector<double> &inLowerBounds,
std::vector<double> &inUpperBounds,
std::vector<int> &inGridSizes):
mGridDimension(inGridDimension),
mLowerBounds(inLowerBounds),
mUpperBounds(inUpperBounds),
mGridSizes(inGridSizes)
{

}


Grid::~Grid(void)
{
}
1
Is the Grid.cpp file also included in the compilation process?Maurice
The grip is included in one project . The main.ccp in another project. I compiled the first project containing the Grip class successfully. When I compile the second project containing the main.cpp , i get this errorKlibration
The problem occurs because the project with main.cpp cannot access the compiled Grid object. You can either add Grid.cpp and Grid.h to the main project or you should compile the project including Grid as a static or shared library and link that with the main project.Maurice
Oh I see ! Thank you . I have never done that before, do you know how to link them ? or can you give me the right direction ?Klibration
Are you using some kind of tool like Visual Studio, codeblocks or plain Makefiles? EDIT: It's probably easier to add them to a single project, the use of a library is only useful when you want to share the code or if some parts can be swapped out at runtime.Maurice

1 Answers

3
votes

The problem seems to be the fact that there are two projects and one cannot find the compiled class of the other.

I will try to explain the procedure to use one project as library and one as application in Visual Studio.

First in the library project do rightclick->properties, then under the tab General, Configuration Type should be Static library (.lib).

Then in the application project go to properties again, select Linker->Input and in the textbox Additional Libraries, use the arrow to select Edit. Then at the bottom at the full or relative path to the created library of the first project (You can first build it to verify the location).