2014-10-02 33 views
0

編輯後無法解析的外部錯誤:需要刪除後 - 問題是微不足道的(打字錯誤),不會有任何幫助他人獲取當操作符重載,甚至定義的所有方法

我越來越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 
//.. 
} 

我跑當我嘗試在show()方法中輸出ab對象時,會出現錯誤。

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

回答

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

不一樣

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

的iTunesEntry類中(友法)

而且const不應該被用來作爲你將不得不修改的out對象ostream

+0

我的上帝非常感謝你。我無法捕捉到 – kamoussa 2014-10-02 07:41:26

+2

這是錯誤的'const const ostream&'如果你打算寫信給它... – 2014-10-02 07:41:30

+0

@moussashi我認爲你現在不能刪除它,因爲你有+ ve投票的答案,但是我會投票結束它作爲一個簡單的印刷錯誤。 – P0W 2014-10-02 07:46:09