1

I have seen many related questions to this problem, but after carefully following advice from members, my problem still persists. The code is quite simple. I only have the following header file ("instrument.h"), which contains the base class and the template class:

#include <stdio.h>
#include <string>

using namespace std;

class Instrument
{
public:
    Instrument();
    virtual void print() const = 0;
};

template <class parameter> class Equity : public Instrument
{
public:
    Equity();
    virtual void print() const;
};

Now, in my main function on main.cpp I only do the following:

#include "instrument.h"
#include <iostream>

int main() {

    Equity<double> pb;        
    return 0;
}

Well, I get the very well-known error:

Undefined symbols for architecture x86_64:
  "Equity<double>::Equity()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have already changed in Build Settings the C++ standard library to libstdc++, also to default compiler, and so on. Do I have a problem with my project settings? Is perhaps the template wrongly implemented? I was thinking I should also have a instrument.cpp file, but then again definitions for templates must be kept in the header file so that would probably crash too.

Thanks in advance

1 Answer 1

1

You declared the default constructors for both Instrument and Equity but defined them nowhere.

Alter their definitions appropriately:

public:
    Equity() = default; // Or {} in pre-C++11
//           ^^^^^^^^^

(And equivalently for Instrument)

You can also completely omit the declarations of any default constructors for now since you didn't declare any other constructors in both Equity and Instrument and the default constructors will be generated automatically.

4
  • thanks for your answer, but unfortunately none of those options work, keep getting the same error.
    – Adam
    Commented Oct 30, 2014 at 13:05
  • changed both accordingly, tried even several combinations, still same error.
    – Adam
    Commented Oct 30, 2014 at 13:23
  • @Adam ... that sounds quite impossible. Have you saved the header after the changes? Have you also implemented the derived print function (e.g. {}), just to be sure? You mean to tell me that this code doesn't compile on your machine?
    – Columbo
    Commented Oct 30, 2014 at 13:26
  • it does actually, but I had to replace the "default" by "{}", and also add the {} after the "print" declaration in the Equity class, like what you've done. Cheers for that.
    – Adam
    Commented Oct 30, 2014 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.