c – 编译器返回“合成方法’operator =’这里首先需要”

前端之家收集整理的这篇文章主要介绍了c – 编译器返回“合成方法’operator =’这里首先需要”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道这可能是一个简单的问题,但我在最后一个半小时一直在努力,我真的迷失了.

这里编译错误

  1. synthesized method File& File::operator=(const File&)’ first required here

我有这段代码

  1. void FileManager::InitManager()
  2. {
  3. int numberOfFile = Settings::GetSettings()->NumberOfFile() + 1;
  4.  
  5. for( unsigned int i = 1; i < numberOfFile; i++ )
  6. {
  7. std::string path = "data/data" ;
  8. path += i;
  9. path += ".ndb";
  10.  
  11. File tempFile( path );
  12.  
  13. _files.push_back( tempFile ); // line that cause the error
  14.  
  15. /*if( PRINT_LOAD )
  16. {
  17. std::cout << "Adding file " << path << std::endl;
  18. }*/
  19. }
  20. }

_files如果在此标头中定义:

  1. #pragma once
  2.  
  3. //C++ Header
  4. #include <vector>
  5.  
  6. //C Header
  7.  
  8. //local header
  9. #include "file.h"
  10.  
  11. class FileManager
  12. {
  13. public:
  14. static FileManager* GetManager();
  15. ~FileManager();
  16.  
  17. void LoadAllTitle();
  18.  
  19. private:
  20. FileManager();
  21. static FileManager* _fileManager;
  22.  
  23. std::vector<File> _files;
  24. };

File是我创建的对象,它只不过是一个处理文件IO的简单接口.我以前已经完成了用户定义对象的向量,但这是我第一次遇到这个错误.

这是File对象的代码
File.h

  1. #pragma once
  2.  
  3. //C++ Header
  4. #include <fstream>
  5. #include <vector>
  6. #include <string>
  7.  
  8. //C Header
  9.  
  10. //local header
  11.  
  12. class File
  13. {
  14. public:
  15. File();
  16. File( std::string path );
  17. ~File();
  18.  
  19. std::string ReadTitle();
  20.  
  21. void ReadContent();
  22. std::vector<std::string> GetContent();
  23.  
  24. private:
  25. std::ifstream _input;
  26. std::ofstream _output;
  27.  
  28. char _IO;
  29. std::string _path;
  30. std::vector<std::string> _content;
  31. };

File.cpp

  1. #include "file.h"
  2.  
  3. File::File()
  4. : _path( "data/default.ndb" )
  5. {
  6. }
  7.  
  8. File::File( std::string path )
  9. : _path( path )
  10. {
  11. }
  12.  
  13. File::~File()
  14. {
  15. }
  16.  
  17. void File::ReadContent()
  18. {
  19. }
  20.  
  21. std::string File::ReadTitle()
  22. {
  23. _input.open( _path.c_str() );
  24. std::string title = "";
  25.  
  26. while( !_input.eof() )
  27. {
  28. std::string buffer;
  29. getline( _input,buffer );
  30.  
  31. if( buffer.substr( 0,5 ) == "title" )
  32. {
  33. title = buffer.substr( 6 ); // 5 + 1 = 6... since we want to skip the '=' in the ndb
  34. }
  35. }
  36.  
  37. _input.close();
  38. return( title );
  39. }
  40.  
  41. std::vector<std::string> File::GetContent()
  42. {
  43. return( _content );
  44. }

我在linux下用gcc工作.

任何有关解决方案的提示提示都表示赞赏.

对不起,很长的帖子.

谢谢

解决方法

在C 03中,std :: vector< T>要求T是可复制构造和可复制分配的.文件包含标准流数据成员,标准流是不可复制的,因此File也是如此.

您的代码在C 11中可以正常工作(使用移动构造/移动分配),但是您需要避免将标准流对象按值保存为C 03中的数据成员.我建议将编译器升级支持C 11的编译器.移动语义或使用Boost’s smart pointers之一.

猜你在找的C&C++相关文章