'注意:候选人:音量& Volume :: operator =(const Volume&)'。这个错误是什么?

时间:2018-06-06 05:14:16

标签: c++

为什么我一直收到这个错误? 我试图使用nfs共享来访问文件。 我一直收到这些错误。 我想通过nfs共享通过netork访问一些文件。 我已经提到了一些代码,当我尝试编译这些代码时,我得到了这些错误。我对c ++很新。请帮我..!!谢谢。

test@wsm-ela-inc6:~/salu$ make all
g++ -g -Wall -c FS.cpp
FS.cpp: In member function ‘int FS::open(Volume&, const char*, const char*)’:
FS.cpp:128:19: error: no match for ‘operator=’ (operand types are ‘Volume’ and ‘Volume*’)
   open_file.volume= &volume;
                   ^
In file included from FS.h:12:0,
             from FS.cpp:8:
Volume.h:19:7: note: candidate: Volume& Volume::operator=(const Volume&)
 class Volume
       ^
Volume.h:19:7: note:   no known conversion for argument 1 from ‘Volume*’ to ‘const Volume&’
FS.cpp: In member function ‘void FS::close(int)’:
FS.cpp:162:32: error: base operand of ‘->’ has non-pointer type ‘Volume’
  Directory dir(open_file.volume->get_fat());
                            ^
 FS.cpp: In member function ‘int FS::read(int, char*, int)’:
FS.cpp:196:17: error: no match for ‘operator*’ (operand type is ‘Volume’)
   Volume &vol = *open_file.volume;
                 ^
FS.cpp:230:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(read_len<block_size) {
                ^
FS.cpp: In member function ‘int FS::write(int, char*, int)’:
FS.cpp:267:15: error: no match for ‘operator*’ (operand type is ‘Volume’)
 Volume &vol = *open_file.volume;
               ^
FS.cpp:320:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      if(read_len<block_size){
                 ^
FS.cpp:275:8: warning: unused variable ‘blk’ [-Wunused-variable]
   char blk[MAX_BLOCK_SIZE];
        ^
FS.cpp: In member function ‘int FS::seek(int, int)’:
FS.cpp:353:35: error: base operand of ‘->’ has non-pointer type ‘Volume’
  FAT &fat_file = (open_file.volume->get_fat());
                                   ^
FS.cpp: In member function ‘int FS::rmdir(Volume&, const char*, const char*)’:
FS.cpp:462:6: error: ‘file_fcb’ was not declared in this scope
   if(file_fcb->type !=DIR_TYPE) return DISK_ERROR;
      ^
Makefile:13: recipe for target 'FS.o' failed
make: *** [FS.o] Error 1

完整代码在以下链接中给出: https://github.com/anieshchawla/networkfilesystem/

/**     @file FS.cpp
 *
 *      File System implementation
 *
 *      @author Copyright Robert van Engelen, March 2006
 **/
//#include "FCB.h"

#include "FS.h"
#include "Directory.h"
#include <iostream>

FS::FS()
{ }

FS::~FS()
{ }

int FS::open(const Volume& volume, const char *path, const char *name)
{
int fd = 0;

// TODO("Implement FS::open()");

// Check if file was already opened (has entry in open file table):
// Check all open file entries for count > 0.
// If the volume, path, and file name match, then ++count and return fd.

// Otherwise, set fd to a free entry in the open file table with count ==0
// and set the open_file.name to the open() name argument.

// If no entries are available then report error and return DISK_ERROR.

//assert(open_file_table[fd].count == 0);

while((fd < MAX_OPEN_FILES)){
if(strcmp(open_file_table[fd].name.cstr() , name)==0){
  break;
}
fd++;
}
if (fd==MAX_OPEN_FILES){
fd = 0;
while(open_file_table[fd].count!=0 && fd<MAX_OPEN_FILES){
  fd++;
}
if(fd == MAX_OPEN_FILES) fd=DISK_ERROR;
else {
  assert(open_file_table[fd].count == 0);
  open_file_table[fd].name = name;
}
}

///////// OpenFile& open_file = open_file_table[fd];

// Create a Directory object for the volume's FAT and use   Directory::chdir()
// to move to the path location of the file.
// The dir should now point to the directory of the file.
// If the dir does not exist, then report and return DISK_ERROR.

// Get the FCB of the file via the dir object.

// If there is no FCB, then this is a new file so we create a new FCB with
// size = 0, type = FILE_TYPE, created = time(NULL), and we allocate a new
// start_block from the FAT of the volume.

// Otherwise, if there is an FCB, then set the open_file.size and
// open_file.start_block from the FCB attributes.

// Set the other open_file attributes:
// open_file.pos = 0;
// open_file.current_block = open_file.start_block;
// open_file.current_block_pos = 0;
// open_file.updated = false;

// Increment the open_file.count.

// fd = DISK_ERROR;

OpenFile& open_file = open_file_table[fd];//we used & symbol because if  we change open_file we want corresponding
//change to happend in open_file_table entry also

// Create a Directory object for the volume's FAT and use Directory::chdir()
// to move to the path location of the file.
// The dir should now point to the directory of the file.
// If the dir does not exist, then report and return DISK_ERROR.
Directory dir(volume.get_fat());
dir.chdir(path);
//if(!dir.exists(open_file.name)) return DISK_ERROR;
// Get the FCB of the file via the dir object.

const FCB *fcb_file = dir.get_fcb(open_file.name);

// If there is no FCB, then this is a new file so we create a new FCB with
// size = 0, type = FILE_TYPE, created = time(NULL), and we allocate a new
// start_block from the FAT of the volume.
if(fcb_file == NULL) {
FCB fcb;
fcb.size = 0;
fcb.type = FILE_TYPE;
fcb.created = time(NULL);
fcb.start_block = volume.get_fat().alloc();
open_file.start_block = fcb.start_block;
dir.set_fcb(open_file.name, fcb);
}

// Otherwise, if there is an FCB, then set the open_file.size and
// open_file.start_block from the FCB attributes.
else{
open_file.size = fcb_file->size;
open_file.start_block = fcb_file->start_block;
}

// Set the other open_file attributes:
open_file.pos = 0;
open_file.volume= &volume;
open_file.current_block = open_file.start_block;
open_file.current_block_pos = 0;
open_file.updated = false;
// Increment the open_file.count.
open_file.count++;

//fd = DISK_ERROR;

return fd;

/////////////// return fd;
}

void FS::close(int fd)
{
OpenFile& open_file = open_file_table[fd];

assert(open_file.count > 0);

// TODO("Implement FS::close()");

 // If the file was updated, update its FCB in the directory:
if (open_file.updated)
{
// Create a Directory object for the volume's FAT and use Directory::chdir()
// to move to the path location of the file.
// The dir should now point to the directory of the file.
// If the dir does not exist, then report and return DISK_ERROR.

 Directory dir(open_file.volume->get_fat());
  dir.chdir(open_file.path);
  if(!dir.exists(open_file.name)) exit DISK_ERROR;

// Get the FCB object of the file, update it with the open_file attributes
// such as size, and set the FCB of the file

const FCB *fcb_file = dir.get_fcb(open_file.name);
if(fcb_file==NULL){

}
open_file.count--;

// What to do if the FCB was not found? This could happen when the file is
// deleted before closed. So rm() must check if the file is not open!
}

// Decrement the open_file.count
}

int FS::read(int fd, char *buf, int len)
{
OpenFile& open_file = open_file_table[fd];

assert(open_file.count > 0);

// TODO("Implement FS::read()");

int block_num = open_file.current_block;
int byte_to_read = len;
Volume &vol = *open_file.volume;
FAT &file_table = vol.get_fat();
Disk &read_fm_disk = file_table.get_disk();
int block_size = read_fm_disk.block_size();

char blk[MAX_BLOCK_SIZE];

// We need to copy data of at most len bytes from disk to the buf  argument.

while(len>0){
open_file.current_block = block_num;
size_t read_len = std::min(len,block_size);
read_fm_disk.read(block_num,blk);
int read_length = open_file.current_block_pos + read_len;
//std::cout<<"read length= "<<read_length<<" current_block_pos= "<<open_file.current_block_pos<<std::endl;
//std::cout<<"block_size= "<<read_fm_disk.block_size()<<" block_num "<<block_num<<std::endl;
open_file.pos +=read_len;
if(read_length > block_size ){
  memcpy(buf,blk+open_file.current_block_pos,block_size-open_file.current_block_pos);
  block_num = file_table.find_next(block_num);
  open_file.current_block = block_num;
  read_fm_disk.read(block_num,blk);
  //std::cout<<"value copied till now is "<< buf<<std::endl;
  //std::cout<<"value to be copied "<< blk<<std::endl;
  read_len -= (block_size - open_file.current_block_pos);
  buf+= (block_size - open_file.current_block_pos);
  len-= (block_size - open_file.current_block_pos);
  open_file.current_block_pos = 0;
    }
//if(block_num==24) std::cout<<"value should be "<<blk+open_file.current_block_pos<<std::endl;
memcpy(buf,blk+open_file.current_block_pos,read_len);
buf+=read_len;
if(read_len<block_size) {
  open_file.current_block_pos += read_len;
  if(open_file.current_block_pos > block_size) open_file.current_block_pos-=block_size;
   }
else{

  block_num = file_table.find_next(block_num);
}
len-=read_len;
}

// To do so, use a loop to copy the data in chunks.
// Within the loop, update the open_file.pos position in the file,
// the open_file.current_block, and open_file.current_block_pos offset into
// the current block being read.

// You need to get the FAT of the volume of the open file and use find_next()
// to find the blocks.

// Return the number of bytes copied, or DISK_ERROR when an error occurred.

 ///////// return DISK_ERROR;

return byte_to_read;
}
int FS::write(int fd, char *buf, int len) 
{
OpenFile& open_file = open_file_table[fd];

assert(open_file.count > 0);

open_file.updated = true;

//TODO("Implement FS::write()");


Volume &vol = *open_file.volume;
FAT &file_table = vol.get_fat();
Disk &fm_disk = file_table.get_disk();
Directory dir(file_table);
FCB fcb_of_file;
int block_size = fm_disk.block_size();

char blk[MAX_BLOCK_SIZE];

const FCB *old_fcb_file = dir.get_fcb(open_file.name);
int block_num = open_file.current_block;


// We need to copy data from buf of length len to disk.

// To do so, use a loop to copy the data in chunks.
// Within the loop, update the open_file.size, open_file.pos,
// the open_file.current_block, and open_file.current_block_pos offset into
// the current block being written.

while(len>0){
size_t read_len = std::min(len,block_size);
char buf_disk[block_size];
//std::cout<<"the block number is "<<block_num<<std::endl;
if(open_file.current_block_pos == block_size){
  //std::cout<<"we are inside this block num "<<block_num<<std::endl;
  block_num = file_table.alloc_next(block_num);
  open_file.current_block = block_num;
  open_file.current_block_pos = 0;
  fm_disk.read(block_num,buf_disk);
}
fm_disk.read(block_num,buf_disk);
char write_buf[block_size];
memcpy(write_buf,buf_disk,open_file.current_block_pos);
int read_length = open_file.current_block_pos+read_len;
open_file.pos +=read_len;
open_file.size+=read_len;

if(read_length > block_size){
  memcpy(write_buf+open_file.current_block_pos,buf,block_size- open_file.current_block_pos);
  fm_disk.write(block_num,write_buf);  
  block_num = file_table.alloc_next(block_num);
  open_file.current_block = block_num;
  buf+=block_size-open_file.current_block_pos;
  read_len -= block_size - open_file.current_block_pos;
  len-= block_size - open_file.current_block_pos;
  open_file.current_block_pos = 0;
  fm_disk.read(block_num,buf_disk);
  read_length = read_len;
}
memcpy(write_buf+open_file.current_block_pos,buf,read_len);
memcpy(write_buf+read_length, buf_disk + read_length,block_size- read_length);
if(read_len<block_size){
  open_file.current_block_pos+=read_len;
  if(open_file.current_block_pos>block_size){
    open_file.current_block_pos-=block_size;
  }
}
fm_disk.write(block_num,write_buf);

buf+=read_len;
len-=read_len;
 }
fcb_of_file.type = old_fcb_file->type;
fcb_of_file.size = open_file.size;
fcb_of_file.created = old_fcb_file->created;
fcb_of_file.start_block = old_fcb_file->start_block;
dir.set_fcb(open_file.name,fcb_of_file);
// You need to get the FAT of the volume of the open file and use
// alloc_next() to find or get new blocks.

// Note that writing data inside blocks may require reading blocks first.

// Return DISK_OK or DISK_ERROR.


return DISK_OK;
}

int FS::seek(int fd, int pos)
{
OpenFile& open_file = open_file_table[fd];

assert(open_file.count > 0);

FAT &fat_file = (open_file.volume->get_fat());
int block_size = fat_file.get_disk().block_size();
//TODO("Implement FS::seek()");
//std::cout<<"the file position= "<<open_file.pos<<" requested pos= "<<pos<<std::endl;
int block_shifts = (pos - open_file.pos);
int offset = pos%block_size;
//std::cout<<"block shifts "<<block_shifts<<" offset "<<offset<<std::endl;
if(open_file.size < pos) return DISK_ERROR;
else{
if(block_shifts < 0) {
  block_shifts = (int)(pos/block_size) - (int)(open_file.pos/block_size);
  if(open_file.pos == ((int)(open_file.pos/block_size))*block_size) block_shifts++;
  //std::cout<<"block shifts negative is "<<block_shifts<<std::endl;
  block_shifts = open_file.current_block - open_file.start_block + block_shifts;
  //std::cout<<"the block shifts from start are "<<block_shifts<<std::endl;
  open_file.current_block = open_file.start_block;
}
open_file.pos = pos;

while(block_shifts > 0 ){
    open_file.current_block = fat_file.find_next(open_file.current_block);
    block_shifts--;
}
open_file.current_block_pos = offset;
return DISK_OK;

}

// Change the open_file.pos, open_file.current_block, and
// open_file.current_block_pos within the allowable range of the file
// where the range is 0 to size-1.

// Return DISK_OK or DISK_ERROR when an error occurred

return DISK_ERROR;
}

int FS::ls(Volume& volume, const char *path)
{
Directory dir(volume.get_fat());

dir.chdir(path);

printf("Volume %s:%s (%d blocks, %d free)\n", volume.get_name(), path, volume.get_fat().num_blocks(), volume.get_fat().num_free_blocks());

dir.list();
return DISK_OK;
}

int FS::rm(Volume& volume, const char *path, const char *name)
{
//TODO("Implement FS::rm()");
int fd = -1;
 Directory dir(volume.get_fat());
dir.chdir(path);
FileName file_name(name);
const FCB* file_fcb = dir.get_fcb(file_name);

// Check if this is a regular file.

if(file_fcb->type !=FILE_TYPE) return DISK_ERROR;

// Check if the file is not open.
while((fd < MAX_OPEN_FILES)){
if(strcmp(open_file_table[fd].name.cstr() , name)==0){
  break;
}
fd++;
}
// Create a Directory object for the volume's FAT and use Directory::chdir()
// to move to the path location of the file.
// The dir should now point to the directory of the file.
// If the dir does not exist, then report and return DISK_ERROR.

// Use Directory::erase() to remove the file.
if(fd != MAX_OPEN_FILES){
assert(open_file_table[fd].count ==0);

dir.erase(file_name);
return DISK_OK;
}


// Return DISK_OK or DISK_ERROR.

return DISK_ERROR;
}

int FS::mkdir(Volume& volume, const char *path, const char *name)
{
Directory dir(volume.get_fat());

dir.chdir(path);

FileName dir_name(name);

return dir.mkdir(dir_name);
}

int FS::rmdir(Volume& volume, const char *path, const char *name)
{
Directory dir(volume.get_fat());

dir.chdir(path);

FileName dir_name(name);
const FCB* file_fcb = dir.get_fcb(dir_name);
//TODO("Complete FS::rmdir()");
// Check if this is a directory.
if(file_fcb->type !=DIR_TYPE) return DISK_ERROR;
if(!dir.exists(dir_name)) return DISK_ERROR;
// Check if the directory is empty.
//if(!dir.empty()) return DISK_ERROR;
dir.erase(dir_name);
return DISK_OK;
}




/**     @file Volume.h
*
*      Volume structure
*
*      @author Copyright Robert van Engelen, March 2006
**/

#ifndef VOLUME_H
#define VOLUME_H

#include "includes.h"
#include "Disk.h"
#include "FAT.h"

/** A Volume object associates a volume name with a formatted Disk.
*
*   The Volume object contains a FAT and a reference to the Disk object.
**/
class Volume
{
private:
/// The volume name.
const char *name;
/// The FAT with the Disk reference.
FAT fat;

public:
/// Create a new named Volume (and FAT) for a formatted Disk object.
Volume(const char *name, Disk& disk);
~Volume();
/// Return the name of this volume.
const char *get_name() const;
/// Return a reference to the FAT of the Disk for this volume.
FAT& get_fat();


//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


/*

Volume(const char *name, Disk& disk) : name(strdup(name)), fat(disk)
{ }

~Volume()
{
if (name)
free((void*)name);
}

const char * get_name() const
{
return name;
}

FAT& get_fat()
{
return fat;
}


*/

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



};

#endif

1 个答案:

答案 0 :(得分:1)

编译器想要告诉你的是:

&#34;请解释这一行open_file.volume= &volume,你在那里做了什么以及如何将指向Volume的指针分配给Volume。特别是显示为这种分配定义赋值运算符的位置。&#34;

要么你没有定义任何新的做法=,要么你错过了这个案例 (请原谅我没有筛选完全非最小的例子。它应该只包含重现你不理解的第一个编译器错误所需的代码。)

但是,我想你可能只想做

open_file.volume= volume;