C++缓冲区操作
C++ buffer manipulation
我的教授给了我们一个示例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;
}
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- C++字符*缓冲区的大小
- 重载操作程序时出错>>用于类中的字符串 memebr
- 无法对套接字执行操作,因为系统缺少足够的缓冲区空间或队列已满
- 安全操作以清除C++中的空输入缓冲区
- QML:读取由计算着色器操作的缓冲区
- 操作方法:将 boost::endian 缓冲区类型转换回本机格式
- 在Win32管道上将两个缓冲区合并为一个写操作
- 在不使用缓冲区的情况下对2个图像进行操作(加法、减法等)
- 在MS Visual Studio 2010上使用带有Cuda互操作的OpenGL缓冲区时出现异常错误
- 从 C++ 中的函数操作字符数组缓冲区
- C++缓冲区操作
- 如何直接操作终端输出缓冲区
- 是否需要为C/C++中的文件I/O操作创建自己的缓冲区
- 这样操作cv::Mat的内部缓冲区安全吗
- 使用核心配置文件交换缓冲区使用了无效操作
- DX11 OpenGL 互操作:无法复制缓冲区
- stringstream缓冲区操作
- WriteFile成功,即使缓冲区被挂起的操作覆盖
- 矩阵乘以矢量操作与原始缓冲区在本征