मैं अपने क्लास क्लास प्रकार के स्टॉक के लिए एक स्मृति याद करता हूं जो बेस क्लास उपकरण से विरासत में मिला है, जब मैं अपने सरणी के दूसरे तत्व तक पहुंचने का प्रयास करता हूं, तो यह त्रुटि फेंकता है। हालात ठीक हैं जब मैं अपनी नई सरणी आकार 1क्यों मैं अपने लूप में गतिशील आवंटित स्मृति तक नहीं पहुंच सकता?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Instrument{
public:
virtual void display(){}
virtual void output(){}
virtual void readFile(){}
virtual ~Instrument(){}
};
class Stock :
public Instrument{
public:
Stock(){
}
virtual void input(){
cout << "This is stock, please input its information: ";
cin >> name >> bidPrice >> askPrice >> lastPrice >> issueExchange;
}
virtual void display(){
cout <<"This is to display stock: "<< name << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< issueExchange << " "
<< endl;
}
virtual void output(){
ofstream myfile;
myfile.open("Stock.txt", ios::out | ios::app);
if (myfile.is_open()){
myfile << "This is a stock: "
<< name << " "
<< bidPrice << " "
<< askPrice << " "
<< lastPrice << " "
<< issueExchange << " "
<< endl;
}
else cout << "Unable to open file";
}
virtual void readFile(){
string line;
ifstream myfile("Stock.txt");
cout << "\nThis is file stored\n";
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
}
virtual ~Stock(){}
private:
char name[13];
double bidPrice;
double askPrice;
double lastPrice;
int issueExchange;
};
int main(){
const int N = 5;//it works fine if I use N=1;
Instrument *pBase = NULL;
pBase = new Stock[N];
for (int i = 0; i < N; i++){
pBase[i].input();// here throws an exception and ends the program
pBase[i].display();
pBase[i].output();
}
pBase[N - 1].readFile();
delete[] pBase;
system("pause");
return 0;
}
@GermaineJason यह जावा या सी # नहीं है। वस्तुएं हैं, और एक एनपीई मौजूद नहीं है। – deviantfan
दिलचस्प, मुझे त्रुटि दिखाई देती है, और मुझे आश्चर्य है कि मैंने इसे पहले कभी नहीं देखा है। यह लगभग टुकड़ा करने की तरह है, लेकिन यह अलग है। @GermaineJason सरणी में 5 वस्तुएं हैं। –
@MooingDuck, यह स्ट्रिंग से char [13] में बदल दिया गया है, क्या आप मेरा # शामिल –
Leewis