C++缓冲区操作

C++ buffer manipulation

本文关键字:操作 缓冲区 C++      更新时间:2023-10-16

我的教授给了我们一个示例main\source.cpp文件,并希望我们创建必要的class\header文件来使其工作。他是没有示例代码模糊的"有用提示"类型,所以我迷路了。

我知道这是一个很长的帖子,任何可能愿意帮助我的输入都将是一个巨大的帮助。

资料来源.cpp他提供;

#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw
#include <string>
#include "DataBuffer.h"
using namespace std;
void testDataBuffer(int arr[], int length);
int main() { 
    const int ARR0_LEN = 2; 
    int arr0[ARR0_LEN] = { -2,-1 }; 
    const int ARR1_LEN = 10; 
    int arr1[ARR1_LEN] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
    const int ARR2_LEN = 25; int arr2[ARR2_LEN] = { 2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22 }; 
    testDataBuffer(arr0, ARR0_LEN); 
    testDataBuffer(arr1, ARR1_LEN); 
    testDataBuffer(arr2, ARR2_LEN); 
    //hold console open
    std::cin.get();
    return 0; 
}
void testDataBuffer(int arr[], int length) {
    DataBuffer buf; buf.copyData(arr, length); 
    buf.print(); cout << "Sum " << buf.sum() << endl; 
    cout << "Min " << buf.minValue() << endl; 
    cout << "Max " << buf.maxValue() << endl; 
    cout << "Range " << buf.range() << endl; 
    cout << "Mean " << buf.mean() << endl;
}

My DataBuff.h

#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw
#include <string>
#pragma once
class DataBuffer {
        static const int BUFFER_SIZE = 256;
        int buffer[BUFFER_SIZE];
        static const int length = 5;
        int arr[length] = { 1,2,3,4,5 };
    public:
        bool copyData(int intArray, int length);
        string print();
        double mean(int sum);
        int sum();
        int maxValue();
        int minValue();
        int range(int small, int large);
 };

我的数据缓冲区.cpp

#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw
#include <string>
#include <array>        // .sizeof()
#include "DataBuffer.h"
using namespace std;
bool DataBuffer::copyData(int arr, int length)  {   
    for (int i = 0; i < length; i++) {
        arr = buffer[i];  
    }
    cout << "Length of buffer is " << length; 
    if (sizeof(buffer) < length) {
        return false;
    }
    else {
        return true;
    }
}
string DataBuffer::print() { 
    if (length <= 0) {
        cout << "{}" << endl;
    }
    else {
        cout << buffer[0];
        for (int i = 1; i < length; i++) {
            cout << setw(10) << buffer[i];
            cout << endl;
        }
    }
}
int DataBuffer::sum()
{
    int i, sum = 0;
    for (i = 0; i < length; i++)
    {
        sum += buffer[i];
    }
    return sum;
}
double DataBuffer::mean(int sum) {
    double mean = sum / length;
    return mean;
}
int DataBuffer::maxValue() {
    int i = 0;
    int large = buffer[0];
    for (i = 1; i < length; i++)
    {
        if (buffer[i] > large)
            large = buffer[i];
    }
    return large;
}
int DataBuffer::minValue() {
    int i; 
    int small = buffer[0];
    for(i=1; i < length; i++)
    {
        if(buffer[i] < small)
            small = buffer[i];
    }
    return small;
}
int DataBuffer::range(int small, int large)
{
    int range = large - small;
    return range;
}

我坚持的主要错误;

教授指出范围和平均值应该接受参数,那么为什么他的 cpp 中的方法调用没有参数呢?我收到说明range doesn't take 0 arguments的错误

错误 C3646 'print': 未知覆盖说明符 AustinNorrisBuffer c:\users\austinn\downloads\austinnorrisbuffer\databuffer.h 19
错误 C2039 "print": 不是 'DataBuffer' 的成员 AustinNorrisBuffer c:\users\austinn\downloads\austinnorrisbuffer\source.cpp 29

我知道的又是长帖子,任何帮助都值得赞赏!

我建议采用以下方法(以便您了解教授的想法并在此过程中保持理智):

  • 注释掉所有内容。
  • 取消注释选定的代码段。
  • 使用搜索引擎来研究这些特定错误。修复代码/标头并继续。
  • 如果您遇到一个非常具体的问题并需要帮助,那么您可以在此处发布一个格式良好的问题,您将很快得到具体的答案。

祝你好运。

以下是我必须注释掉的内容,以便至少对其进行编译:

DataBuff.h

#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw
#include <string>
#pragma once
class DataBuffer {
    static const int BUFFER_SIZE = 256;
    int buffer[BUFFER_SIZE];
    static const int length = 5;
    int arr[length] ={1,2,3,4,5};
public:
    bool copyData(int intArray,int length);
    //string print();
    double mean(int sum);
    int sum();
    int maxValue();
    int minValue();
    int range(int small,int large);
};

数据缓冲区.cpp

#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw
#include <string>
#include <array>        // .sizeof()
#include "DataBuff.h"
using namespace std;
bool DataBuffer::copyData(int arr,int length)
{
    for (int i = 0; i < length; i++) {
        arr = buffer[i];
    }
    cout << "Length of buffer is " << length;
    if (sizeof(buffer) < length) {
        return false;
    }
    else {
        return true;
    }
}
//string DataBuffer::print()
//{
    //if (length <= 0) {
    //  cout << "{}" << endl;
    //}
    //else {
    //  //cout << buffer[0];
    //  for (int i = 1; i < length; i++) {
    //      //cout << setw(10) << buffer[i];
    //      cout << endl;
    //  }
    //}
//}
int DataBuffer::sum()
{
    int i,sum = 0;
    for (i = 0; i < length; i++) {
        sum += buffer[i];
    }
    return sum;
}
double DataBuffer::mean(int sum)
{
    double mean = sum / length;
    return mean;
}
int DataBuffer::maxValue()
{
    int i = 0;
    int large = buffer[0];
    for (i = 1; i < length; i++) {
        if (buffer[i] > large)
            large = buffer[i];
    }
    return large;
}
int DataBuffer::minValue()
{
    int i;
    int small = buffer[0];
    for (i=1; i < length; i++) {
        if (buffer[i] < small)
            small = buffer[i];
    }
    return small;
}
int DataBuffer::range(int small,int large)
{
    int range = large - small;
    return range;
}

资料来源.cpp

#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setw
#include <string>
#include "DataBuff.h"
using namespace std;
void testDataBuffer(int arr[],int length);
int main()
{
    const int ARR0_LEN = 2;
    int arr0[ARR0_LEN] ={-2,-1};
    const int ARR1_LEN = 10;
    int arr1[ARR1_LEN] ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    const int ARR2_LEN = 25; int arr2[ARR2_LEN] ={2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22};
    testDataBuffer(arr0,ARR0_LEN);
    testDataBuffer(arr1,ARR1_LEN);
    testDataBuffer(arr2,ARR2_LEN);
    //hold console open
    std::cin.get();
    return 0;
}
void testDataBuffer(int arr[],int length)
{
    DataBuffer buf; 
    //buf.copyData(arr,length);
    //buf.print(); cout << "Sum " << buf.sum() << endl;
    //cout << "Min " << buf.minValue() << endl;
    //cout << "Max " << buf.maxValue() << endl;
    //cout << "Range " << buf.range() << endl;
    //cout << "Mean " << buf.mean() << endl;
}