2014-10-20 77 views
0

無法使< <過載工作,並且無法通過節點使用'getter'方法打印'Account'實例。不完全確定要提供哪些附加信息,請僅僅詢問我沒有說明的其他信息! 我的操作符重載有很明顯的錯誤,但我不確定它是什麼。 我,呃......可能會在每個代碼的起始位置加倍。模板和輸出運算符超載

Account.cpp

#include "Account.h" 
    namespace myNamespace{ 
    // CONSTRUCTOR 
    Account::Account(int initialValue) 
    { 
     acct_balance = initialValue; 
    } 
    //MUTATOR MEMBER FUNCTION 
    void Account::deposit(int amount) 
    { 
     acct_balance += amount; 
    } 


    void Account::withdraw(int amount) 
    { 
     acct_balance -= amount; 
    } 


    void Account::setName(std::string newName) 
    { 
     name = newName; 
    } 
    //QUERY MEMBER FUNCTION 
    int Account::balance() const 
    { 
     return acct_balance; 
    } 


    bool Account::has_funds() const 
    { 
     return (acct_balance > 0.0); 
    } 


    std::string Account::getName() 
    { 
     return name; 
    } 


    int& operator += (Account& a,const Account& b) 
    { 
     a.acct_balance += b.balance(); 
     return a.acct_balance; 
    } 


    std::ostream& operator << (std::ostream& out, const Account& a) 
    { 
     out << a.acct_balance; 
    } 
    }; 

的main.cpp

#include <iostream> 
    #include <string> 
    #include "Account.h" 
    #include "Node.h" 




    using namespace myNamespace; 
    using namespace std; 


    int main() 
    { 
    Node<int>* intNode = new Node<int>(5); 
    Node<std::string>* stringNode = new Node<std::string>("hello"); 
    Account* acc1 = new Account(); 
    Node<Account>* accountNode = new Node<Account>(*acc1); 



    cout << "Initialised intNode with " << (intNode)->getData() << " ,\n"; 
    cout << "Initialised stringNode with " << stringNode->getData() << " ,\n"; 
    cout << "Intiialised accountNode with $" << accountNode << "." << endl; 
    return 0; 
    } 


    /* 
    Modify the Node class you produced in the last few weeks so that it becomes 
    a class template for a Node that can store a pointer to a generic type 
    instance. The constructor for your new Node returns a pointer to the newlycreated 
    Node instance, which has been created in the heap. Instantiate a 
    Node to store a pointer to an int; a pointer to a string; and a pointer to 
    an Account. 
    */ 

Node.h

#ifndef NODE_H 
    #define NODE_H 
    #include <iostream> 
    using namespace std; 


    namespace myNamespace{ 
    { 
    template <class T> 
    class Node 
    { 
    public: 
     Node(T const& initData, Node<T>* const& initPrev = NULL, Node<T>* const& initNext = NULL); 
     void setData(T newData); 
     T getData(); 
     void setPrev(Node<T>* newPrev); 
     Node<T>* getPrev(); 
     void setNext(Node<T>* newNext); 
     Node<T>* getNext(); 


     friend std::ostream& operator << (std::ostream& out, const Node<T>& a); 
    private: 
     T data; 
     Node<T> *next; 
     Node<T> *prev; 
    }; 
    #include "Node.template" 
    } 
    #endif 

Node.template

template <class T> 
    Node<T>::Node(T const& initData, Node<T>* const& initPrev, Node<T>* const& initNext) 
    { 
    data = initData; 
    prev = initPrev; 
    next = initNext; 
    } 


    template <class T> 
    void Node<T>::setData(T newData) 
    { 
    data = newData; 
    } 


    template <class T> 
    T Node<T>::getData() 
    { 
    return data; 
    } 


    template <class T> 
    void Node<T>::setPrev(Node<T>* newPrev) 
    { 
    prev = newPrev; 
    } 


    template <class T> 
    Node<T>* Node<T>::getPrev() 
    { 
    return prev; 
    } 


    template <class T> 
    void Node<T>::setNext(Node<T>* newNext) 
    { 
    next = newNext; 
    } 


    template <class T> 
    Node<T>* Node<T>::getNext() 
    { 
    return next; 
    } 


    template <class T> 
    std::ostream& operator << (std::ostream& out, const Node<T>& a) 
    { 
    out << a->getData(); 
    return out; 
    } 

當我把它打印出來使用

cout << "Initialised intNode with " << (intNode) << " ,\n"; 
cout << "Initialised stringNode with " << (stringNode) << " ,\n"; 
cout << "Intiialised accountNode with $" << (accountNode) << "." << endl; 

輸出是

Initialised intNode with 0x7f9251c03950 , 
Initialised stringNode with 0x7f9251c03970 , 
Intiialised accountNode with $0x7f9251c039c0. 

提領時,這裏是錯誤

Undefined symbols for architecture x86_64: 
    "law_lab07::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, law_lab07::Node<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const&)", referenced from: 
     _main in main.o 
    "law_lab07::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, law_lab07::Node<law_lab07::Account> const&)", referenced from: 
     _main in main.o 
    "law_lab07::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, law_lab07::Node<int> const&)", referenced from: 
     _main in main.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
make: *** [runfile] Error 1 
+0

發佈錯誤將有所幫助。但是從我所看到的你在cpp文件中實現模板時,他們應該在頭文件中。 – 0x499602D2 2014-10-20 04:47:53

+0

在發佈錯誤時,可能會修復您的插件運算符;它缺少一個返回引用。我想你會發現[**這個答案類似的問題**](http://stackoverflow.com/questions/4660123/overloading-friend-operator-for-template-class/4661372#4661372)相當-很有幫助。 (答案是恆星,順便說一句)。 – WhozCraig 2014-10-20 04:50:16

+0

插入器的聲明也需要成爲模板。 – 0x499602D2 2014-10-20 04:53:01

回答

1

首先,始終使用警告進行編譯。海灣合作委員會給了我很好的提示,我相信克朗也會這樣做。

您的操作員定義有問題,因爲它是非模板的朋友。請看看http://en.cppreference.com/w/cpp/language/friend

解決方法是如下:

namespace myNamespace{ 

template <class T> 
class Node 
{ 
public: 
    Node(T const& initData, Node<T>* const& initPrev = NULL, Node<T>* const& initNext = NULL); 
    void setData(T newData); 
    T getData() const; 
    void setPrev(Node<T>* newPrev); 
    Node<T>* getPrev(); 
    void setNext(Node<T>* newNext); 
    Node<T>* getNext(); 

    friend std::ostream& operator<< (std::ostream& out, const Node& a) 
       { 
       return out << a.getData(); 
       } 

private: 
    T data; 
    Node<T> *next; 
    Node<T> *prev; 
}; 
#include "Node.template" 
} 
#endif 

請注意,你需要讓你的getData()方法一個const一個編譯。

如之前由其他人則建議也不要忘了取消引用的主要指針:

cout << "Initialised intNode with " << (intNode)->getData() << " ,\n"; 
cout << "Initialised stringNode with " << stringNode->getData() << " ,\n"; 
cout << "Intiialised accountNode with $" << *accountNode << "." << endl; 

這是Account.h文件我做:

using namespace std; 

namespace myNamespace{ 
class Account 
{ 
    public: 
     Account(){} 
     Account(int initialValue); 
     void deposit(int amount); 
     void withdraw(int amount); 
     void setName(std::string newName); 
     int balance() const; 
     bool has_funds() const; 
     std::string getName(); 
     friend int& operator += (Account& a,const Account& b); 
     friend std::ostream& operator << (std::ostream& out, const Account& a); 

    protected: 
    private: 
     int acct_balance; 
     string name; 
}; 
} 
#endif // ACCOUNT_H 

還要添加運營商的「退貨」< <賬戶實施

+0

我不確定問題出在哪裏,但我得到了Initialised stringNode與你好,分段錯誤:11 fOrceez 2014-10-20 07:34:19

+0

你可以嘗試用運算符<<實現中的「測試\ n」字符串替換a.getData()嗎?我與我編譯的代碼沒有任何問題。 – Lectem 2014-10-20 07:37:40

+0

這工作正常!必須是我的「帳戶」類的東西? – fOrceez 2014-10-20 07:41:47

0

我認爲問題出在cout << "Intiialised accountNode with $" << accountNode << "." << endl;。您正嘗試打印一個Node<Account>*而不是Node<Account>,您沒有過載。

+0

對。對不起,我應該澄清,即使我解除引用我的指針,我也無法使用overloader打印它們:/我將用我的錯誤編輯主帖子 – fOrceez 2014-10-20 05:55:22