控制台末尾的调试错误

问题描述:

我是一名学生,正在学习C++。控制台末尾的调试错误

这是我的cpp的代码

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    CFood output; 
    output.whatFunc(); 
    cout<<"my outputs"<<endl<<output<<endl; 
    return 0; 
} 

ostream& operator <<(ostream& outputStream, const CFood& output) 
{ 
    for(int i=0; i<2; i++) 
    { 
    outputStream <<"1 : "<<output.m_strName[i]<<" 2 : "<<output.m_servingSize[i]<<"g "<<"3 : "<< 
     output.m_calorie[i]<<"cal "<<"4 : "<<output.m_transFat[i]<<"g"<<endl; 

    } 

    return outputStream; 
} 

当我调试它,它的工作。但是在控制台的结尾,它给了我错误消息;;;

它说:“发生在work.exe [5796]未处理的win32异常”

申请

我的头是

class CFood 
{ 
public: 
    CFood(void); 
    ~CFood(void); 

private: 
    string m_strName[7]; 
    double m_servingSize[7]; 
    double m_calorie[7]; 
    double m_transFat[7]; 

public: 
    void whatFunc(void); 
friend ostream& operator <<(ostream& outputStream,const CFood& output); 
} 

我觉得有什么不对,我code..And我认为这是CFood输出;(只是想..)

你知道它为什么有调试错误吗?

++对不起,我忘了whatFunc(无效)

这是代码

void CFood::whatFunc(void) // 
{ 
    m_strName[0]="chicken"; 
    m_strName[1]="rice"; 
    m_strName[2]="meat"; 
    m_strName[3]="strawberry"; 
    m_strName[4]="apple"; 
    m_strName[5]="water"; 
    m_strName[6]="juice"; 
    m_servingSize[0]=10; 
    m_servingSize[1]=20; 
    m_servingSize[2]=30; 
    m_servingSize[3]=40; 
    m_servingSize[4]=50; 
    m_servingSize[5]=60; 
    m_servingSize[6]=70; 
    m_calorie[0]=10.45; 
    m_calorie[1]=20.57; 
    m_calorie[2]=30.78; 
    m_calorie[3]=40.23; 
    m_calorie[4]=50.85; 
    m_calorie[5]=60.73; 
    m_calorie[6]=70.27; 
    m_transFat[0]=0.01; 
    m_transFat[1]=0.02; 
    m_transFat[2]=0.03; 
    m_transFat[3]=0.04; 
    m_transFat[4]=0.05; 
    m_transFat[5]=0.06; 
    m_transFat[6]=0.07; 



} 
+1

请发布[MCVE](http://*.com/help/mcve)。 – 2014-11-22 04:05:02

+0

当您使用调试器完成代​​码时,哪条线会给您带来问题? – 2014-11-22 04:36:51

+0

您的头文件缺少'class'关键字,打开和关闭大括号。 – 2014-11-22 04:37:39

那么其很难说究竟是什么出了问题不完整的源代码。根据我的愚见,整个资料来源都非常值得期待。将明显链接的数据放置在一组具有静态大小的未链接数组中并不是一个很好的模式。请尝试类似的方式:

#include <iostream> 
#include <vector> 
#include <string> 
#include <ostream> 

struct CFoodItem{ 
    std::string m_strName; 
    double m_servingSize; 
    double m_calorie; 
    double m_transFat; 
}; 

class CFood 
{ 
public: 
    void AddFoodItem(const CFoodItem& cItem); 
    friend std::ostream& operator <<(std::ostream& outputStream, const CFood& output); 
private: 
    std::vector<CFoodItem> m_vItems; 
}; 


std::ostream& operator <<(std::ostream& outputStream, const CFood& output) 
{ 
    for (auto i = output.m_vItems.begin(); i != output.m_vItems.end(); ++i) 
    { 
     outputStream << "1 : " << i->m_strName << " 2 : " << i->m_servingSize << "g " << "3 : " << 
      i->m_calorie << "cal " << "4 : " << i->m_transFat << "g" << std::endl; 

    } 

    return (outputStream); 
} 

void CFood::AddFoodItem(const CFoodItem& cItem) 
{ 
    m_vItems.push_back(cItem); 
} 

int __cdecl main(void) 
{ 
    CFood output; 
    CFoodItem itm; 
    itm.m_strName = "some food"; 
    itm.m_servingSize = 100500; 
    itm.m_calorie = 42; 
    itm.m_transFat = 42; 
    output.AddFoodItem(itm); 
    std::cout << "my outputs" << std::endl << output << std::endl; 
    return 0; 
}