HealthCare Provider.exe中出现未处理的“系统,访问冲突异常”类型的异常

时间:2010-11-24 17:52:33

标签: c++

我的代码编译但引发了异常:“HealthCareProvider.exe中出现了'System,Access Violation Exception'类型的未处理异常附加信息:尝试读取或写入受保护的内存...”救命??

问题在于print()方法。我不知道为什么。 迭代器只打印出一堆不同的数字(需要toString())

谦虚地,

麦克

#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;

}

2 个答案:

答案 0 :(得分:6)

您正在创建一个大小为6的向量,但您只初始化了前4个条目。其余两个指针为NULL,这就是您致电healthCareProviders[i] ->print();时获得访问冲突的原因。

一个简单的解决方案是使用vector::push_back根据需要添加元素,而不是提前指定大小:

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

答案 1 :(得分:1)

如果您按如下方式重新编写代码:

//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个元素。如果您希望保留空间以便稍后将条目推入向量,请使用“保留”功能。这会分配内存,但不会更改向量的“大小”。