无法使 C++ 中的输出流过载;
我正在尝试超载 <<我的类 STEntry
中的运算符,但不断遇到此错误。我的课程粘贴在错误下方。
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
I am trying to overload the << operator in my class STEntry
but keep running into this error. My class is pasted bellow the error.
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&)
My class in STEntry.h. It is pretty simple. Im trying to display some variable values.
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您重载了
operator<<
就好了。导致问题的是该函数内部的行。从错误消息来看,它不知道如何将
lexeme
(string
)写入流中。您是否同时包含
和
?我在您发布的代码中只看到其中之一。You overloaded
operator<<
just fine. It's the line inside that function that's causing problems.From the error message, it doesn't know how to write
lexeme
(astring
) into the stream.Did you include both
<iostream>
and<string>
? I only see one of them in your posted code.添加
到您的文件。
Add
to your file.
您可能需要在标头之前包含 iostream
you probably need include iostream before your header
您可能尚未将任何流库包含到您的项目中。尝试
#include
或You may have not included any stream library into your project. Try
#include <iostream>
or<ostream>
您实际上必须实现运算符<<换句话说,你必须写它。
另请注意,宏名称上的 __ 保留供编译器实现者使用,不应使用。
You actually have to implement the operator<< In other words you have to write it.
Also please note that __ on the macro names is reserved for use by compiler implementors and should not be used.