無法使< <過載工作,並且無法通過節點使用'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
發佈錯誤將有所幫助。但是從我所看到的你在cpp文件中實現模板時,他們應該在頭文件中。 – 0x499602D2 2014-10-20 04:47:53
在發佈錯誤時,可能會修復您的插件運算符;它缺少一個返回引用。我想你會發現[**這個答案類似的問題**](http://stackoverflow.com/questions/4660123/overloading-friend-operator-for-template-class/4661372#4661372)相當-很有幫助。 (答案是恆星,順便說一句)。 – WhozCraig 2014-10-20 04:50:16
插入器的聲明也需要成爲模板。 – 0x499602D2 2014-10-20 04:53:01