【问题标题】:how do I get this counter function in C++ working?如何在 C++ 中使用此计数器功能?
【发布时间】:2014-12-04 14:11:55
【问题描述】:

我正在尝试设置一个计数器功能,该功能将计算输入文件中的项目数并在消息框中显示答案。我想我已经接近了,但我似乎无法让它正常工作。 这是我的结构。

#pragma region Global Declarations

#include <iostream>
#include <fstream>
#include <iomanip>

struct PetData
{
    int IdNumber;
    char PetType[25];
    double PetPrice;

    //Count and display the number of items in the linked list
    int CountItems;
    PetData * Link;
};
PetData *Headpointer = NULL;

ifstream DataFile;
ofstream FileOut;

//Create a report listing the records
//that currently comprise the linked list
void ListRecords ( char * );

void InsertItem ( int, char[],double, PetData* );
void OutputItem ( PetData*);



#pragma endregion

这是我的柜台

 OutputItem ( Headpointer );
 //FinalMessage->Visible=true;

 Headpointer->ListRecords ( OutPutFileName );

 MessageBox::Show ("Listed Printed To Output File \n"
             + Headpointer->CountItems + " Items Were Printed",
             "Report Created", MessageBoxButtons::OK,
             MessageBoxIcon::Information);
         //cleanup
 DataFile.close( );
 FileOut.close( );
 delete CurrentRecordPointer;
 Headpointer = NULL;

感谢您提供的任何帮助。谢谢

【问题讨论】:

  • 您能否比“我似乎无法正常工作”更具体一些?

标签: c++ linked-list counter


【解决方案1】:

你可能需要类似的东西吗?

#pragma region Global Declarations


#include <Windows.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>

class LinkedList
{
public:
    struct PetData
    {
        int IdNumber;
        char PetType[25];
        double PetPrice;

        //Count and display the number of items in the linked list        
        PetData * Link;
    };
private:

    PetData *Headpointer;
    ifstream DataFile;
    ofstream FileOut;

    void DeleteItems();
public:

    LinkedList():Headpointer(NULL)
    {        
        OutputItem ( Headpointer );
    };

    ~LinkedList()
    {
        DataFile.close( );
        FileOut.close( );
        DeleteItems();
    }

    //Create a report listing the records
    //that currently comprise the linked list
    void ListRecords (const char * );

    void InsertItem ( int, char[],double, PetData* );    
    void OutputItem ( PetData*);
    int CountItems();    
};



#pragma endregion



        const char * OutPutFileName="out.file";
        LinkedList * list = new LinkedList();        
         //FinalMessage->Visible=true;

         list->ListRecords ( OutPutFileName );

         wstringstream ss;
         ss<<"Listed Printed To Output File \n" << list->CountItems() << " Items Were Printed";
         MessageBox(NULL, ss.str().c_str(), TEXT("Report Created"), MB_OK|MB_ICONINFORMATION);
         //cleanup

【讨论】:

    【解决方案2】:

    你有这个:

    Headpointer->ListRecords ( OutPutFileName );
    

    相当于

    (*Headpointer).listRecords(OutputFileName);
    

    但是 *Headpointer 是一个 PetData,它没有 listRecords 函数。该函数在结构外部声明。

    【讨论】:

      猜你喜欢
      • 2021-12-30
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多