0

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.

4

2 回答 2

1

似乎 Method.cpp 不是项目的一部分,所以它没有被编译并且链接器找不到Method.

您应该将所有 CPP 文件添加到您的项目中 - Main、Method、Model1 和 Model2。当您使用它时,请确保 H 文件也都在那里。

于 2012-07-17T08:51:47.980 回答
0

我认为在 Model.h 中缺少一些分号。您需要用分号结束类定义。

例如:

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

但是,不确定这是否可以解决您的问题。但是缺少分号会带来各种各样的问题。

于 2012-07-17T09:21:51.577 回答