E
E
Egor2016-02-21 14:10:55
C++ / C#
Egor, 2016-02-21 14:10:55

Undefined reference to?

Good afternoon! I get the following compiler errors:

/tmp/cc6E8ltA.o: In function `main':
main.cpp:(.text+0x24): undefined reference to `Hero::view(int)'
main.cpp:(.text+0x30): undefined reference to `Hero::show()'
main.cpp:(.text+0x41): undefined reference to `Hero::view(int)'
main.cpp:(.text+0x4d): undefined reference to `Hero::show()'
collect2: error: ld returned 1 exit status

main.cpp
#include <iostream>
#include "classes.h"

int main (void)
{
  Hero n1;
  n1.view (1);
  n1.show ();
  Hero n2;
  n2.view (2);
  n2.show ();
  return 0;
}

classes.h
class Hero{
  private:
    int life_;

    char view_;
  public:
    void view (int); 
    void show (void);
};

methods.cpp
#include "classes.h"
#include <cstring>

void Hero::view (int value)
{
  if (value == 1)
  {
    strcpy (view_, "Hunter");
    life_ = 10000;
  }
  if (value == 2)
  {
    strcpy (view_, "Soldier");
    life_ = 600;
  }
}

void Hero::show ()
{
  std::cout << "View: " << view_ << std::endl
            << "Life: " << life_ << std::endl;
}

What's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2016-02-21
@Nemovok

The project did not include the methods.cpp file. Because the linker says that there are no these functions.
The C language (as, indeed, most assemblers) assembles cpp-files together with non-language means: makefiles and project files. First, the compiler processes all compilation units separately from each other, and then the linker assembles the executable file from what happened. Of course, if some function was not found somewhere, it can only be identified when linking.

V
Vladimir Martyanov, 2016-02-21
@vilgeforce

The input linker did not receive an object with the implementation of the specified methods. Either received, but there is some garbage with names. According to the data given, it’s not more accurate to say, you need to know exactly how you collect all this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question