0

I am not an expert of C++ so please, forgive my question. I am trying to create a class with a member that could be of any value. Something like this:

class Input {

private:
    string         name;
    AnyValue      value;

}

where AnyValue may be: int, float, string. I came up with this:

.h

template <typename Value>
class Input {

private:
    string name;
    Value  value;

public:

    Input<Value>(string name);

}

.cpp

template <typename Value>
Input<Value>::Input(string name) {

    this->name = name;
    this->value = 0;
}                             

template <typename Value>
void Input<Value>::setValue(Value value) {
   this->value = value;
}

template <typename Value>
Value Input<Value>::getValue(void) {

   return value;
}

main is

int main(int argc, const char * argv[]) {

   Input<int> i1 = Input<int>("i1");
}

Problem is that I get this error message from the linker:

Undefined symbols for architecture x86_64:
"Input<int>::Input(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
  _main in main.o

but I cannot understand what I am missing.

Fab
  • 1,468
  • 1
  • 16
  • 37
  • 1
    @ πάντα ῥεῖ: Thank you, I didn't know that in this case I had to implement methods in the .h file. Doing this, it works! – Fab Feb 11 '16 at 18:11
  • _@Fabrizio_ De nada. I see that question asked several times a week. It's rare that people appreciate their questions being closed as duplicates. Have an upvote for that. – πάντα ῥεῖ Feb 11 '16 at 18:23

0 Answers0