-3

I've got message: undefined reference to AVector::AVector() on the string class AInv { in the following code:

#ifndef AINV_HPP_
#define AINV_HPP_
#include "AVector.hpp"
class AInv {
    public:
        double w;
        double g;
        AVector vector;
};
#endif /* AINV_HPP_ */

definition of AVector:

#ifndef AVECTOR_HPP_
#define AVECTOR_HPP_

class AVector {
    public:
        double x;
        double y;
        double z;

        AVector();
        AVector(const AVector &v);
};
#endif /* AVECTOR_HPP_ */

Is it possible to use class variable (not address) as a class member? I'd like to avoid explicit constructor / destructor for the variable vector.

manlio
  • 18,345
  • 14
  • 76
  • 126
ABC
  • 9
  • 2
  • 3
    The error `undefined reference` is a link error. You probably don't have a definition for the default constructor for `AVector` or it is not being linked in – Niall Nov 28 '14 at 12:48
  • Thanks to Niall! I do skip AVector.o in the makefile. The problem is solved! – ABC Nov 28 '14 at 13:06

1 Answers1

1

Is it possible to use class variable (not address) as a class member?

Yes, it is.

I'd like to avoid explicit constructor / destructor for the variable vector.

There's no need for an explicit destructor; the destructor for each member of class type will be called automatically by the AInv destructor, whether you write that yourself or leave the compiler to generate an implicit destructor.

To avoid the need for an explicit constructor call, the member type must have an accessible default constructor. You've declared one, so that's fine.

However, the error message indicates that you forgot to define the default constructor, or didn't link with the file that contains the definition.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644