merterpam
merterpam

Reputation: 785

Unable match function definition to an existing definition in templated class

I'm sorry if such a question is asked before but i could not find no matter how much I searched (there were similar questions but none of them seemed to work for me).

I am writing a templated class and everything works fine except when I try to overload operator+. I try to overload operator+ two times for different parameters but compiler does not see one of the definitions and gives an eror. Here's the code:

Worker.h (one of my previos homeworks, implemented the problem here since it is easier to see ):

#ifndef _WORKER_H
#define _WORKER_H

#include <string>
#include "Project.h"

using namespace std;

template <class T>
class Worker {

public:
  Worker(const string &, Project &);

  void Work(const int &);
  const Worker & Worker::operator+(const int &); //Problem
  const Worker & Worker::operator+(const string &); //Problem

  string getName() const;
  int getTime() const;
private:
  string myName; //The name of the worker
  Project & myProject; 
  int myTime; //Variable to keep track of how much the worker worked.
};

#include "Worker.cpp"
#endif

and the relevant part of Worker.cpp:

template <class T>
const Worker<T> & Worker<T>::operator+(const int & add)
{
  cout << add;
  return *this;
}

template <class T>
const Worker<T> & Worker<T>::operator+(const string & add) 
{
  cout << add;
  return *this;
}

+ operators are not doing anything right now, the problem is the compiler only sees first declared function (in this case the with the parameter int). There also does not seem to be problem with the functions because if I try to overload only one time, both of them work fine alone. Also I can use them (making the necessary changes) in a non-templated class.

I think it is something simple but since I'm new to templates I could not figure out what the problem was.

Upvotes: 0

Views: 212

Answers (2)

juanchopanza
juanchopanza

Reputation: 227418

There are a few problems with your approach, unrelated to templates.

First your operators would only work for one ordering of operations: Worker<T> + int, and not int + Worker<T>

Second typically you would want to return a new Worker instance, not return this by reference, because A+Bshould not modify A or B.

So what you could do is define non-member operators for the different orderings:

template <typename T>
Worker<T> operator+(int i, const Worker<T>& t) { }

template <typename T>
Worker<T> operator+(const Worker<T>& t, int i) { }

and so on.

Note that for operators that should affect the state of the object, such as +=, *= and so on, the usual approach is to use member operators and return by reference, because in these cases it makes perfect sense.

Upvotes: 2

Andrew Brock
Andrew Brock

Reputation: 1364

2 things here, 1 of which is your issue

The return type of a member function is not affected by the fact that the class is a template, hence the declaration of operator+ returning Worker and the definition returning Worker<T> are different. In the class definition, they should be:

const Worker<T> & operator+(const int &); //Problem
const Worker<T> & operator+(const string &); //Problem

The other thing which is also changed in the above code is you don't need the scope (Worker::) in a class declaration

Upvotes: 1

Related Questions