我正在爲學校的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