0

EDIT: need to delete post -- problem was trivial (typographical error) and won't be of any help to others

I'm getting errorLNK2019, the unresolved external error, when trying to use an operator overload in one of my cpp files. I've looked all over, and many people have been able to fix the problem by making sure to define every single method in their class prototypes.

I think this has to do a lot with my project design to be honest, but I really can't pinpoint exactly why this error is happening

Here's the code:

//a.cpp
//
// ... skipped all code to bottom to where i modified it
//OVERLOADED FUNCTIONS
int operator+(const int n, const a& entry){
   return n + entry.getTime();
}
ostream& operator<<(ostream & out, const a& entry){
   out << entry.getTitle() << " by " << entry.getArtist()
      << " (" << entry.getTime() << ") ";
   return out;
}

//*********************************************************
// a.h
//... only posting what I changed
//
// Inside the class..

class a
{
public:
   friend ostream& operator<<(const ostream& out, const a& entry);
   friend int operator+(const int n, const a& entry);
//..
//.. SNIPPED
//..
}

I run into the error when I try to output a b object in the show() method.

//b.cpp
#include "b.h"

b b::etnsl(const int &indexOfItemToAdd) const{ 
   if (originalObjects != NULL && indexOfItemToAdd >= (*originalObjects).size()){
      throw "Index out of bounds";
   }
   b x(originalObjects);
   vector<int> *iCopy = x.getIndices();
   (*iCopy) = indices;
   iCopy->push_back(indexOfItemToAdd);
   x.setSum(sum + (*originalObjects)[indexOfItemToAdd].getTime());
   return x;
}

void b::show() const{
   cout << "    Item at loc " << "0x" << this << ":" << endl;
   //int j = indices.size();
   //if (j == 0)
   if (size == 0)
      cout << "    Empty item." << endl;
   else{
      for (int i = 0; i < size; i++) //ERROR IN LOOP
         cout << "    Index " << indices[i] << " : " << (*originalObjects)[indices[i]] << endl;
   }
}
int b::getSum() const{
   return sum;
}
void b::setSum(const int& num){
   sum = num;
}
vector<int>* b::getIndices(){
   return &indices;
}

//*********************************************************
//b header class

#ifndef B_H
#define B_H

#include <iostream>
#include <vector>
#include "a.h"
using namespace std;

class b{
private:
   int sum, size;
   vector <a> *originalObjects;
   vector <int> indices;
public:
   b(vector<a> *orig = NULL) //counts as 2 constructors: default and a custom one.
      : sum(0), originalObjects(orig), size(indices.size()) {
   }
   b etnsl(const int &indexOfItemToAdd) const; 
   void show() const;
   int getSum() const;
   void setSum(const int& num);
   vector<int> *getIndices();
};

#endif
kamoussa
  • 49
  • 1
  • 9

1 Answers1

2
ostream& operator<<(ostream & out, const iTunesEntry& tune)

is not same as

ostream& operator<<(const ostream& out, const iTunesEntry& entry);
//                  ~~~~~

inside the iTunesEntry class (friend method)

And const shouldn't be used as you will have to modify the out object of ostream

P0W
  • 46,614
  • 9
  • 72
  • 119
  • @moussashi I think you cannot delete it now, since you got an answer with +ve votes, however I'll vote for closing it as a simple typographical error. – P0W Oct 02 '14 at 07:46