error: no match for operator ==

2020-05-09 19:32发布

问题:

I'm receiving an error for multiple areas of my three files. (I'm also having problems with my default constructor, which is why it is commented out. But I want to solve this problem first)

// ComputerType.h
//************************************************************************
// This file gives the specification of a ComputerType abstract data type
//************************************************************************
//  #ifndef COMPUTERTYPE_H
    //  #define COMPUTERTYPE_H

      #include<fstream>
      #include<iostream>
      #include<iomanip>
      #include<string>
      #include<cctype>

      using namespace std;

      class ComputerType
      {
       public:

    //     ComputerType();
         //******************************************************************************
         // Default Constructor.
         // Purpose: Initialize the data by default value.
         // Input: None.
         // Precondition: None.

  ComputerType(int initID, string initPrcsr, string initRAMsize, string initDiskSize, string initCompType, float initcost, string initLab);
     //*******************************************************************************
     // Purpose: To initialize the data.
     // Input: InitID, initPrcsr, initRAMSize, initDiskSize, initCompType, initCost, initLab.
     // Precondition: initID, initPrcsr, initRAMSize, initDiskSize, initCompType, initCost, initLab have values.
     // Output: None.
     // PostCondition: Class object is constructed && Computer is set to incoming parameters.
     // Note: None.
     //********************************************************************************

     void SetComputer(int ID, string processorDesc, string RAM, string disk, string type, float compCost, string lab);
     //*******************************************************************************
     // Purpose: To set object data to value for use.
     // Input: ID, processorDesc, RAM, disk, type, compCost, lab.
     // Precondition: ID, processorDesc, RAM, disk, type, compCost, lab have values.
     // Output: None.
     // PostCondition: Object values are set to incoming parameters.
     // Note: None.
     //*******************************************************************************
 void SetComputerType(string& computerTypeConvert);
     //*******************************************************************************
     // Purpose: To set private data computerType to value for use.
     // Input: computerTypeConvert.
     // Precondition: typeConvert has value.
     // Output: None.
     // PostCondition: Object value computerType is set to incoming parameters.
     // Note: None.
     //*******************************************************************************

 string Processorls();
     //******************************************************************************
     //Purpose: Function Processorls returns the computer processor.
     //Input: None.
     //Pre: Object exists.
     //Output: string.
     //Post: processor is returned.
     //Note: None.
     //******************************************************************************

  string Locationls();
     //******************************************************************************
     //Purpose: Returns the computer.s lab location.
     //Input: None.
     //Pre: Object exists.
     //Output: string.
     //Post: string labLocation is returned.
     //Note:  None
     //******************************************************************************

     void ReadRd(ifstream& inFile);
     //******************************************************************************
     //Purpose: Function reads idNumber, processor, RAMSize, DiskSize, computerType,
     // cost, and labLocation from file, and assigns them to object variables.
     //Input: inFile.
     //Pre: inFile file open and OK.
     //Output: None.
     //Post: idNumber, processor, RAMSize, DiskSize, computerType, cost, and labLocation are stored from //file into object.
     //Note:  None.
  void PrintRd(ofstream& outFile);
     //******************************************************************************
     //Purpose: Function PrintRd prints idNumber, processor, RAMSize, DiskSize, computerType, cost and //labLocation to a file.
     //Input: outFile.
     //Pre: OutFile open and ok.
     //Output: float, string.
     //Post: function prints data to outFile.
     //Note:  none.
     //*****************************************************************************
  private:

     int idNumber;
     string processor;
     string RAMSize;
     string DiskSize;
     string computerType;
     float cost;
     string labLocation;
  };
//*****************************************************************************************

implementation file:

 //ComputerType.cxx
  //*********************************************************
  // This file implements the ComputerType member functions
  //*********************************************************
  #include "ComputerType.h"
  #include <iostream>
  #include <iomanip>
  #include <fstream>
  #include <cctype>
  #include <string>
  using namespace std;

  // Private members of class:
  // int idNumber;
  // string processor;
  // string RAMSize;
  // string DiskSize;
  // string computerType;
  // float cost;
  // string labLocation

//  ComputerType::ComputerType()
  //******************************************************************************
  // Default Constructor.
  // Purpose: Initialize the data by default value.
 // Precondition: None.
  // Output: None.
  // Postcondition: idNumber == -5555 && processor == NONE & RAMSize == Zero && DiskSize == Zero &&
  // computerType == NONE && cost == -777.00 && labLocation = NONE.
  // Note: None.
  //******************************************************************************
 // {
   // idNumber = -5555;
   // processor = "NONE";
   // RAMSize = "Zero";
   // DiskSize = "Zero";
   // computerType = "NONE";
   // cost = -777.00;
   // labLocation = "NONE";
 // }
  //***********************************************************************************8

  ComputerType::ComputerType(int initID, string initPrcsr, string initRAMSize, string initDiskSize, string initCompType, float initCost, string initLa$
  //*******************************************************************************
  // Purpose: To initialize the data.
  // Input: InitID, initPrcsr, initRAMSize, initDiskSize, initCompType, initCost, initLab.
  // Precondition: initID, initPrcsr, initRAMSize, initDiskSize, initCompType, initCost, initLab  have values.
  // Output: None.
  // PostCondition: Class object is constructed && private data is set to incoming parameters.
  // Note: None.
{
    idNumber = initID;
    processor = initPrcsr;
    RAMSize = initRAMSize;
    DiskSize = initDiskSize;
    computerType = initCompType;
    cost = initCost;
    labLocation = initLab;

  }
  //********************************************************************************

  void ComputerType::SetComputer(int ID, string processorDesc, string RAM, string disk, string type, float compCost, string lab)
  //*******************************************************************************
  // Purpose: To set object data to value for use.
  // Input: ID, processorDesc, RAM, disk, type, compCost.
  // Precondition: ID, processorDesc, RAM, disk, type, compCost, lab have values.
  // Output: None.
  // PostCondition: Object values are set to incoming parameters.
  // Note: None.
  //*******************************************************************************
  {
    idNumber = ID;
    processor = processorDesc;
    RAMSize = RAM;
 DiskSize = disk;
    computerType = type;
    cost = compCost;
    labLocation = lab;
  }
  //********************************************************************************

  void ComputerType::SetComputerType(string& computerTypeConvert)
  //*******************************************************************************
  // Purpose: To set private data computerType to value for use.
  // Input: typeconvert.
  // Precondition: typeConvert has value.
  // Output: None.
  {
    if(computerType == 'd' || computerType == 'D' || computerType == "desktop" || computerType == "Desktop" || computerType == "DESKTOP")
     {
       computerTypeConvert = "Desktop";
     }
    if(computerType == 'l' || computerType == 'L' || computerType == "laptop" || computerType == "Laptop" || computerType == "LAPTOP")
     {
      computerTypeConvert = "Laptop";
     }

    else
     computerTypeConvert = "Invalid input value";

    computerType = computerTypeConvert;
  }
  //*********************************************************************

  string ComputerType::Processorls()
  //******************************************************************************
  //Purpose: Function Processorls returns the computer processor.
  //Input: None.
  //Pre: Object exists.
  //Output: string.
 {
    return processor;
  }
  //******************************************************************************

  string ComputerType::Locationls()
  //******************************************************************************
  //Purpose: Returns the computer.s lab location.
  //Input: None.
  //Pre: Object exists.
 //Post: string labLocation is returned.
  //Note:  None.
  //******************************************************************************
  {
    return labLocation;
  }
  //******************************************************************************

  void ComputerType::ReadRd(ifstream& inFile)
  //******************************************************************************
  //Purpose: Function reads idNumber, processor, RAMSize, DiskSize, computerType, cost, and //labLocation from file, and assigns them to object variab$
  //Input: inFile.
  //Pre: inFile file open and OK.
  //Output: None.
  //Post: idNumber, processor, RAMSize, DiskSize, computerType, cost, and labLocation are stored from //file into object.
  //Note:  None.
  //******************************************************************************

  {
    int RAMSizeInt;
    string RAMSizeString;
    int DiskSizeString;
    string DiskSizeInt;

    inFile >> idNumber >> processor >> RAMSizeInt >> RAMSizeString >> DiskSizeInt >> DiskSizeString >> computerType >> cost >> labLocation;
 RAMSize = RAMSizeInt & RAMSizeString;
    DiskSize = DiskSizeInt & DiskSizeString;
  }
   //******************************************************************************

  void ComputerType::PrintRd(ofstream& outFile)
  //******************************************************************************
  //Purpose: Function PrintRd prints idNumber, processor, RAMSize, DiskSize, computerType, cost and //labLocation to a file.
  //Input: outFile.
  //Pre: OutFile open and ok.
  //Output: float, string.
  //Post: function prints data to outFile.
  //Note:  none.
 {
    outFile << setw(14) << idNumber << setw(25) << processor << setw(15) << RAMSize  << setw(18) << DiskSize;
    outFile << setw(19) << computerType << setw(9) << cost << setw(7) << labLocation << endl;
  }
   //*****************************************************************************

client code:

 #include "ComputerType.h"
  #include <iostream>
  #include <fstream>
  #include <iomanip>
  #include <cctype>
  #include <string>
  using namespace std;

  int main()
  {
   // declare and open file streams
   ifstream inFile;
   ofstream outFile;

   inFile.open("in.data");
   outFile.open("out.data");
   outFile.setf(ios::fixed);
   outFile.precision(2);

   ComputerType rd;
   string computerTypeConvert;

   computertypeConvert = "type";


 // Check input file
  // if(inFile)
  // {
    // if(outFile)
    // {
     // while(inFile)
    // {
      // function calls for reading and assigning values to private data
      rd.ReadRd(inFile);
      rd.SetComputerType(computerTypeConvert);

      // Print to outFile
      outFile << setw(14) << "ID Number" << setw(25) <<  "P R 0 C E S S O R" << setw(15) << "RAM     Size";
      outFile << setw(18) << "Disk Size" << setw(19) << "Computer Type" << setw(9) << "Cost";
      outFile << setw(7) << "Location" << endl;
      rd.PrintRd(outFile);
    //  }
   return 0;
  }

Here is my error list:

 cxx -o runComputerType.out ComputerType.cxx runComputerType.cxx
ComputerType.cxx: In member function âvoid ComputerType::SetComputerType(std::string&)â:
ComputerType.cxx:95: error: no match for âoperator==â in â((ComputerType*)this)->ComputerType::computerType == 'd'â
ComputerType.cxx:95: error: no match for âoperator==â in â((ComputerType*)this)->ComputerType::computerType == 'D'â
ComputerType.cxx:99: error: no match for âoperator==â in â((ComputerType*)this)->ComputerType::computerType == 'l'â
ComputerType.cxx:99: error: no match for âoperator==â in â((ComputerType*)this)->ComputerType::computerType == 'L'â
ComputerType.cxx: In member function âvoid ComputerType::ReadRd(std::ifstream&)â:
ComputerType.cxx:156: error: no match for âoperator&â in âRAMSizeInt & RAMSizeStringâ
/usr/include/c++/4.3/bits/ios_base.h:81: note: candidates are: std::_Ios_Fmtflags std::operator&(std::_Ios_Fmtflags, std::_Ios_Fmtflags)
/usr/include/c++/4.3/bits/ios_base.h:121: note:                 std::_Ios_Openmode std::operator&(std::_Ios_Openmode, std::_Ios_Openmode)
/usr/include/c++/4.3/bits/ios_base.h:159: note:                 std::_Ios_Iostate std::operator&(std::_Ios_Iostate, std::_Ios_Iostate)
ComputerType.cxx:157: error: no match for âoperator&â in âDiskSizeInt & DiskSizeStringâ
/usr/include/c++/4.3/bits/ios_base.h:81: note: candidates are: std::_Ios_Fmtflags std::operator&(std::_Ios_Fmtflags, std::_Ios_Fmtflags)
/usr/include/c++/4.3/bits/ios_base.h:121: note:                 std::_Ios_Openmode std::operator&(std::_Ios_Openmode, std::_Ios_Openmode)
/usr/include/c++/4.3/bits/ios_base.h:159: note:                 std::_Ios_Iostate std::operator&(std::_Ios_Iostate, std::_Ios_Iostate)
runComputerType.cxx: In function âint main()â:
runComputerType.cxx:35: error: no matching function for call to âComputerType::ComputerType()â
ComputerType.h:32: note: candidates are: ComputerType::ComputerType(int, std::string, std::string, std::string, std::string, float, std::string)
ComputerType.h:17: note:                 ComputerType::ComputerType(const ComputerType&)

回答1:

Since computerType is a string, its operator==() is declared to compare with another string (enclosed by double quote "), not another character (enclosed by single quote ').

Thus,

computerType == 'd' || computerType == 'D'

should be rewritten as

computerType == "d" || computerType == "D"