我試圖在我的類STEntry
中超載< <運算符,但仍然遇到此錯誤。我的課上粘貼錯誤。無法在C++中過載輸出流
stentry.h: In function ‘std::ostream& operator<<(std::ostream&, const STEntry&)’:
stentry.h:48: error: no match for ‘operator<<’ in ‘std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)out)), ((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)(& temp->STEntry::lexeme))) << ','’
stentry.h:46: note: candidates are: std::ostream& operator<<(std::ostream&, const STEntry&)
我在STEntry.h中的類。這很簡單。我試圖顯示一些變量值。
#ifndef __STENTRY__
#define __STENTRY__
#include <string>
using namespace std;
class STEntry {
public:
string lexeme; // addr. of lexema associated with this entry
int tokenval; // token value for this entry
int offset; // location of variable in block
STEntry(string name = "", int newval = 0, int newoffset = 0);
// function: constructor ... initializes major fields
// Relational operators:
bool operator == (const STEntry &) const;
bool operator != (const STEntry &) const;
friend ostream & operator << (ostream &, const STEntry &);
};
//--- BEGIN IMPLEMENTATION
//constructor
STEntry::STEntry(string name, int newval, int newoffset)
{
lexeme = name;
tokenval = newval;
offset = newoffset;
}
// ....
//Output a single STEntry to standard output
std::ostream& operator << (std::ostream& out, const STEntry & temp)
{
out << temp.lexeme << ',' << temp.tokenval << ',' << temp.offset;
return out;
}
//--- END OF IMPLEMENTATION
#endif
就是這樣。 TX。 – sri 2012-01-29 02:52:01