2 qq 32756899 qq_32756899 于 2016.05.05 18:15 提问

C++编译通过但是cmd窗口不显示程序直接退出了

应该跟添加getchar() system(“pause”)什么的没有关系

 //MyRectangle.h
#pragma once
#include"Screen.h"
class MyRectangle
{
public:
 MyRectangle();
 MyRectangle(int x1, int y1, int x2, int y2, Screen* screen);
 void setCoordinations(int x1, int y1, int x2, int y2);
 void setScreen(Screen& screen);
 void Draw();
private:
 int x1_;
 int x2_;
 int y1_;
 int y2_;
 Screen* screen_;
};

//Screen.h
#pragma once
class Screen
{
public:
 void exitWhenInvalidScreen(int x, int y);
 int getWidth();
 int getHeight();
 int setWidth(int width);    //return width
 int setHeight(int height);  //return height
 Screen();
 Screen(int, int);
 //~Screen();
private:
 int width;
 int height;
};

//main.cpp
#include<iostream>
#include"Screen.h"
#include"MyRectangle.h"
using namespace std;
int main()
{
 int width,height;
 cin>>width>>height;
 Screen screen(width,height);
 int leftX,leftY,rightX,rightY;
 cin>>leftX>>leftY>>rightX>>rightY;
 MyRectangle myRectangle1(leftX, leftY, rightX, rightY, &screen);
 MyRectangle *myRectangles=new MyRectangle[2];
 myRectangles[1].setCoordinations(10, 300, 700, 500);
 myRectangles[1].setScreen(screen);
 myRectangle1.Draw();
 for (int i = 0; i<2; i++) {
  myRectangles[i].setScreen(screen);
  (myRectangles + i)->Draw();
 }

 delete[]myRectangles;

#ifdef DEBUG
 stdcin.get();
#endif
 system("pause");
 return 0;
} 
//MyRectangle.h

#pragma once
#include"Screen.h"
class MyRectangle
{
public:
 MyRectangle();
 MyRectangle(int x1, int y1, int x2, int y2, Screen* screen);
 void setCoordinations(int x1, int y1, int x2, int y2);
 void setScreen(Screen& screen);
 void Draw();
private:
 int x1_;
 int x2_;
 int y1_;
 int y2_;
 Screen* screen_;
};




//Screen.h

#pragma once
class Screen
{
public:
 void exitWhenInvalidScreen(int x, int y);
 int getWidth();
 int getHeight();
 int setWidth(int width);    //return width
 int setHeight(int height);  //return height
 Screen();
 Screen(int, int);
 //~Screen();
private:
 int width;
 int height;
};




//main.cpp

#include<iostream>
#include"Screen.h"
#include"MyRectangle.h"
using namespace std;
int main()
{
 int width,height;
 cin>>width>>height;
 Screen screen(width,height);
 int leftX,leftY,rightX,rightY;
 cin>>leftX>>leftY>>rightX>>rightY;
 MyRectangle myRectangle1(leftX, leftY, rightX, rightY, &screen);
 MyRectangle *myRectangles=new MyRectangle[2];
 myRectangles[1].setCoordinations(10, 300, 700, 500);
 myRectangles[1].setScreen(screen);
 myRectangle1.Draw();
 for (int i = 0; i<2; i++) {
  myRectangles[i].setScreen(screen);
  (myRectangles + i)->Draw();
 }




 delete[]myRectangles;




#ifdef DEBUG
 stdcin.get();
#endif
 system("pause");
 return 0;
}



//MyRetangle.cpp

#include"MyRectangle.h"
#include"Screen.h"
#include<iostream>
#include<graphics.h>
MyRectangle::MyRectangle()
{
 x1_ = 0; y1_ = 0; x2_ = 0; y2_ = 0;
 std::cout << "myrectangle" << std::endl;
}
MyRectangle::MyRectangle(int x1, int y1, int x2, int y2, Screen* screen)
{
 x1_ = x1;
 x2_ = x2;
 y1_ = y1;
 y2_ = y2;
 screen_ = screen;
 std::cout << "myrectangle" << std::endl;
}
void MyRectangle::setCoordinations(int x1, int y1, int x2, int y2)
{
 x1_ = x1;
 x2_ = x2;
 y1_ = y1;
 y2_ = y2;
}
void MyRectangle::setScreen(Screen& screen)
{
 screen_ = &screen;
}
void MyRectangle::Draw()
{
 if ((x1_ <= 0) || (x2_ <= 0) || (y1_ <= 0) || (y2_ <= 0) || (x2_ - x1_)>(*screen_).getWidth() || (y2_ - y1_)>(*screen_).getHeight())
 {
  std::cout << "invalid myrectangle" << std::endl;
 }
 else
 {
  rectangle(x1_, y1_, x2_, y2_);
  std::cout << x1_ << ' ' << y1_ << ' ' << x2_ - x1_ << ' ' << y2_ - y1_ << std::endl;
 }
}



//Screen.cpp

#include"Screen.h"
#include<iostream>
#include<cstdlib>
#include<graphics.h>
using namespace std;
Screen::Screen()
{
 width = 640;
 height = 480;
 initgraph(width, height);
 std::cout << "screen" << std::endl;
}
Screen::Screen(int x, int y)
{
 width = x;
 height = y;
 exitWhenInvalidScreen(x, y);
 initgraph(width, height);
 std::cout << "screen" << std::endl;
}
/*Screen::~Screen()
{
getchar();
cout << "折构函数已启用";
closegraph();
}*/




int Screen::getWidth()
{
 return width;
}
int Screen::getHeight()
{
 return height;
}
int Screen::setWidth(int w)
{
 width = w;
 exitWhenInvalidScreen(width, 500);
 return width;
}
int Screen::setHeight(int h) //return height
{
 height = h;
 exitWhenInvalidScreen(500, height);
 return height;
}
void Screen::exitWhenInvalidScreen(int x, int y)
{
 if (!((x > 0 && x <= 1000) && (y > 0 && y <= 1000)))
 {
  std::cout << "invalid screen size";
  exit(0);
 }
}

其中跟graph有关的函数EGE图形库中的,应该没什么问题。用的是VS2013
新手一枚 望不吝指教-。-
“图图.exe”(Win32): 已加载“F:\C++\图图\Debug\图图.exe”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\ntdll.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel32.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\KernelBase.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\imm32.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\user32.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\gdi32.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcr120d.dll”。无法查找或打开 PDB 文件。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcp120d.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\ole32.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\msimg32.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\WinSxS\x86_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.10586.20_none_22adb5eaa762c7fa\GdiPlus.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\combase.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcrt.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\rpcrt4.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\sspicli.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\cryptbase.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\bcryptprimitives.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\sechost.dll”。已加载符号。
“图图.exe”(Win32): 已加载“C:\Windows\SysWOW64\oleaut32.dll”。已加载符号。
线程 0x2360 已退出,返回值为 0 (0x0)。
线程 0x12cc 已退出,返回值为 0 (0x0)。
线程 0x1898 已退出,返回值为 0 (0x0)。
程序“[7540] 图图.exe”已退出,返回值为 0 (0x0)。

2个回答

u010541307
u010541307   2016.05.05 22:34

可以在命令行中进入图图.exe所在目录,运行图图+参数试试。(个人觉得应该是缺少输入参数,但是你的主函数中没有写参数输入)

qq_26904543
qq_26904543   2016.05.06 08:19

很简单的问题 你按的是F5吧,F5是调试,如果调试不存在错误就会退出

想cmd窗口不消失,按ctrl+f5就好,

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!