Seite 1 von 1

Fehler bei der Operatorenüberladung

Verfasst: Fr Nov 04, 2011 11:05 pm
von Bebu
Hallo, ich bin mal wieder verzweifelt dabei einen Fehler zu finden... die Wald vor lauter Bäumen Nummer vermute ich mal wieder. Dedupe::FileInfo, hier mal der Header(verkürzt):

Code: Alles auswählen

#include <boost/filesystem.hpp>
#include <iostream>

  class FileInfo
  {
    public:

   

    /**
      *Use the filepath to construct this class
      *constructor with Dedupe::FilePath (typedef of boost::filesystem::path)
      */
    FileInfo( Dedupe::FilePath PathToFile );

    FileInfo( Dedupe::FilePath PathToFile,
                    unsigned long long size,
                    time_t ChangeDate,
                    Dedupe::FileInfo::FileType Type );

    FileInfo( Dedupe::FilePath PathToFile,
                    unsigned long long size,
                    time_t ChangeDate,
                    Dedupe::FileInfo::FileType Type,
                    unsigned long long HashValue );

/*****************************************************************************/

    /**
      *@return path to file
      */
    Dedupe::FilePath GetPath() const;

    /**
      *@return size of the file in bytes
      */
    unsigned long long GetSize() const;

    /**
      *@return the date of the last file change
      */
    time_t GetDateChanged() const;

    /**
      *@return true, if file is existing
      */
    bool GetExisting() const;

    /**
      *@return one constant from FileType
      */
    Dedupe::FileInfo::FileType GetType() const;

/*****************************************************************************/

    /**
      *Sets the Hashvalue
      */
    void SetHash( unsigned long long const &value );

    /**
      *@return the Hash Value
      */
    unsigned long long GetHash() const;

/*****************************************************************************/

    /**
      *Set FileStatus
      */
    void SetStatus( FileStatus value );

    /**Get FileStatus
      *@return one constant from FileStatus
      */
    FileStatus GetStatus() const;

    /**
      *FileSearch and the FileInfo constructor can store some error messages
      *here. Error Messages are only stored, if GetStatus has value "IgnoreFile"
      */
    void SetErrorMessage( std::string const &message );

    /**
      *Get the error message, that is stored, if FileStatus is set to IgnoreFile
      */
    std::string GetErrorMessage() const;

/*****************************************************************************/
    /**
      *The operator is here to make std::sort() usable with FileInfo
      */
    bool operator < (const FileInfo & rhs ) const { return Path < rhs.Path; }

    friend bool operator== ( const Dedupe::FileInfo rhs, const Dedupe::FileInfo lhs);
    friend bool operator!= ( const Dedupe::FileInfo rhs, const Dedupe::FileInfo lhs);

    friend std::ostream& operator<<( std::ostream& os, const Dedupe::FileInfo& Info );


/*****************************************************************************/

    private:

    Dedupe::FilePath Path;
    unsigned long long Size;
    time_t DateChanged;
    bool Existing;
    Dedupe::FileInfo::FileType  Type;
    unsigned long long  Hash;
    FileStatus StateOfFile;
    std::string FileSearchErrorMessage;
  };

  /**
    *FileStream is a vector of Dedupe::Fileinfo
    */
  typedef std::vector<Dedupe::FileInfo> FileStream;
}

inline bool operator== ( const Dedupe::FileInfo rhs, const Dedupe::FileInfo lhs)
{
  return( rhs.Path == lhs.Path )
      && ( rhs.GetDateChanged() == lhs.GetDateChanged() )
      && ( rhs.GetExisting() == lhs.GetExisting() )
      && ( rhs.GetType() == lhs.GetType() )
      && ( rhs.GetHash() == lhs.GetHash() );
  }

inline bool operator!= ( const Dedupe::FileInfo rhs, const Dedupe::FileInfo lhs)
{
  return !( rhs == lhs );
}

inline std::ostream& operator<< ( std::ostream& os, const Dedupe::FileInfo& Info )
{
  os << Info.GetPath() << ":" << Info.GetDateChanged() << ":" << Info.GetExisting()
        << ":" << Info.GetType() << ":" << Info.GetHash();
  return os;
}
#endif
Hier die Problemkinder. Dieser Operator:

Code: Alles auswählen

inline bool operator== ( const Dedupe::FileInfo rhs, const Dedupe::FileInfo lhs)
{
  return( rhs.Path == lhs.Path )
      && ( rhs.GetDateChanged() == lhs.GetDateChanged() )
      && ( rhs.GetExisting() == lhs.GetExisting() )
      && ( rhs.GetType() == lhs.GetType() )
      && ( rhs.GetHash() == lhs.GetHash() );
  }
ist innerhalb der Klasse im public Bereich als friend gesetzt. Trotzdem verbietet mir der Kompiler den Zugriff auf die Privaten Elemente von FileInfo, wie z. B. die Path Variable. Das ist blöd, denn sonst muss ich für jeden Vergleich eine der Getter Funktionen aufrufen, was ja nicht gerade effizent ist. Außerdem verstehe ich nicht, warum er das macht, er ist doch friend.

Zweites Problem. Dieser Operator

Code: Alles auswählen

inline bool operator!= ( const Dedupe::FileInfo rhs, const Dedupe::FileInfo lhs)
{
  return !( rhs == lhs );
}
Ich bekomme folgenden Fehler vom Kompiler: ambiguous overload for 'operator==' in 'rhs == lhs' note: canidates are: note:bool operator==(Dedupe::FileInfo, Dedupe::FileInfo) note: bool Dedupe::operator==(Dedupe::FileInfo, Dedupe::FileInfo).

Als ich gegoogled habe, bin ich darauf gestoßen, dass der Kompiler hier vergeblich versucht implizit auf einen Typ zu casten, es ihm aber nicht gelingt. Jetzt bin ich aber völlig verwirrt, ich vergleiche doch einfach zwei FileInfo Objekte, oder bin ich blind?

Danke für eure Hilfe

Re: Fehler bei der Operatorenüberladung

Verfasst: Fr Nov 04, 2011 11:48 pm
von Kerli
Bebu hat geschrieben:Ich bekomme folgenden Fehler vom Kompiler: ambiguous overload for 'operator==' in 'rhs == lhs' note: canidates are: note:bool operator==(Dedupe::FileInfo, Dedupe::FileInfo) note: bool Dedupe::operator==(Dedupe::FileInfo, Dedupe::FileInfo).
Schau dir doch einmal genau die Signaturen der vorgeschlagenen Operatoren an. Damit solltest du auch dein erstes Problem, welches eigentlich das selbe Problem ist, lösen können. Schau vor allem auf die Namespaces ;)

Re: Fehler bei der Operatorenüberladung

Verfasst: Sa Nov 05, 2011 12:39 am
von Bebu
AAAARRRGGG Kopf auf Tischplatte :evil: Eine } versetzt und jetzt gehts. Manchmal könnte ich explodieren...