【发布时间】:2017-12-28 17:49:04
【问题描述】:
这是我为一个类编写的程序的开始,我试图让它读取一个包含许多浮点数的文件。使用双打应该不是问题。我假设使用 array1.readDataFromFile(); 调用我的函数 会访问array1 结构中动态创建的数组吗?然而当
这是头文件
// Specification file for the NuberArrayClass
//a.k.a NumberArrayClass.h file
#ifndef NUMBERARRAYCLASS_H
#define NUMBERARRAYCLASS_H
using namespace std;
// class declaration
class NumberArrayClass
{
private:
int arraySize;
double * numberArray = nullptr;
public:
//constructor declaration
NumberArrayClass();
//member functions
void readDataFromFile();
void displayArray();
//destructor
~NumberArrayClass()
{
delete[] numberArray;
} // end of destructor
};
#endif // NUMBERARRAYCLASS_H
这是类函数文件
//NumberArrayClass.cpp file
#include "NumberArrayClass.h" //needed to access arry
#include <iostream>
#include <fstream> //needed for file read
using namespace std;
NumberArrayClass::NumberArrayClass()
{
arraySize = 250;
numberArray = new double[arraySize];
}
void NumberArrayClass::readDataFromFile()
{
//creating a read object and opening file.
ifstream inFile;
inFile.open("DoubleData.txt");
int countIt = 0; //this is a counter
if (!inFile.fail())
{
cout << "File open!" << endl;
//this should populate the
while (countIt < arraySize && inFile >> numberArray[countIt]);
{
countIt++; //incramenting counter.
}
}
else
{
cout << "File Read fail!" << endl;
}//end of if/else statement.
}//end of readDataFromFile
void NumberArrayClass::displayArray()
{
for (int i = 0; i < arraySize; i++)
{
cout << numberArray[i] << endl;
}
}//end of displayArray
这是我的主要内容
//main .cpp file
#include <iostream>
#include "NumberArrayClass.h"
using namespace std;
int main()
{
//creating our class object.
NumberArrayClass array1;
array1.readDataFromFile();
array1.displayArray();
system("pause");
return 0;
}
我不确定是语法错误还是什么.. 文件打开但 array1.displayArray();吐出垃圾。
【问题讨论】:
-
在那里检查:
However when the.
标签: arrays function class visual-c++