سؤال

I'm still kind of new to C++ and don't know why I'm getting these linker errors while trying trying to call these functions in another class.

The errors are:

error LNK2019: unresolved external symbol "public: float __thiscall Star::getMass(void)" (?getMass@Star@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update@Projectile@@QAEXQAVStar@@H@Z)

error LNK2019: unresolved external symbol "public: float __thiscall Star::getX(void)" (?getX@Star@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update@Projectile@@QAEXQAVStar@@H@Z)

error LNK2019: unresolved external symbol "public: float __thiscall Star::getY(void)" (?getY@Star@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" (?Update@Projectile@@QAEXQAVStar@@H@Z)

Projectile.cpp:

#include <hge.h>
#include "Projectile.h"
#include "Physics.h"
#include "Star.h"
#include <math.h>

Projectile::Projectile(float xV, float yV, float x, float y, float m, HTEXTURE tex)
{
    xVel = xV;
    yVel = yV;
    xPos = x;
    yPos = y;
    mass = m;
    quad.tex = tex;
}

void Projectile::Update(Star stars[], int length)
{
    for(int i = 0; i<length; ++i)
    {
        float force = Physics::calcGravityForce(mass, stars[i].getMass(), Physics::calcDist(xPos, yPos, stars[i].getX(), stars[i].getY()));
        Accelerate(force, stars[i].getX() - xPos, stars[i].getY() - yPos);
    }
}

void Projectile::Accelerate(float force, float x, float y)
{
    float c = sqrt((x * x) + (y * y));
    xVel += x/c;
    yVel += y/c;
}

Star is defined in Star.h here:

#ifndef STAR_H
#define STAR_H

#include <hge.h>

class Star
{
private:
    float mass, radius, x, y;
    hgeQuad quad;

public:
    Star(float m, float r, float X, float Y, HTEXTURE);
    float getMass();
    float getRadius();
    float getX();
    float getY();
    Star() {}
};

#endif
هل كانت مفيدة؟

المحلول

You have several functions declared in the Star class:

Star(float m, float r, float X, float Y, HTEXTURE);
float getMass();
float getRadius();
float getX();
float getY();

And you are trying to use some of them without providing a definition, that is to say, the body of the function, which is why you're getting those linker errors.

Add a new .cpp file to your project named Star.cpp (the name doesn't matter though) and add the definitions of the functions for the Star class, like you've done for the Projectile class. (You could just add them to any .cpp file in your project, like Projectile.cpp, but if you have a separate header file, it's good to have a seperate .cpp file too.)

Or if you don't want to have another cpp file in your project, you can put the bodies of the functions inside the class itself:

class Star
{
private:
    float mass, radius, x, y;
    hgeQuad quad;

public:
    Star(float m, float r, float X, float Y, HTEXTURE);
    float getMass() { return mass; }
    float getRadius() { return radius; }
    float getX() { return x; }
    float getY() { return y; }
    Star() {}
};

That style is common for small "getter" functions like getMass, getRadius, etc. which just return a member variable.


Though it's not directly related to your question, I should point out a few things:

  1. Make all your "getter" functions (like getMass etc) const (so that they can be used on const Star objects) by putting the word const after the parameters (the () in this case) like this: float getMass() const { return mass; }

  2. Because you have member variables in the Star class, you should set them to some sensible default value in the constructor which takes no parameters,

like this:

Star() : mass(0), radius(0), x(0), y(0) {}

Which will set mass, radius, x and y to 0. (This unusual syntax is called an initialiser list. You can read about them here.)

You can even do this without a seperate constructor by using default arguments:

Star(float m = 0, float r = 0, float X = 0, float Y = 0, HTEXTURE = 0);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top