I have following scenario ..
(sorry for the long (& detailed code presentation), but not sure how to explain this problem otherwise)
--- misc.cpp ---
template <typename T>
const T& mymax(const T &a, const T &b) {
return a > b ? a : b;
}
--- misc.h ---
#ifndef __MISC_H__
#define __MISC_H__
template <typename T>
const T& mymax(const T &a, const T &b);
#endif /* __MISC_H__ */
--- test.cpp ---
include <iostream>
#include "A.h"
#include "misc.h"
using namespace std;
int main(int argc, char *arg[]) {
A turtle {"Turtle", 70}, hbird{"Humming bird", 47};
turtle.show();
hbird.show();
A animal = mymax<A>(turtle, hbird);
animal.show();
return 0;
}
--- A.h ---
#ifndef __A_H__
#define __A_H__
#include <iostream> // for std::ostream
class A {
friend std::ostream& operator<<(std::ostream &, const A&);
private:
char *str;
int i;
public:
A(const char *, int); // Overloaded constuctor
A(); // No-args constructor
~A(); // destructor
A(const A&); // copy constructor
A(A&&); // move constructor
A& operator=(const A&); // copy assignment
A& operator=(A&&); // move assignment
bool operator>(const A&) const;
void setint(int i=0);
void show(); // display
};
#endif /* __A_H__ */
A implements operator> like this --
--- A.cpp ---
bool A::operator>(const A& rhs) const {
if (i > rhs.i)
return true;
return false;
}
Now if I build it, it is giving me below error:
make
g++ -g -std=c++14 -Wall -c A.cpp
g++ -g -std=c++14 -Wall -c misc.cpp
g++ -g -std=c++14 -Wall -c test.cpp
g++ -g -std=c++14 -Wall A.o misc.o test.o -o test
/usr/bin/ld: test.o: in function `main':
/home/onkar/cc/learncpp/stl/test.cpp:23: undefined reference to `A const& mymax<A>(A const&, A const&)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:5: test] Error 1
Not sure what's wrong.
Can anyone, please help me figure this out.
Aucun commentaire:
Enregistrer un commentaire