【问题标题】:C++ Build success, but run failedC++ 构建成功,但运行失败
【发布时间】:2017-02-25 21:44:21
【问题描述】:

我是新的 C++,所以请原谅我问这个问题。我创建了一个项目并第一次运行它,它是成功的。但是当我开始另一个项目并向它添加 4 个类时(您可以从选项卡中看到)并且 ma​​in.cpp 无法运行。我很困惑,因为两个项目中的代码完全相同。

运行成功: Success

构建成功但运行失败: Run Failed

有什么办法可以解决这个问题?

我必须发布我所有课程的代码吗? (有8个文件)

student.h:

  #ifndef CLSSTUDENT_H
    #define CLSSTUDENT_H
    #include <string>
    #include <iostream>

    using namespace std;

    class clsStudent {

    protected:
        string name;
        string student_no;
        string program;

    public:
        clsStudent(string n, string sn,string prog );
        virtual void displayStudentDetails();



};

student.cpp

#include "TutorialClass.h"

void TutorialClass::addStudent(clsStudent std)
{

    _students.push_back(std);


}

int TutorialClass::getStudentCount()
{

    return _students.size();


}

void TutorialClass::display()
{


}
#endif /* CLSSTUDENT_H */

我打开了一个新项目,只添加了这个类。它也无法运行。代码有什么问题?

【问题讨论】:

标签: c++


【解决方案1】:

您的程序似乎只有在与其他文件一起编译时才会运行失败。我敢打赌,在这些文件中,您有错误代码在运行之前 main() 开始运行。

这可能发生在以下情况:

int f() {
    throw; // bam! Uncaught exception;
}

int x = f(); // this runs before main()

或者这个:

class C {
    C() {
        cout << "This runs before main() too!" << endl;
    }
};

C my_c; // calls constructor

在这两种情况下:代码在main() 之前执行。您想要这样做是因为您希望在运行 main() 之前初始化全局变量。如果此初始化代码设法通过段错误或exit() 调用使程序崩溃,或者抛出一些未被捕获的异常?你有一个崩溃的程序,甚至还没有机会运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 2011-12-28
    • 2018-03-31
    • 1970-01-01
    相关资源
    最近更新 更多