2

I'm new to C++, and I'm having some trouble setting up a simple class reference.

Class: Test.hh

#ifndef _TEST_HH_
#define _TEST_HH_

class Test {
    public:
        Test (double x);
};

#endif

Class Test.cc

#include "Test.hh"
#include <stdio.h>

Test::Test(double x) {
   printf("%f",x);
}

Now I want to access this class from another class:

Class: DriverClass.hh

#ifndef _DRIVERCLASS_HH_
#define _DRIVERCLASS_HH_

#include "Test.hh"

class DriverClass {
    public:
        DriverClass(double y);
        Test *t;
}

#endif

Class DriverClass.cc

#include "DriverClass.hh"

DriverClass::DriverClass(double y) {
    t = new Test(y);
}

However, I get an error: "undefined reference to 'Test::Test(double)?

Does anyone know what might be wrong? Please assume that DriverClass is being called directly from a main method (not shown).

comp_sci5050
  • 145
  • 2
  • 11
  • 1
    because you're not linking both compiled objects – UKMonkey Oct 06 '16 at 15:45
  • Can you show us how you called the compiler (full command line)? – mindriot Oct 06 '16 at 15:45
  • `#include "DriverClasss.hh"` - triple `s`? Also `#ifndef`s are not closed. – Ruslan Osmanov Oct 06 '16 at 15:46
  • Sorry the third "s" and the #endif were just errors when I was copying my code over to stackoverflow. The problem's still the same – comp_sci5050 Oct 06 '16 at 15:50
  • @UKMonkey can you explain? Not sure I follow – comp_sci5050 Oct 06 '16 at 15:52
  • @comp_sci5050 either compile all of your cpp files at once (`g++ *.cpp`) or compile them separately to object files and then link them together(pretty sure you can find out how if you use the search function) – Alex Díaz Oct 06 '16 at 15:59
  • @comp_sci from http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work At the the linking stage, you're failing to get all your object files included; meaning that your linker knows that the class should exist, and that it should have functions (from the fact that you used it) - but doesn't have the information about the implementation of the functions – UKMonkey Oct 06 '16 at 16:04

1 Answers1

2

There is still an error in your post - a missing ; after the DriverClass declaration. The rest is correct.

You should compile and link all the sources. The following is a sample Makefile and a sample test code.

Makefile

all: t

t: t.cc DriverClass.cc Test.cc
    g++ -Wall -g -o $@ $^

clean:
    rm -f *.o t

However, note that it's generally recommended to compile the sources into objects separately in order to compile only the sources changed after the last compilation. For example:

CFLAGS=-Wall -g

all: t

t: t.o DriverClass.o Test.o
    g++ -o $@ $^

t.o: t.cc DriverClass.o Test.o
    g++ $(CFLAGS) -c $< -o $@

DriverClass.o: DriverClass.cc
    g++ $(CFLAGS) -c $< -o $@

Test.o: Test.cc
    g++ $(CFLAGS) -c $^ -o $@

clean:
    rm -f *.o t

I've used the GNU compiler. For the meaning of $@ and $^ variables refer to the official documentation.

t.cc

#include "Test.hh"
#include "DriverClass.hh"

int main(int argc, char const* argv[])
{
  DriverClass d(10.4);
  return 0;
}

Testing

$ make
g++ -Wall -g -o t t.cc DriverClass.cc Test.cc
$ ./t
10.400000

P.S.: don't forget to delete the allocated object.

Ruslan Osmanov
  • 20,486
  • 7
  • 46
  • 60