错误:不匹配'运营商=' in' myFile = SDClass :: open(const char *,uint8_t)(((const char *)" test.txt"),19u)'

时间:2014-10-02 04:29:38

标签: c++

FILE.CPP:我正在使用这个文件并使用它我创建了对象" myFile"

/*

 SD - a slightly more friendly wrapper for sdfatlib

 This library aims to expose a subset of SD card functionality
 in the form of a higher level "wrapper" object.

 License: GNU General Public License V3
          (Because sdfatlib is licensed with this.)

 (C) Copyright 2010 SparkFun Electronics

 */

#include <SD.h>
/* for debugging file open/close leaks
   uint8_t nfilecount=0;
*/

File::File(SdFile f,
const char *n) {
  // oh man you are kidding me, new() doesnt exist? Ok we do it by hand!
  _file=(SdFile *)malloc(sizeof(SdFile));
  if (_file) {
    memcpy(_file, &f, sizeof(SdFile));
    strncpy(_name, n, 12);
    _name[12]=0;
    /* for debugging file open/close leaks
       nfilecount++;
       Serial.print("Created \"");
       Serial.print(n);
       Serial.print("\": ");
       Serial.println(nfilecount, DEC);
    */
  }
}
File::File(void) {
  _file=0;
  _name[0]=0;
  //Serial.print("Created empty file object");

}
File::~File(void) {
  //  Serial.print("Deleted file object");

}
// returns a pointer to the file name
char *File::name(void) {
  return _name;
}
// a directory is a special type of file
boolean File::isDirectory(void) {
  return (_file && _file->isDir());
}
size_t File::write(uint8_t val) {
  return write(&val, 1);
}
size_t File::write(const uint8_t *buf,
size_t size) {
  size_t t;
  if (!_file) {
    setWriteError();
    return 0;
  }
  _file->clearWriteError();
  t=_file->write(buf,
  size);
  if (_file->getWriteError()) {
    setWriteError();
    return 0;
  }
  return t;
}
int File::peek() {
  if (! _file) return 0;
  int c=_file->read();
  if (c !=-1) _file->seekCur(-1);
  return c;
}
int File::read() {
  if (_file) return _file->read();
  return -1;
}
// buffered read for more efficient, high speed reading
int File::read(void *buf,
uint16_t nbyte) {
  if (_file) return _file->read(buf, nbyte);
  return 0;
}
int File::available() {
  if (! _file) return 0;
  uint32_t n=size() - position();
  return n > 0X7FFF ? 0X7FFF: n;
}
void File::flush() {
  if (_file) _file->sync();
}
boolean File::seek(uint32_t pos) {
  if (! _file) return false;
  return _file->seekSet(pos);
}
uint32_t File::position() {
  if (! _file) return -1;
  return _file->curPosition();
}
uint32_t File::size() {
  if (! _file) return 0;
  return _file->fileSize();
}
void File::close() {
  if (_file) {
    _file->close();
    free(_file);
    _file=0;
    /* for debugging file open/close leaks
    nfilecount--;
    Serial.print("Deleted ");
    Serial.println(nfilecount, DEC);
    */
  }
}
File::operator bool() {
  if (_file) return _file->isOpen();
  return false;
}

我正在尝试使用read制作SD卡write package Simulink。这里发生的是我将SD.cppSD.h以及其他必需的files并创建一个wrapper file,其中包含所需的操作(即读取和写入SD卡)和我收到以下错误

SDcard_wrapper.cpp: In function 'void SDcard_Update_wrapper(const boolean_T*, real_T*, const uint8_T*, int_T)':
./SDcard_wrapper.cpp:113: error: no match for 'operator=' in 'myFile = SDClass::open(const char*, uint8_t)(((const char*)"test.txt"), 19u)'
C:\Users\Samanth\Documents\MATLAB\newSD-trail/utility/SdFat.h:135: note: candidates are: SdFile& SdFile::operator=(const SdFile&)
./SDcard_wrapper.cpp:123: error: could not convert 'myFile' to 'bool'

以下是SDcard_wrapper.cpp错误位置:


myFile = SD.open("test.txt", FILE_WRITE); //This is the line where I am getting 1st error

if (myFile) // this is the point where I am getting the error: could not convert 'myFile' to 'bool'
{
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
} else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
}

File SDClass::open(const char* filepath, uint8_t mode) {
    /*

         Open the supplied file path for reading or writing.

         The file content can be accessed via the `file` property of
         the `SDClass` object--this property is currently
         a standard `SdFile` object from `sdfatlib`.

         Defaults to read only.

         If `write` is true, default action (when `append` is true) is to
         append data to the end of the file.

         If `append` is false then the file will be truncated first.

         If the file does not exist and it is opened for writing the file
         will be created.

         An attempt to open a file for reading that does not exist is an
         error.

       */

    int pathidx;

    // do the interative search
    SdFile parentdir = getParentDir(filepath, &pathidx);
    // no more subdirs!

    filepath += pathidx;

    if (!filepath[0]) {
        // it was the directory itself!
        return File(parentdir, "/");
    }

    // Open the file itself
    SdFile file;

    // failed to open a subdir!
    if (!parentdir.isOpen())
        return File();

    // there is a special case for the Root directory since its a static dir
    if (parentdir.isRoot()) {
        if (!file.open(SD.root, filepath, mode)) {
            // failed to open the file :(
            return File();
        }
        // dont close the root!
    } else {
        if (!file.open(parentdir, filepath, mode)) {
            return File();
        }
        // close the parent
        parentdir.close();
    }

    if (mode & (O_APPEND | O_WRITE))
        file.seekSet(file.fileSize());
    return File(file, filepath);
}

class SdFile : public Print // This is the line pointed above in the second error just for information SdFat.h:135: note: candidates are: SdFile& SdFile::operator=(const SdFile&)
{
    public:
    /** Create an instance of SdFile. */
        SdFile(void): type_(FAT_FILE_TYPE_CLOSED) {}
        /**
         * writeError is set to true if an error occurs during a write().
         * Set writeError to false before calling print() and/or write() and check
         * for true after calls to print() and/or write().
         */
        //bool writeError;

    /**
     * Cancel unbuffered reads for this file.
     * See setUnbufferedRead()
     */
        void clearUnbufferedRead(void) {
        flags_ &= ~F_FILE_UNBUFFERED_READ;
    }
    uint8_t close(void);
    uint8_t contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
    uint8_t createContiguous(SdFile* dirFile, const char* fileName, uint32_t size);
    /** \return The current cluster number for a file or directory. */
    uint32_t curCluster(void) const {
        return curCluster_;
    }
    /** \return The current position for a file or directory. */
    uint32_t curPosition(void) const {
        return curPosition_;
    }

    static void dateTimeCallback(void(*dateTime)(uint16_t* date, uint16_t* time)) {
        dateTime_ = dateTime;
    }
    /**
     * Cancel the date/time callback function.
     */
    static void dateTimeCallbackCancel(void) {
        // use explicit zero since NULL is not defined for Sanguino
        dateTime_ = 0;
    }

0 个答案:

没有答案