这是我以前从未见过的奇怪错误,不知道如何解决。崩溃发生在
na = m
这是相关的代码。有问题的行标有 *:
在主要:
#include <cstdlib>
#include <iostream>
#include "stu.h"
#include <string>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
stu stu;
int score[2];
std::string name;
std::cout <<"enter name:";
std::cin >> name;
//THIS IS AN EDIT IN AFTER SEEING THAT A IMPORTANT ERROR POINT WAS NOT SHOWN TO THE FIRST COUPLE REPLY
************************************************************
//THIS IS CAUSING THE PROBLEM WHEN I COMMENT IT OUT THE PROGRAM WORKS
std::cout << "enter score 1:";
std::cin >> score[0];
std::cout << "enter score 2:";
std::cin >> score[2];
std::cout << "enter score 3:";
std::cin >> score[3];
*************************************************************
stu.setname( name );
// ...
}
在stu.ccp
:
void stu::setname(std::string m)
{
std::cout <<"1";//<--to find where the code was crashing
na = m; // *** the crash
std::cout <<"1";
}
在stu.hpp
:
class stu
#include <string>
{
public:
stu();
void setname(std::string);
std::string getname();
void settest(int, int,int);
void display();
private:
std::string na;
int score[2];
};