Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top