程序不调用正确的重载操作符 [英] program not calling the right overloaded operator

查看:130
本文介绍了程序不调用正确的重载操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类目录,其中包含类File的对象和重载的操作符,像这样:

 目录
{
std :: vector< char> name,timeOfCreation;
std :: vector< File> arr;

public:
Directory(std :: string);
Directory(const Directory&);
〜Directory();


目录操作符+ =(文件);
目录操作符+ =(目录);
目录操作符 - =(File);
目录操作符〜();
文件操作符[](int);
operator int();

friend bool operator%(Directory,File);
friend bool operator%(Directory,Directory);
friend目录运算符+(目录,目录);
friend目录操作符 - (目录,目录);
friend long int operator +(Directory);
friend std :: ofstream&运算符<< (std :: ofstream& Directory);

friend void main();
};

好,现在问题出现在主要我有

  void main()
{
// make目录的对象d
std :: cout< d;
}

程序现在调用操作符int(),而不是operator< 。因此命令
std :: cout 的行为类似于 std :: cout< (int)d ,它会写出我的目录中的文件数。



这里是运算符的实现<<和int():

  Directory :: operator int()
{
return 。尺寸()​​;
}

std :: ofstream&运算符<< (std :: ofstream& out,Directory d)
{
//获取三个字段的最大字段宽度
int widthLeft = 0,widthMiddle = 0,widthRight = 10;
std :: vector< File> :: const_iterator i = d.arr.begin();
for(; i< d.arr.end(); ++ i)
{
if((int)d.timeOfCreation.size()> widthLeft)
widthLeft =(int)d.timeOfCreation.size();
if((int)d.name.size()> widthMiddle)
widthMiddle =(int)d.name.size()
}
out<< std :: setw(widthLeft)<<Date& Time of creation;
out<< std :: setw(widthMiddle)<<Name;
out<< std :: setw(widthRight)<<Total Size\\\
;
return out;

}

注意:运算符<尚未完成,我只是测试setw函数,但它仍然应该写出一行。

解决方案

cout ostream ,而不是 ofstream p>

应该工作:

  std :: ostream&运算符<< (std :: ostream& Directory)

您可能希望将目录作为参考。 / p>

I've the class Directory which does stuff with objects of class File, and overloaded operators, like this:

class Directory
{
    std::vector<char> name, timeOfCreation;
    std::vector<File> arr;

public:
    Directory(std::string);
    Directory(const Directory&);
    ~Directory();


    Directory operator += (File);
    Directory operator += (Directory);
    Directory operator -= (File);
    Directory operator ~();
    File operator [] (int);
    operator int();

    friend bool operator % (Directory, File);
    friend bool operator % (Directory, Directory);
    friend Directory operator + (Directory, Directory);
    friend Directory operator - (Directory, Directory);
    friend long int operator + (Directory);
    friend std::ofstream& operator << (std::ofstream&, Directory);

friend void main();
};

Ok, now the issue arises when in main i have

void main()
{
     //make object of Directory d
    std::cout << d;
}

The program now calls the operator int() instead of operator <<. Thus the command std::cout <<d acts like std::cout << (int)d, and it writes out the number of Files in my Directory.

Here are the implementations of operators << and int():

Directory::operator int()
{
    return (int)arr.size();
}

std::ofstream& operator << (std::ofstream& out, Directory d)
{
    // get the max field widths for three fields
    int widthLeft = 0, widthMiddle = 0, widthRight = 10;
    std::vector<File>::const_iterator i = d.arr.begin();
    for(; i < d.arr.end(); ++i)
    {
        if((int)d.timeOfCreation.size() > widthLeft)
            widthLeft = (int)d.timeOfCreation.size();
    if((int)d.name.size() > widthMiddle)
            widthMiddle = (int)d.name.size();
    }
    out<<std::setw(widthLeft)<<"Date & Time of creation";
    out<<std::setw(widthMiddle)<<"Name";
    out<<std::setw(widthRight)<<"Total Size\n";
    return out;

}

Note: the operator << isn't finished yet, I'm just testing the setw function, but it still should write out that one line.

解决方案

cout is an ostream, not an ofstream.

Should work:

std::ostream& operator << (std::ostream&, Directory)

You might want to pass the directory as a reference.

这篇关于程序不调用正确的重载操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆