Mark Z.
Mark Z.

Reputation: 655

error LNK2019 for unknown reason

My code is like this:

Basic.h

#define Type double

Model.h

#include "Basic.h"

class Model{
 protected:
  int _N;
 public:
  Model(int _N, Type* config){ config=new Type[N]; _N=N}
  virtual Type compute();
}
class Model1: public Model{
 protected:
  int _Nr,_Nc;
 public:
  Model1(int Nr, int Nc, Type* config):Model(Nr*Nc,config){_Nr=Nr;_Nc=Nc;}
  virtual Type compute();
}
class Model2: Public Model{
 public:
  Model2(int N,Type* config):Model(N,config){/*other unrelated codes*/}
  virtual Type compute();
}

Model1.cpp

#include "Model.h"

Type Model1::compute(){
/*definition*/
}

Model2.cpp

#include "Model.h"

Type Model2::compute(){
/*definition*/
}

Method.h

#include "Model.h"

void Method(Model* sys);

Method.cpp

#include "Method.h"

void Method(Model* sys){ 
Type a=sys->compute();
/*code*/}

Main.cpp

#include "Method.h"

int main(int argc, char** argv){
  Model* sys=new Model2();
  Method(sys);
  /*code*/
}

I can't find any problems but the compiler keeps complaining "error LNK2019: unresolved external symbol void __cdecl Method(class Model *) referenced in function _main".

I am so frustrated because I am a beginner of this and fail to find out the key. I don't know what can cause this: is it anything wrong in my #define? Or it is because different subclass have functions with the same name(seems it should be OK)? Or there is other bugs? I don't even know to add what tags to this question...

Can anyone help me to figure this out? Thanks a lot!


Thanks for suggestions and I have updated the questions to make sure all the constructors are contained.

Upvotes: 0

Views: 207

Answers (2)

Arnestig
Arnestig

Reputation: 2342

Missing some semicolons in the Model.h I think. You need to end the class definitions with semicolon.

For example:

class Model{
 protected:
  int _N;
 public:
  Model(int _N, Type* config){ config=new Type[N]; _N=N}
  virtual Type compute();
};

Not sure if this is the solution to your problem however. But missing semicolons can bring up all sorts of issues.

Upvotes: 0

zmbq
zmbq

Reputation: 39013

It seems as if Method.cpp is not part of the project, so it's not compiled and the linker can't find Method.

You should add all CPP files to your project - Main, Method, Model1 and Model2. While you're at it, make sure the H files are all there, as well.

Upvotes: 1

Related Questions