无法使 C++ 中的输出流过载;

发布于 2024-12-29 13:44:56 字数 1941 浏览 2 评论 0原文

我正在尝试超载 <<我的类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

澉约 2025-01-05 13:44:56

您重载了 operator<< 就好了。导致问题的是该函数内部的行。

out << temp.lexeme << ',' << temp.tokenval << ',' << temp.offset;

从错误消息来看,它不知道如何将lexemestring)写入流中。

您是否同时包含 ?我在您发布的代码中只看到其中之一。

You overloaded operator<< just fine. It's the line inside that function that's causing problems.

out << temp.lexeme << ',' << temp.tokenval << ',' << temp.offset;

From the error message, it doesn't know how to write lexeme (a string) into the stream.

Did you include both <iostream> and <string>? I only see one of them in your posted code.

单身情人 2025-01-05 13:44:56

添加

#include <iostream>

到您的文件。

Add

#include <iostream>

to your file.

您可能需要在标头之前包含 iostream

you probably need include iostream before your header

三生殊途 2025-01-05 13:44:56

您可能尚未将任何流库包含到您的项目中。尝试 #include

You may have not included any stream library into your project. Try #include <iostream> or <ostream>

无远思近则忧 2025-01-05 13:44:56

您实际上必须实现运算符<<换句话说,你必须写它。

另请注意,宏名称上的 __ 保留供编译器实现者使用,不应使用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文