Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运算符无故更改类的参数(*int)_C++_Class_Pointers_Operator Overloading_Arguments - Fatal编程技术网

C++ 运算符无故更改类的参数(*int)

C++ 运算符无故更改类的参数(*int),c++,class,pointers,operator-overloading,arguments,C++,Class,Pointers,Operator Overloading,Arguments,在我的类文件中,我: class File { std::vector<char> name, timeOfCreation, timeOfLastEdit, content; std::vector<char>::const_iterator* pPos, *pPosOfEnd; int *pSize; bool *pForcedSize; void setTimeOfLastEdit(); int* indexOfLas

在我的类文件中,我:

class File
{
    std::vector<char> name, timeOfCreation, timeOfLastEdit, content;
    std::vector<char>::const_iterator* pPos, *pPosOfEnd;
    int *pSize;
    bool *pForcedSize;
    void setTimeOfLastEdit();
    int* indexOfLastChar, *indexOfCurrentChar;

    friend class Interface;
    friend class Directory;
public:
    File(std::string, int argSize = 0, std::string arg_content = 0); // constructor
    File(); // constructor
    File(const File&); // copy constructor
    ~File(); // destructor
    char* returnName(); 

    File& operator = (const File&);
    File operator += (const int);
    File operator -= (const int);
    File& operator ++ (); // prefix
    File& operator -- (); // prefix
    File operator ++ (int); // postfix
    File operator -- (int); // postfix

    bool operator ! ();

    int operator () (char*, int);

    friend  bool operator == (const File&, const File&);
    friend  bool operator != (const File&, const File&);
    friend  bool operator >= (const File&, const File&);
    friend  bool operator <= (const File&, const File&);
    friend  bool operator < (const File&, const File&);
    friend  bool operator > (const File&, const File&);

    friend std::istream & operator >> (std::istream&, File&);
    friend std::ostream & operator << (std::ostream&, File);

};
编辑1:这里是我更改indexOfCurrentChar的地方:

1) 在赋值运算符=(但是,我没有在接口类中调用它) 2) 在复制构造函数中(在接口类中也不使用它,它只是复制值) 3) 在文件构造函数中:

File::File()
{
    // stuff
    indexOfCurrentChar = new int;
} //I'm not setting its value, just allocating memory
File::File(std::string arg_name, int arg_size, std::string arg_content)
{
     // stuff
     indexOfCurrentChar = new int;
     *indexOfCurrentChar = 0;
    //setting it to 0, but its just at creation time
}
4) 在文件构造函数中:

File::File()
{
    // stuff
    indexOfCurrentChar = new int;
} //I'm not setting its value, just allocating memory
File::File(std::string arg_name, int arg_size, std::string arg_content)
{
     // stuff
     indexOfCurrentChar = new int;
     *indexOfCurrentChar = 0;
    //setting it to 0, but its just at creation time
}
5) 在>>操作符中 //获取文件内容

std::cout<<"Enter file content: ";
    *object.indexOfCurrentChar = 0;
    while( in.get(c) && c != '\n');
    i = 0;
    in.get(c);
    if(*(object.pSize) == 0)
    {
        while(c != '\n')
        {
            object.content.push_back(c);
            in.get(c);  
            ++i;
        }
        *(object.pSize) = (int)object.content.size();
        *(object.pPosOfEnd) = object.content.end();
        *object.indexOfLastChar = i;
    }
    else
    {
        i = 0;
        std::vector<char>::const_iterator it = object.content.begin();
        while(c != '\n')
        {
            if(i == *object.pSize)
            {
                *object.pPosOfEnd = it;
                *object.indexOfLastChar = i;
            }
            if(i >= *object.pSize)
            {
                in.get(c);
                continue;
            }
            object.content.push_back(c);
            in.get(c);
            ++i;
        }
    }


    *(object.pPos) = object.content.begin();

享受^^

我建议更换

 if(i == *indexOfLastChar-1)


我想你是在读“indexOfLastChar”之前的值,并将其与I进行比较。

我已经将此作为评论发布,但我认为

indexOfCurrentChar = n; 
应该是

 *indexOfCurrentChar += n; 

我猜问题在于
目录
类及其存储和返回
文件
对象的方式。例如,如果
Directory::operator[]
返回一个
文件而不是
文件&
,那么对返回对象所做的任何更改都不会保留到
目录
的内部副本,因此在一行中进行两次调用,如

int i = dir[index](buffer, n);
int j = dir[index](buffer, n1);
实际上会导致对两个不同的临时
文件
对象调用
File::operator()。这可以解释为什么
*indexOfCurrentChar
的值不是您期望的值

编辑

既然您已经展示了
Directory::operator[]
的实现,我可以自信地说我的怀疑是正确的。即使您更改了
Directory::operator[]
的签名,使其现在返回一个
文件&
,您的实现仍然不会按您所希望的方式运行,因为它不会返回对正确的
文件
对象(该
目录
对象的内部副本)的引用。三项声明

File* temp = new File;
*temp = *i;
return *temp;
创建
目录
对象的内部
文件
对象的新副本,并返回对该对象的引用,因此对返回的
文件
对象的成员变量所做的任何更改都不会反映在
目录::arr
向量的相应条目中。由于
vector::operator[]
返回一个引用,因此将
Directory::operator[]
的主体更改为

return arr[index];
应该给你想要的行为


我可能还应该注意到,您当前的
Directory::operator[]
实现泄漏内存:
delete temp从不运行,因为
返回*temp
导致控件离开函数。

您确定第二次使用的是相同的索引吗?您是否将
*indexOfcurrentChar
指向的内容更改为两个
操作符()调用之间的某个位置?@Meysam是的,考虑到它正确地写出了内容,请从头开始,而它应该从上个月底开始做extraction@Tony狮子我很确定我没有,但我想这就是代码中发生的事情,可能是由于运算符[],但我对此表示怀疑…在构造函数中,indexOfCurrentChar=0应该更改为*indexOfCurrentChar=0刚刚做了,不幸的是它仍然是一样的:(取消引用运算符(*)在减法运算符(-)之前求值,减法运算符在==运算符之前求值。因此,我认为这一更改不会有任何区别:indexOfCurrentChar=n;/should it is*/*indexOfCurrentChar+=n;是的,应该感谢,但它并不能解决我当前的问题:(不,我测试了这个,这不是问题的原因。@Meysam你怎么能测试这个?你有
目录
类的源代码吗?你是对的。我的假设是
目录
是一个
文件
对象的数组。
操作符[]
的实现在这里很重要,正如你所说:)事实上,根据海报中所示的
目录
类的定义,我现在相当确定这就是问题所在。@chess007是的,它只返回了文件,应该是这样吗?
文件和目录::操作符[](int index){std::vector::const_迭代器I=arr.begin();for(int j=0;j
我已将
[]
运算符更改为return
File&
但问题仍然存在。您认为它现在正确返回引用了吗?
int i = dir[index](buffer, n);
int j = dir[index](buffer, n1);
File* temp = new File;
*temp = *i;
return *temp;
return arr[index];