-2

So I have some .cc and .h files.

// Class.cc
#include "Class.h"
Class::Class(){
    this->x = 1;
    this->y = new HelpingClass[1];
    y[0] = HelpingObject{};  // Calls the HelpingObject's default ctor
}

So this is my main .cc file where I have created a constructor for my Class, which is defined in my Class.h file:

// Class.h
#include "HelpingClass.h"
struct Class{
    HelpingObject * y; // heap allocated array of HelpingObject
    int x;     // size of allocated array
    Class(); // The constructor
    ...
}

This "HelpingObject" is included in my HelpingClass.h with a working HelpingClass.cc also in my VScode file (I know the HelpingClass implementation works well already so I'm ommitting HelpingClass.cc):

// HelpingClass.h
struct HelpingObject {
  int a;
  int b;
  HelpingObject(int a = 0, int b = 10);
}
// Doesn't really matter what this object is, but my "Class" has a field
// which is a heap allocated array of these objects

The problem is, I'm getting and error that says "Class.cc:(.text+0x43): undefined reference to `HelpingObject::HelpingObject(int, int)' collect2.exe: error: ld returned 1 exit status"

What is ths? I'm using VSCode if that helps, but even if I make a Makefile with an executable with all these files in linux, I get the same error. Please help

edit: I changed the name of my actual code to these general names to make it easier but I messed up in copying it over to here haha. The syntax is all correct in my code, my implementation isnt'

ming
  • 229
  • 1
  • 9
  • 1
    You are not linking your object files properly. – Jesper Juhl Nov 03 '19 at 18:16
  • You probably don't build with all the relevant source files, or all the object files generated from those source files? – Some programmer dude Nov 03 '19 at 18:16
  • 2
    `Class:Class()` should be `Class::Class()`. – Daniel Langr Nov 03 '19 at 18:16
  • 1
    By the way, why is `y` a pointer? Why do you use array allocation for it? And please stop using one-letter variables, even in a [mcve] use nice descriptive names. – Some programmer dude Nov 03 '19 at 18:17
  • 1
    I'm keeping with that dup - https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix ... If that doesn't make sense - I would suggest you point out which bits you didn't understand – UKMonkey Nov 03 '19 at 18:17
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Raymond Chen Nov 03 '19 at 18:20
  • 1
    Looking a t the error message, I suppose that `Rational` was to be understood as `HelpingObject`? Could you post the real code ? – Christophe Nov 03 '19 at 18:20
  • @Christophe Yes whoops it's supposed to be HelpingObject, not Rational, I fixed it – ming Nov 03 '19 at 18:38
  • @DanielLangr Fixed, I just copied over the code incorrectly – ming Nov 03 '19 at 18:38
  • @Someprogrammerdude It's just supposed to be a heap allocated array? I'm not sure what the question is here – ming Nov 03 '19 at 18:39
  • @JesperJuhl How so? – ming Nov 03 '19 at 18:39
  • The error message shows that the constructor for the HelpingObject is correctly invoked but is not found by the loader. Since you have no other constructor and your HelpingObject.cc was already tested, the error most probably comes from HelpingObject.o is not takein into account by the linker. Try to compile Class.cc and HelpingObject.cc in a single command. – Christophe Nov 03 '19 at 19:06
  • So it is a problem with how I'm linking them, there's nothing wrong with my actual code (The constructor mainly?) @Christophe – ming Nov 03 '19 at 19:07
  • @ming yes. At least for the error for which you asked this question. – Christophe Nov 03 '19 at 19:10

2 Answers2

0

You have made a few mistakes here.

  1. Class:Class needs an extra colon; right now, it's a goto label followed by the definition of a free function missing a return type. The parser's not going to know what to do with that.
  2. You named the constructor of HelpingClass .. Rational. It should be HelpingClass.

These bugs have confused the heck out of your toolchain, which cannot find any of your constructor definitions!

Also, you should avoid manual memory management. There's no need for it. Get yourself a nice vector or unique_ptr.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

You have to add a semicolon to the end of each class declaration.

For instance,

struct Class
{
    ...
}

should be

struct Class
{
    ...
};

Also, you have to provide an implementation of HelpingObject::HelpingObject(int a, int b)

Sid S
  • 6,037
  • 2
  • 18
  • 24