-3

Currently trying to use vectors to store bullets in a game. when i attempt to use push_back to add a new object to the list i get an unresolved external symbol error. I've tried changing it to an integer and the vector works so i doubt its a linker error? my vector is initialised in my main CPP and is then passed a few times to this the function within a separate class.

Here's my code:

void Hero::shoot(std::vector<Bullet> bullets)
{
    Bullet firedBullet();
    bullets.push_back(firedBullet());
}

My error is:

LNK2019 unresolved external symbol "class Bullet __cdecl firedBullet(void)" (?firedBullet@@YA?AVBullet@@XZ) referenced in function "public: void __thiscall Hero::shoot(class std::vector >)" (?shoot@Hero@@QAEXV?$vector@VBullet@@V?$allocator@VBullet@@@std@@@std@@@Z) Project1 H:\C++\Project1\Project1\Hero.obj

Please Help.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
Pyrous
  • 1
  • 1
  • 1
    "i doubt its a linker error" unresolved references are always linker errors. –  Apr 22 '17 at 17:40
  • Of course it's a linker error. The linker is complaining after all. You didn't provide enough code to figure out the exact cause, so please provide a [mcve]. Having said that, this question is probably a duplicate of http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Rakete1111 Apr 22 '17 at 17:41
  • 1
    `Bullet firedBullet();` -- This does not do what you think it does. It does not declare an instance of `Bullet`. I bet the linker is complaining about your bogus `fireBullet()` function. Why not post the linker error? – PaulMcKenzie Apr 22 '17 at 17:44
  • 1
    Most vexing parse, `Bullet firedBullet();` forward declares a function called `firedBullet` that returns a `Bullet` and takes no arguments. Then in `bullets.push_back(firedBullet());` the compiler thinks you're calling that function but the linker can't find a definition for it. – Unimportant Apr 22 '17 at 17:46
  • Excuse me thus us my first post still getting getting to grips with how to pose my questions etc. By linker error I meant an I messed up a an external depency link sorry for being so unclear. Will check out the linked post. – Pyrous Apr 22 '17 at 17:49
  • @Pyrous The linker emitted an error to you -- just copy and paste that error message in your post. Right now you got an answer based on wrong C++ coding that would cause a certain linker error, but we don't know for sure until you post that error. – PaulMcKenzie Apr 22 '17 at 17:55
  • @Pyrous -- As you can see, the linker error mentions `firedBullet`, just as the comments and answers suggested. – PaulMcKenzie Apr 22 '17 at 18:40
  • @PaulMcKenzie thanks, I'll make sure to include in future posts. – Pyrous Apr 22 '17 at 18:41

1 Answers1

1

That's because you have declared and called a function that has no definition.

Bullet firedBullet();

declares a function that takes no parameters and returns a Bullet.

bullets.push_back(firedBullet());

calls that function and thus you get a linker error.
(I suspect that you added the parentheses on firedBulletto make it compile?)

It should be

Bullet firedBullet;
bullets.push_back(firedBullet);

or

bullets.push_back(Bullet());

or

bullets.emplace_back();
molbdnilo
  • 64,751
  • 3
  • 43
  • 82