Получение неразрешенной внешней ошибки при перегрузке оператора даже после определения всех методов

РЕДАКТИРОВАТЬ: необходимо удалить сообщение - проблема была тривиальной (опечатка) и не будет никакой помощи другим

Я получаю errorLNK2019, неразрешенную внешнюю ошибку, при попытке использовать перегрузку оператора в одном из моих файлов cpp. Я просмотрел все, и многие люди смогли решить эту проблему, определив каждый метод в своих прототипах.

Честно говоря, я думаю, что это во многом связано с дизайном моего проекта, но я действительно не могу точно определить, почему происходит эта ошибка.

Вот код:

//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
//..
}

Я сталкиваюсь с ошибкой при попытке вывести объект ab в методе show().

//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

1 ответ

Решение
ostream& operator<<(ostream & out, const iTunesEntry& tune)

не то же самое, что

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

inside the iTunesEntry class (friend method)

А также const shouldn't be used as you will have to modify the out объект ostream

Другие вопросы по тегам