0

Background: I have a larger program where I decided to practice vectors by creating a particle emitter. So i created a class called Particle that did the Particle stuff, then a class called ParticleEmitter to handle the particles. The plan being that at it's core ParticleEmitter would have a vector of Particels, since this was an excercise to learn how to use vectors. Problem: It does not compile. I get a Linking error.

Error 1 error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: class Particle & __thiscall std::vectorParticle,class std::allocator >::operator[](unsigned int)" (??A?$vector@VParticle@@V?$allocator@VParticle@@@std@@@std@@QAEAAVParticle@@I@Z) ...\projects\MyExperiment\Main.obj MyExperiment

The original error made me try to just strip down to some dummy classes declared and defined in before Main.

#include <vector>

class Particle {
private:
    double xPos_;
    double yPos_;

public:
    Particle();
    void setMe(double a, double b);
};

Particle::Particle() {
    xPos_ = 0;
    yPos_ = 0;
}

void Particle::setMe(double a, double b) {
    xPos_ = a;
    yPos_ = b;
}

class ParticleEmitter {
private:
    ParticleEmitter();
    std::vector<Particle> particleArray_;

public:
    void Populate();
    void Update();
};

ParticleEmitter::ParticleEmitter() {
    particleArray_.reserve(5);
}

void ParticleEmitter::Populate()  {
    Particle newParticle;
    newParticle.setMe(23, 45);
    particleArray_.push_back(newParticle);
    particleArray_.push_back(newParticle);
}

void ParticleEmitter::Update()  {
    for (std::vector<Particle>::size_type i = 0; i < particleArray_.size(); i++)
    {
        particleArray_[i].setMe(i, i + 1);
    }
}

The trigger for the error is the last line: particleArray_[i].setMe(i, i + 1); If I comment out this it compiles fine. The rub is that if I copy only this barebones code to a new project, it compiles fine.

In my main project,I have no #includes that should interfere with this, only <iostream> and "SDL.h". There are also no other variables etc with similar names.

Solutions tried: 3 different ways of accessing the vector:

    for (std::vector<Particle>::size_type i = 0; i != particleArray_.size(); i++)
        {
            particleArray_[i].SetxVel(particleArray_[i].GetxVel() + (GRAVITY_CONSTANT * timeSlice) / 1000);
            // Other stuff
        }

    for (std::vector<Particle>::size_type i = 0; i != particleArray_.size(); i++)
    {
        Particle& pA = particleArray_[i];
        pA.SetxVel(pA.GetxVel() + (GRAVITY_CONSTANT * timeSlice) / 1000);
             // Other stuff

    }

    for (std::vector<Particle>::iterator it = particleArray_.begin(); it != particleArray_.end(); ++it)
    {
        it->SetxVel(it->GetxVel() + (GRAVITY_CONSTANT * timeSlice) / 1000);
        // Other stuff
    }

This is from original code where GetxVel and SetxVel are public functions from the Particle class. Common to all three ways to access the vector is that it fails to compile due to linking error. In the original code, the destructor is also defined/declared (empty).

So the real question is: Am I doing something wrong? And why does it give linking errors? Any insights or further reading are most welcome. I have banged my head and Google against this for many days now...

Mark
  • 55
  • 5
  • 2
    Check this SO thread which reports the same issue (http://stackoverflow.com/questions/11919836/unresolved-external). – Mahesh Oct 14 '15 at 21:32
  • I did not see this article. And sure enough it was the same issue causing my woes. Thanks! – Mark Oct 14 '15 at 21:52

0 Answers0