C++认为<<'不是班级成员,但它是

发布于 2024-08-05 23:36:50 字数 783 浏览 9 评论 0 原文

我必须编写一个简单的日志类,它将输出写入文件。

我希望它能够重载 <<运算符,所以我可以这样做:

MyLog log("C:\\log.txt");
log<<"Message";

但是 Visual C++ 告诉我:“错误 C2039:'<<' :不是“MyLog”的成员“

我不知道我做错了什么。

这是代码:

MyLog.h

#pragma once
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;

class MyLog
{
private:
    ofstream logfile;
public:
    MyLog(char* filename);
    friend MyLog& operator<<(MyLog& l,char*msg);
};

MyLog.cpp

#include "MyLog.h"

MyLog::MyLog(char* filename)
{
    logfile.open(filename);
}

MyLog& MyLog::operator<<(MyLog& l,char*msg)
{
    cout<<msg;
    return l;
}

有谁知道出了什么问题?

I have to write a simple log class, that would write the output to a file.

I want it to work with overloading the << operator, so I can do this:

MyLog log("C:\\log.txt");
log<<"Message";

But Visual C++ tells me: "error C2039: '<<' : is not a member of 'MyLog' "

I don't know what I am doing wrong.

Here is the code:

MyLog.h

#pragma once
#include <iostream>
#include <conio.h>
#include <fstream>
using namespace std;

class MyLog
{
private:
    ofstream logfile;
public:
    MyLog(char* filename);
    friend MyLog& operator<<(MyLog& l,char*msg);
};

MyLog.cpp

#include "MyLog.h"

MyLog::MyLog(char* filename)
{
    logfile.open(filename);
}

MyLog& MyLog::operator<<(MyLog& l,char*msg)
{
    cout<<msg;
    return l;
}

Does anyone know what is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

花开柳相依 2024-08-12 23:36:50

您已声明自由函数 MyLog&运算符 <<(MyLog& l,char* msg) 成为 MyLog 类的 friend。它不是类本身的成员,因此函数的定义应以此开头:

MyLog& operator<<(MyLog& l,char* msg)
{
   //...

You have declared the free function MyLog& operator<<(MyLog& l,char* msg) to be a friend of the MyLog class. It is not a member of the class itself, so your definition of the function should start with this:

MyLog& operator<<(MyLog& l,char* msg)
{
   //...
稚气少女 2024-08-12 23:36:50

Visual C++ 是对的,您的 operator<< 确实不是类 MyLog 的成员。尝试将其设为成员函数而不是友元单独函数:

class MyLog {
    // ...

public:
    MyLog& operator<<(int i);
}

MyLog& MyLog::operator<<(int i) {
    cout << i;
    return *this;
}

Visual C++ is right, your operator<< is indeed not a member of class MyLog. Try making it a member function instead of a friended separate function:

class MyLog {
    // ...

public:
    MyLog& operator<<(int i);
}

MyLog& MyLog::operator<<(int i) {
    cout << i;
    return *this;
}
向地狱狂奔 2024-08-12 23:36:50

您尝试输出 char* :

log<<"Message";

但只为 int 定义了运算符:

MyLog& MyLog::operator<<(MyLog& l,int i)

我猜错误消息还说“或者没有可接受的转换”

You try to output a char* :

log<<"Message";

But only defined your operator for int :

MyLog& MyLog::operator<<(MyLog& l,int i)

I guess the error message also says "or there is no acceptable conversion"

征棹 2024-08-12 23:36:50

在这里,您声明了 int 的 << 运算符,并且您尝试传递 char*。

也尝试声明 char* 重载:

MyLog& operator<<(MyLog& l,char* msg);

顺便说一下,为什么要将它声明为 friend

Here you declared the << operator for int, and you are trying to pass a char*.

Try declaring the char* overload as well:

MyLog& operator<<(MyLog& l,char* msg);

By the way, why are you declaring it as friend?

时光沙漏 2024-08-12 23:36:50

尝试修改

friend MyLog& operator<<(MyLog& l,int i);

MyLog& operator<<(MyLog& l,int i);

Try modify

friend MyLog& operator<<(MyLog& l,int i);

to

MyLog& operator<<(MyLog& l,int i);
赴月观长安 2024-08-12 23:36:50

您已声明运算符<<作为非 MyLog 会员的朋友。这有效:

MyLog&运算符<<(MyLog&l,int i)
{
计算<

You've declared operator<< as a friend of not member of MyLog. This works:

MyLog& operator<<(MyLog& l,int i)
{
cout<

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