我正在为学校的C++项目工作,其中程序将从文本文件中读取数字列表,将它们存储在一个动态数组中,然后将它们打印到另一个文本文件中。说实话,我有点迷失在这个指针,我得到的错误“一个类型的值”void“不能用于初始化我的主源文件中的”int“类型的实体。C++动态数组:无法使用“void”类型的值来初始化类型为“int”的实体
Main.cpp的(这是我得到的错误):
#include "dynamic.h"
int main
{
readDynamicData("input.txt","output.txt");
}
dynamic.cpp(骨架的程序):
#include "dynamic.h"
void readDynamicData(string input, string output)
{
DynamicArray da; //struct in the header file
da.count = 0;
da.size = 5; //initial array size of 5
int *temp = da.theArray;
da.theArray = new int[da.size];
ifstream in(input);
ofstream out(output);
in >> da.number; //prime read
while (!in.fail())
{
if (da.count < da.size)
{
da.theArray[da.count] = da.number;
da.count++;
in >> da.number; //reprime
}
else grow; //if there are more numbers than the array size, grow the array
}
out << "Size: " << da.size << endl;
out << "Count: " << da.count << endl;
out << "Data:" << endl;
for (int i = 0; i < da.size; i++)
out << da.theArray[i];
in.close();
out.close();
delete[] temp;
}
void grow(DynamicArray &da) //this portion was given to us
{
int *temp = da.theArray;
da.theArray = new int[da.size * 2];
for (int i = 0; i<da.size; i++)
da.theArray[i] = temp[i];
delete[] temp;
da.size = da.size * 2;
}
和dynamic.h中,头文件:
#include <iostream>
#include <string>
#include <fstream>
#ifndef _DynamicArray_
#define _DynamicArray_
using namespace std;
void readDynamicData(string input, string output);
struct DynamicArray
{
int *theArray;
int count;
int size;
int number;
};
void grow(DynamicArray &da);
#endif
请编辑您的问题以提供[mcve]。 –
我apogolize,但林不知道为什么这不符合最小和完整?我不确定为什么错误被触发,所以我发布了整个代码,因为它的代码是在三个文件中的任何一个。请让我知道如果这不是真的 – pbcrazy
在头文件中使用名称空间标准是非常糟糕的东西 – Raindrop7