简体   繁体   English

在std :: cout字符串上崩溃

[英]Crash on std::cout string

I'm getting a crash on runtime when trying to execute a simple program that cout's a string object, i'm using Borland C++ Compiler version 5.5 with the following code: 当我尝试执行一个包含字符串对象的简单程序时,在运行时崩溃,我在使用Borland C ++编译器5.5版并使用以下代码:

#include <iostream>
#include <string> // Usaremos as funcoes mais modernas de String em C++
#include <conio.h>

using namespace std;

// <Prototipos >

int MenuPrincipal(void);

void DesenharCentralizado(string String, int CoordY);

// </Prototipos>

int main() {
    while(MenuPrincipal() != 0); // Para sair, ele deve retornar 0
    return 0;
}

int MenuPrincipal(void) {
    string Titulo = "Agenda";

    clrscr();

    DesenharCentralizado(Titulo, 4);
    getch();
    return 0;
}

void DesenharCentralizado(string Frase, int CoordY) {
    int PosX=wherex(), PosY=wherey();

    gotoxy((80-Frase.length())/2, CoordY);
    cout << Frase; // XXX CRASH
    gotoxy(PosX, PosY);
}

PS: Please don't complain about using old conio.h and these things, these are for my C++ class and my teacher is teaching us to use it first… PS:请不要抱怨使用旧的conio.h和这些东西,这些是给我的C ++类的,我的老师正在教我们首先使用它。

If you comment out everything in the file and replace it with 如果您注释掉文件中的所有内容并将其替换为

#include <iostream>
int main() { std::cout << "Hello, world!"; }

does that work? 那样有用吗? If yes, then try 如果是,请尝试

#include <iostream>
#include <string>
int main() { std::cout << std::string( "Hello, world!" ); }

With the removal of <conio.h> and calls to its functions, your program is simple enough to indicate a broken toolchain, and adding features one at a time might help track down what's broken. 删除<conio.h>并调用其函数后,您的程序就足够简单,可以指示出一个已损坏的工具链,并且一次添加一个功能可能有助于跟踪发生了什么。 It could be an incorrect runtime library version, or some kind of corruption in the installation of Borland or the project files. 可能是运行时库版本不正确,或者Borland或项目文件的安装中存在某种损坏。

By re-creating the project one step at a time, either you will track down the cause or you will end up with a working project, at which point you can forget about the problem. 通过一次一次重新创建项目,您可以找到原因,或者最终得到一个可以正常工作的项目,这时您可以忽略问题。

Borland 5.5. Borland 5.5。 only had partial support for std::string . 部分支持 std::string

It may well be that your code is to blame in this particular case, but even so, you will not get anywhere (and you will get nowhere very fast) trying to use std::string with that compiler. 在这种特殊情况下,很可能是您的代码应归咎于您,但是即使如此,尝试在该编译器中使用std::string ,您也无济于事(而且您将无法很快获得。)

If your teacher requires you to use Borland 5.5, then your teacher is trying to teach himself or herself as he/she is teaching you. 如果您的老师要求您使用Borland 5.5,则您的老师正在尝试自己教书的过程。 That approach is sometimes OK, but send your teacher here. 有时这种方法还可以,但是请把您的老师送到这里。 Now, to wingleader's teacher: 现在, 对翼长的老师:

Borland 5.5 is a broken tool. Borland 5.5是一个损坏的工具。 It is not just pre-standard: it is broken. 它不只是预标准的:它已损坏。 Students will not learn anything positive from using it, just like trying to learn to play the piano on a piano that is grossly out of tune. 学生不会从使用中学到任何积极的东西,就像试图在严重失调的钢琴上弹奏钢琴一样。

If you must support fifteen year old computers (or older), then use maybe g++ 2.95 or Visual C++ 6.0. 如果必须支持15年以上的计算机(或更旧的计算机),则可以使用g ++ 2.95或Visual C ++ 6.0。 Otherwise, use free modern tools such as (as of 2012) MinGW g++ 4.6 or newer, or Visual C++ 10.0 or newer. 否则,请使用免费的现代工具,例如(自2012年起)MinGW g ++ 4.6或更高版本,或Visual C ++ 10.0或更高版本。 The new compilers (although not the Visual Studio Express IDE) run well even on a computer with just 265 MB RAM, which as of 2012 includes some ten years old PCs. 新的编译器(尽管不是Visual Studio Express IDE)即使在只有265 MB RAM的计算机上也能很好地运行,截至2012年,RAM包含大约十年历史的PC。 Code::Blocks is a good IDE for old Windows computers. Code :: Blocks是旧Windows计算机的不错的IDE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM