未定义和重新定义的错误

时间:2014-09-27 22:40:53

标签: c++ list object include

我有一个客户端代码文件,包含三个不同对象类型的三个.h文件(UnsortedType,CompData,ItemType)。 UnsortedType.h也有一个#include ItemType.h

在包含客户端代码的标头保护和UnsortedType文件中的相同保护之后,我留下的唯一错误是函数ObjectFromFile的模糊编译错误。 当我注释掉这个函数时,我得到了UnsortedType和ItemType数据的几个Undefined错误,所以我觉得还有其他的事情发生了。

此程序用于从inFile对象ItemType项中引入数据,转换为特定格式并检查有效性,然后将数据添加到UnsortedType的特定列表,具体取决于 数据的某些方面。 (有效,无效,RAM和磁盘大小,笔记本电脑数据,桌面数据)。

这是我的错误:

runItemType.cxx: In function âUnsortedType ObjectFromFile(std::ifstream&, UnsortedType&,     CompDataType, ItemType&, UnsortedType&, UnsortedType&, UnsortedType&)â:
runItemType.cxx:215: error: âCompareRAMAndDiskâ was not declared in this scope

这是我的客户代码:

#include <iostream>
  #include <fstream>
  #include <iomanip>
  #include <string>
  #include "UnsortedType.h"
  #include "CompData.h"
  #ifndef ITEM_TYPE_H
  #define ITEM_TYPE_H
  #include "ItemType.h"
  using namespace std;

  UnsortedType ObjectFromFile(ifstream& inFile, UnsortedType& list, CompDataType Data, ItemType& item, UnsortedType& laptopList, UnsortedType& desktopList, UnsortedTyp$
  void CompareRAMAndDisk(ItemType item, UnsortedType& RAMList);
  void DoCommandP(ItemType& item, UnsortedType list, CompDataType Data, ofstream& outFile);
  void DoCommandK(ofstream& outFile, UnsortedType& desktopList);
  void DoCommandL(ofstream& outFile, UnsortedType& laptopList);
  void DoCommandM(ofstream& outFile, UnsortedType RAMList);
  void DoCommandD(UnsortedType& list, ItemType item);
  UnsortedType DoCommandA(ifstream& inFile, UnsortedType& list, CompDataType Data, ItemType& item, UnsortedType& laptopList, UnsortedType& desktopList);
  void PrintHeader(ofstream& outFile);

  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);

   ItemType item;
   UnsortedType list;
   UnsortedType laptopList;
   UnsortedType desktopList;
   UnsortedType RAMList;
   UnsortedType invalidList;
   CompDataType Data;
   char command;
   int length;
   bool full;
   bool found;

   // Check input file
   if(inFile.fail() || outFile.fail())
   {
     cout << "File Failure" << endl;
     return 1;
   }

   list.MakeEmpty();
   outFile << "*~< Lab Report >~" << endl;

   while(inFile)
   {
     inFile >> command;
     if(command == 'P')
     {
       invalidList.PrintList(outFile);
       outFile << endl;
 length = list.LengthIs();
       if(length == 0)
       {
         outFile << "~->List is empty! No print!" << endl;
       }
     }
     else
     {
       PrintHeader(outFile);

       DoCommandP(item, list, Data, outFile);
     }

   if(command = 'A')
   {
     full = list.IsFull();
     if(full)
       outFile << "~->List is full! No add!" << endl;
     else
 invalidList = DoCommandA(inFile, list, Data, item, laptopList, desktopList);
   }
   if(command == 'D')
   {
     length = list.LengthIs();
     if(length == 0)
       outFile << "~->List is empty! No delete!" << endl;
     else
       DoCommandD(list, item);
   }
   if(command == 'L')
   {
     DoCommandL(outFile, laptopList);
   }
   if(command == 'K')
   {
     DoCommandK(outFile, desktopList);
   }
     if(command == 'M')
     {
       DoCommandM(outFile, RAMList);
     }
   }
   outFile << "*< end >*" << endl;
   inFile.close();
   outFile.close();
   return 0;
  }
#endif



 //*****************************************************************************************************************************
  void DoCommandP(ItemType& item, UnsortedType list, CompDataType Data, ofstream& outFile)
  {

   int length;
   length = list.LengthIs();
   list.PrintList(outFile);

   outFile << endl;
   if(Data.numDesktop > 0)
   outFile << "Average cost of Desktops: $" << item.avgCostOfDesktop() << endl;

   else
     outFile << "Average cost of Desktops: " << "No Desktops" << endl;

   if(Data.numLaptop > 0)
     outFile << "Average cost of Laptops: $" << item.avgCostOfLaptop() << endl;

   else
     outFile << "Average cost of Laptops: " << "No Laptops" << endl;

   if(Data.numDesktop > 0 || Data.numLaptop > 0)
   {
     outFile << "High Price: $" << item.HighPriceComputer() << endl;
     outFile << "Low Price: $" << item.LowPriceComputer() << endl;
   }
 else
   {
     outFile << "High Price: " << "No valid computer data" << endl;
     outFile << "LowPrice: " << "No valid computer data" << endl;
   }
   outFile << endl;

   outFile << "SE135: %" << (Data.count135 / length);
   outFile << "SE136: %" << (Data.count136 / length);
   outFile << "SE145: %" << (Data.count145 / length);
   outFile << endl;
   outFile << "*** " << Data.invalidCount << " Record";
   outFile << endl;
   }

  //**********************************************************************************************
  UnsortedType ObjectFromFile(ifstream& inFile, UnsortedType& list, CompDataType Data, ItemType& item, UnsortedType& laptopList, UnsortedType& desktopList, UnsortedTyp$
  {
    UnsortedType invalidList;
    Data.computerTypeConvert = "l";
    Data.invalidCount = 0;
    Data.numDesktop = 0;
    Data.numLaptop = 0;
    Data.count135 = 0;
    Data.count136 = 0;
    Data.count145 = 0;

    item.ReadRd(inFile);
    while(inFile)
    {
     // function calls for reading and assigning values to private data
     Data.valid = item.ValidData();
     item.SetComputerType(Data.computerTypeConvert);

     // Check data validity
     if(Data.valid)
     {
       Data.computerType = item.ComputerTypels();
 Data.location = item.Locationls();
       if(Data.computerType == "l")
       {
         item.FindAvgCostLaptop();
         Data.numLaptop = Data.numLaptop + item.NumberComputerls();
         laptopList.InsertItem(item);
       }
       if(Data.computerType == "d")
       {
         item.FindAvgCostDesktop();
         Data.numDesktop = Data.numDesktop + item.NumberComputerls();
         desktopList.InsertItem(item);
       }
       if(Data.numDesktop + Data.numLaptop > 0)
       {
         item.FindLowPriceComputer();
         item.FindHighPriceComputer();
       }
       if(Data.location == "SE135")
         Data.count135++;
       if(Data.location == "SE136")
         Data.count136++;
       if(Data.location == "SE145")
         Data.count145++;
       CompareRAMAndDisk(item, RAMList);
       list.InsertItem(item);
       item.ReadRd(inFile);
     }
     else
     {
      Data.invalidCount++;
      invalidList.InsertItem(item);
     }
    }
     return invalidList;
   }
  //*********************************************************************************************
  void PrintHeader(ofstream& outFile)
 {
    const string DASH8 = "--------";
    outFile << "*~< Lab Report >~*" << endl << endl;
    outFile << left << setw(16) << "IdNumber" << setw(24) <<  "P R 0 C E S S O R" << setw(17) << "RAM Size";
    outFile << left << setw(15) << "Disk Size" << setw(17) << "Computer Type" << setw(12) << "C o s t";
    outFile << left << setw(9) << "Location" << endl;
    outFile << left << setw(16) << DASH8 << setw(24) <<  "-----------------" << setw(17) << DASH8;
    outFile << left << setw(15) << "---------" << setw(17) << "-------------" << setw(12) << DASH8;
    outFile << left << setw(9) << DASH8 << endl;

  }
  //**************************************************************************************************************
  UnsortedType DoCommandA(ifstream& inFile, UnsortedType& list, CompDataType Data, ItemType& item, UnsortedType& RAMList, UnsortedType& laptopList, UnsortedType& deskt$
  {
     UnsortedType invalidList;
     invalidList = ObjectFromFile(inFile, list, Data, item, laptopList, desktopList, RAMList);
     return invalidList;
  }
  //*******************************************************************************************
  void DoCommandD(UnsortedType& list, ItemType item)
  {
    list.DeleteItem(item);
  }
  //******************************************************************************************
  void DoCommandK(ofstream& outFile, UnsortedType& desktopList)
  {
    desktopList.PrintList(outFile);
  }
  //******************************************************************************************
  void DoCommandL(ofstream& outFile, UnsortedType& laptopList)
  {
    PrintHeader(outFile);
    laptopList.PrintList(outFile);
  }
  //******************************************************************************************
  void DoCommandM(ofstream& outFile, UnsortedType RAMList)
  {
    bool ramData;
 PrintHeader(outFile);
    RAMList.PrintList(outFile);
  }
  //******************************************************************************************
  void CompareRAMAndDisk(ItemType item, UnsortedType RAMList)
  {
    string RAMInt;
    string DiskInt;
    string RAM;
    string Disk;
    int index;

    Disk = item.Diskls();
    RAM = item.RAMls();
    index = Disk.find(' ');
    DiskInt = Disk.substr(0, (index - 1));
    Disk = Disk.substr(index);

    index = RAM.find(' ');
    RAMInt = RAM.substr(0, (index - 1));
    RAM = RAM.substr(index);

    if((RAMInt >= "16" && RAM == "GB") && (DiskInt == "2" && Disk == "TB"))
    RAMList.InsertItem(item);
  }

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

欢迎任何帮助! (另外,如果你看到逻辑错误,请告诉我!我一般可以自己修复它们,但是我越快完成它就越好!)

2 个答案:

答案 0 :(得分:0)

你的功能声明是:

void CompareRAMAndDisk(ItemType item, UnsortedType& RAMList);

,您的函数定义是:

void CompareRAMAndDisk(ItemType item, UnsortedType RAMList);

看到区别?从它的外观来看,您想要将功能定义更改为使用UnsortedType &

答案 1 :(得分:0)

主要问题是我的编译方式; 我已经解决了这个问题。