2010-11-24 3 views
1

My codeがコンパイルされましたが、例外がスローされます: "HealthCareProvider.exeで 'System、Access Violation Exception'型の未処理例外が発生しました。保護されたメモリを読み書きしようとしました... "助けて??HealthCareProvider.exeで 'System、Access Violation Exception'型の未処理の例外が発生しました

問題はprint()メソッドで発生します。どうしてか分かりません。 イテレータだけ束異なる番号を出力

何卒、

マイク

#ifndef _HEALTHCAREPROVIDER_H 
#define _HEALTHCAREPROVIDER_H 

#include <string> 
#include <iostream> 

using namespace std; 

class HealthCareProvider{ 

public: 

//constructor 
HealthCareProvider(const string &lname, const string &fname, const string &type, const int &yearsExperience, const string &coType): 
lastName(lname),firstName(fname),providerType(type),yearsExp(yearsExperience),companyType(coType) 
{ 
} 

//Last Name 
void setLastName(const string &lname){ 
    lastName = lname; 
} 

string getLastName()const{ 
    return lastName; 
} 

... etc. 

//coType 
void setCompanyType(const int &coType){ 
    companyType = coType; 
} 

string getCompanyType()const{ 
    return companyType; 
} 

void print() const { 
    cout<<"Name: "<< getLastName()<<", " <<getFirstName()<<"\nType : "<<getProviderType()<<"\nYears Experience: "<<getYearsExp()<<" \nCompany Type : "<<getCompanyType()<<endl; 
} 

virtual double billForTreatment() = 0; 


private: 

int yearsExperience, yearsExp; 
string type, coType, lname, fname; 
string lastName, firstName, providerType, companyType; 


}; 

#endif 




#include <vector> 
#include <list> 
#include <iostream> 
#include <iomanip> 
#include <typeinfo> 
#include <iterator> 
#include "HealthCareProvider.h" 
#include "Dentist.h" 

using namespace std; 

int main(){ 

    string value; 

    cout << fixed << setprecision (2); 

    //populate 
    vector < HealthCareProvider*> healthCareProviders (6); 

    healthCareProviders [0]=new Dentist("Thatcher","Donald","Dentist",10, "sole proprietorship"); 

    healthCareProviders [1]=new Dentist("Parker","Michelle","Dentist",5, "LLC"); 

    healthCareProviders [2]= new Dentist("Bradford","Michael","Dentist",12, "LLC"); 

    healthCareProviders [3] = new Dentist("Craig","Elizabeth","Dentist",4, "sole proprietorship"); 

    for (size_t i=0; i<healthCareProviders.size(); i++){ 
    healthCareProviders[i] ->print(); 
    cout<<endl; 
    } 

    for (size_t j =0; j< healthCareProviders.size(); j++){ 
    delete healthCareProviders [j]; 
    } 


    cout<<"Pause . . ."<<endl; 
    cin>>value; 

} 
+0

デバッグを実行して、コードが1行ずつ進んで、クラッシュするまで試しましたか?これはクラッシュする箇所を正確に示します。 – Goz

答えて

6

あなたはサイズ6のベクトルを作成しているが、あなただけの最初の4つのエントリに初期化されている(のtoString()が必要) 。残りの2つのポインタはNULLです。そのため、healthCareProviders[i] ->print();に電話するとアクセス違反が発生します。

healthCareProviders.push_back(new Dentist(...)); 
+0

初心者には全く分かりません。 +1 –

1

次のようにあなたのコードを再記述する場合:

簡単な解決策ではなく、事前にサイズを指定する必要に応じて要素を追加するvector::push_backを使用することです

//populate 
    vector < HealthCareProvider*> healthCareProviders; 

    healthCareProviders.push_back(new Dentist("Thatcher","Donald","Dentist",10, "sole proprietorship")); 

    healthCareProviders.push_back(new Dentist("Parker","Michelle","Dentist",5, "LLC")); 

    healthCareProviders.push_back(new Dentist("Bradford","Michael","Dentist",12, "LLC")); 

    healthCareProviders.push_back(new Dentist("Craig","Elizabeth","Dentist",4, "sole proprietorship")); 

それから、もう問題はありません。

ベクトルを6個の要素にしてから、そのうちの4個だけを上にしてベクトル化します。後でエントリーをベクトルにプッシュするスペースを予約したい場合は、「予約」機能を使用してください。これはメモリを割り当てますが、ベクトルの "サイズ"は変更しません。

+0

正しいのですが、理由を説明する必要はありませんが、これはもっと重要です。 –

関連する問題