문제

I'm getting linker errors when using Classes that reference other classes in them.

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MovePattern::~MovePattern(void)" (??1MovePattern@@QAE@XZ) referenced in function "public: __thiscall Enemy::Enemy(int,int,class MovePattern,char)" (??0Enemy@@QAE@HHVMovePattern@@D@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MovePattern::MovePattern(void)" (??0MovePattern@@QAE@XZ) referenced in function "public: __thiscall Enemy::Enemy(int,int,class MovePattern,char)" (??0Enemy@@QAE@HHVMovePattern@@D@Z)

It's from referencing this class:

class MovePattern{
    public: 
        char next;

        MovePattern();
        MovePattern(const MovePattern &old){
            p = old.p;
            pi = 0;
            next = p[0];
            n = p[0];
        }

        MovePattern(char *pattern){
            p = pattern;
            pi = 0;
        next = p[0];
        n = p[0];
        }
        ~MovePattern();

In this class:

class Enemy{
    public:
    Enemy(int a, int b, MovePattern p,char c)
    x = b;
    y = a;

    MovePattern pattern (p);
    symbol = c;

They are currently within the same .cpp file, and MovePattern is above Enemy.

I'm not sure what is going on here

Visual C++ 2010 Express with a blank console project, and I haven't found anything similar to my problem yet, any help would would be appreciated.

도움이 되었습니까?

해결책

You've not defined the default constructor and the destructor, as listed below:

MovePattern();  //default constructor
~MovePattern(); //destructor

You've to define them IF you declare them. Declaration must have definition. Or else you'll get linker error when using them, either implicitly or explicitly.

다른 팁

This linker error is because you haven't compiled/linked the file/code which contains the default constructor (MovePattern::MovePattern()) and destructor(MovePattern::~MovePattern()) definitions.

You should either declare them inline inside the class MovePattern (if you don't really do much in it):

class MovePattern {
public:
  MovePattern () {}
  ~MovePattern () {}
};

Or define them in a seperate .cpp file and compile/link that file with your source:

// MovePattern.cpp
MovePattern::MovePattern ()
{
  //...
}
MovePattern::~MovePattern ()
{
  //...
}

Just remove these two declarations:

MovePattern();
~MovePattern();

You obviously do not need these -- since you defined everything in a single cpp file.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top