/**********************************************************************************************/
编译连接通过,运行时显示内存错误
#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include <math.h>
#include "math.h"using namespace std;//test1类实现输入两个数x,y计算两者积,这部分没有错 class test1{
int x;
int y;
string ID;
public:
int z;
test1(string ID1="mmm",int x1=0,int y1=0,int z=0);
set(string ID1="mmm",int x1=0,int y1=0);
~test1();
void calculate(void);
string getID();
};test1::test1(string ID1,int x1,int y1, int z1){
ID=ID1;
x=x1;
y=y1;
z=z1;
}
test1::set(string ID1,int x1,int y1){
ID=ID1;
x=x1;
y=y1;
}test1::~test1()
{
}
void test1::calculate(void){
z=x*y;
}string test1::getID(){
return ID;
}int main()
{
vector<test1*> m_ptest2;
test1* test;
test=new test1;
test->set("sf",3,6);      
m_ptest2.push_back(test);
delete(test);          test=new test1;
test->set("ghhj",323,6);      
m_ptest2.push_back(test); vector<test1>::iterator *it= m_ptest2.begin(); // 问题出在这里
for(;it!=m_ptest2.end();it++)
{

(*it)->calculate();
printf("%s\n,%d\n\n",(*it)->getID(),(*it)->z); }
    return 0;
}

解决方案 »

  1.   

    vector <test1>::iterator it= m_ptest2.begin(); // 问题出在这里 
    for(;it!=m_ptest2.end();it++) 
    { (*it)->calculate(); 
    printf("%s\n,%d\n\n",(*it)->getID(),(*it)->z); } 
        return 0; 

      

  2.   


    /**********************************************************************************************/ 
    编译连接通过,运行时显示内存错误 
    #include <iostream> 
    #include <string> 
    #include <vector> 
    #include <conio.h> 
    #include <math.h> 
    #include "math.h" using namespace std; //test1类实现输入两个数x,y计算两者积,这部分没有错 class test1{ 
    int x; 
    int y; 
    string ID; 
    public: 
    int z; 
    test1(string ID1="mmm",int x1=0,int y1=0,int z=0); 
    set(string ID1="mmm",int x1=0,int y1=0); 
    ~test1(); 
    void calculate(void); 
    string getID(); 
    }; test1::test1(string ID1,int x1,int y1, int z1){ 
    ID=ID1; 
    x=x1; 
    y=y1; 
    z=z1; 

    test1::set(string ID1,int x1,int y1){ 
    ID=ID1; 
    x=x1; 
    y=y1; 
    } test1::~test1() 


    void test1::calculate(void){ 
    z=x*y; 
    } string test1::getID(){ 
    return ID; 

    int main() 

    vector <test1*> m_ptest2; 
    test1* test; 
    test=new test1; 
    test->set("sf",3,6);      
    m_ptest2.push_back(test); 
    delete(test);         test=new test1; 
    test->set("ghhj",323,6);      
    m_ptest2.push_back(test); 
    vector <test1>::iterator *it= m_ptest2.begin(); // 问题出在这里 
    for(;it!=m_ptest2.end();it++) 
    { (*it)->calculate(); 
    printf("%s\n,%d\n\n",(*it)->getID().c_str(),(*it)->z); } 
        return 0; 
      

  3.   

    另外 delete(test); 这句应该去掉。
      

  4.   

    再一次提个建议vector <test1>::iterator *it= m_ptest2.begin(); 别这么写
    vector <test1*>::iterator it= m_ptest2.begin(); 这么写才是最规范的,虽然都可以
      

  5.   


    哥们,可不可以加你qq啊,我的qq是253647220
    分数满分20分都给你了