碰到c++的运行问题

Kobe___Bryant 2016-05-12 04:28:58
我编写一个c++程序,程序编译通过了(编译器是),但运行的时候碰到了很大的问题,先是debug log显示已下错误:
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\uxtheme.dll'. Symbols loaded.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\combase.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\rpcrt4.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\dwmapi.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\kernel.appcore.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\bcryptprimitives.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\sechost.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\Users\Administrator\AppData\Roaming\TaobaoProtect\TaobaoProtectSE.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\shlwapi.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\psapi.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\advapi32.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\ole32.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\oleaut32.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\crypt32.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\msasn1.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\version.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\sfc.dll'. Cannot find or open the PDB file.
'ex12-2.exe' (Win32): Loaded 'C:\WINDOWS\System32\sfc_os.dll'. Cannot find or open the PDB file.
The thread 0x2c14 has exited with code 0 (0x0).
The thread 0x278c has exited with code 0 (0x0).
Unhandled exception at 0x0F61A893 (msvcr120d.dll) in ex12-2.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
然后程序运行为:


程序部分代码是:

[code=c]
#include "stdafx.h"
#include "string.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
String s1("and I am a c++ student.");
String s2 = "Please enter your name: ";
String s3;
cout << s2;
cin >> s3;
s2 = "My name is " + s3;
cout << s2 << ".\n";
s2 = s2 + s1;
s2.Stringupper();
cout << "The string\n" << s2 << "\ncontains " << s2.times('A') << " 'A' characters in it. \n";
s1 = "red";
String rgb[3] = { String(s1), String("green"), String("blue") };
cout << "Enter the name of a primer color for mixing light: ";
String ans;
bool success = false;
while (cin >> ans)
{
ans.Stringlow();
for (int i = 0; i < 3; i++)
{
if (ans == rgb[i])
cout << "That is right!\n";
success = true;
break;
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye\n";
system("pause");
return 0;
}
[/code]

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

class String
{
private:
char * str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String & s);
~String();
int length() const { return len; }
void Stringlow();
void Stringupper();
int times(char ch);
String & operator=(const String & s);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
friend String & operator+(String & str1, const String & str2);
friend String operator+(char * ch, const String & str);
friend bool operator<(const String & str1, const String & str2);
friend bool operator>(const String & str1, const String & str2);
friend bool operator==(const String & str1, const String & str2);
friend ostream & operator<<(ostream & os, const String & str);
friend istream & operator>>(istream & is, String & str);
static int HowMany();
};

#include "stdafx.h"
#include "string.h"

int String::num_strings = 0;

String::String(const char * ch)
{
len = strlen(ch);
str = new char[len + 1];
strcpy_s(str, len, ch);
num_strings++;
}

String::String()
{
len = 4;
str = new char[1];
str[0] = '\0';
num_strings++;

}

String::String(const String & s)
{
len = s.len;
str = new char[len];
strcpy_s(str, len, s.str);
num_strings++;
}

String::~String()
{
delete [] str;
num_strings--;
}

void String::Stringlow()
{
for (int i = 0; i < len; i++)
str[i] = tolower(str[i]);
}

void String::Stringupper()
{
for (int i = 0; i < len; i++)
str[i] = toupper(str[i]);
}

int String::times(char ch)
{
int times = 0;
for (int i = 0; i < len; i++)
{
while (str[i] == ch)
times++;
}
return times;
}

String & String::operator=(const String & s)
{
if (this == &s)
return *this;
delete[] str;
len = s.len;
str = new char[len + 1];
strcpy_s(str, len, s.str);
return *this;
}

String & String::operator=(const char * ch)
{
delete[] str;
len = strlen(ch);
str = new char[len + 1];
strcpy_s(str, len, ch);
return *this;
}

char & String::operator[](int i)
{
return str[i];
}

const char & String::operator[](int i) const
{
return str[i];
}

String & operator+(String & str1, const String & str2)
{
str1.len += str2.len;
strcat_s(str1.str, str2.len, str2.str);
return str1;
}

String operator+(char * ch, const String & str)
{
String temp;
temp.len = strlen(strcat(ch, str.str)) + 1;
temp.str = new char[temp.len];
strcpy_s(temp.str, temp.len, strcat(ch, str.str));
return temp;
delete[] temp.str;
}

bool operator<(const String & str1, const String & str2)
{
return (strcmp(str1.str, str2.str) < 0);
}

bool operator>(const String & str1, const String & str2)
{
return (strcmp(str1.str, str2.str) > 0);
}

bool operator==(const String & str1, const String & str2)
{
return (strcmp(str1.str, str2.str) == 0);
}

ostream & operator<<(ostream & os, const String & str)
{
os << str.str;
return os;
}

istream & operator>>(istream & is, String & str)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
str = temp;
while (is && is.get() != '\n')
continue;
return is;
}

int String::HowMany()
{
return num_strings;
}

...全文
218 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqbk1 2016-05-14
  • 打赏
  • 举报
回复
引用 3 楼 luciferisnotsatan 的回复:
学英文很重要,弹出的断言错误框都告诉你哪里出错了
+10086
Kobe___Bryant 2016-05-14
  • 打赏
  • 举报
回复
引用 11 楼 paschen 的回复:
[quote=引用 7 楼 baidu_24736703 的回复:] [quote=引用 1 楼 qq423399099 的回复:] 目测是你的strcpy_s strcpy_s(str, len, ch);改成strcpy_s(str, len+1, ch); 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
按照你们说的改了之后程序好了,我知道自己很不足在调试程序方面,但我不知道怎么调试程序,因为对这个编译器不是很熟悉,所以请问哪里调试教程吗 [/quote] http://jingyan.baidu.com/article/ae97a646ccdbc0bbfc461d6d.html[/quote] 谢谢了
paschen 版主 2016-05-14
  • 打赏
  • 举报
回复
引用 7 楼 baidu_24736703 的回复:
[quote=引用 1 楼 qq423399099 的回复:] 目测是你的strcpy_s strcpy_s(str, len, ch);改成strcpy_s(str, len+1, ch); 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
按照你们说的改了之后程序好了,我知道自己很不足在调试程序方面,但我不知道怎么调试程序,因为对这个编译器不是很熟悉,所以请问哪里调试教程吗 [/quote] http://jingyan.baidu.com/article/ae97a646ccdbc0bbfc461d6d.html
赵4老师 2016-05-13
  • 打赏
  • 举报
回复
教VS调试的视频不难搜到吧。
huatian5 2016-05-13
  • 打赏
  • 举报
回复
根据错误提示很好找的,写的很清楚啊
Kobe___Bryant 2016-05-12
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
目测是你的strcpy_s strcpy_s(str, len, ch);改成strcpy_s(str, len+1, ch); 崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。 代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
按照你们说的改了之后程序好了,我知道自己很不足在调试程序方面,但我不知道怎么调试程序,因为对这个编译器不是很熟悉,所以请问哪里调试教程吗
hen_hao_ji 2016-05-12
  • 打赏
  • 举报
回复
hello world! +
Kobe___Bryant 2016-05-12
  • 打赏
  • 举报
回复
按照你们说的改了之后程序好了,我知道自己很不足在调试程序方面,但我不知道怎么调试程序,因为对这个编译器不是很熟悉,所以请问哪里调试教程吗
赵4老师 2016-05-12
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
luciferisnotsatan 2016-05-12
  • 打赏
  • 举报
回复
学英文很重要,弹出的断言错误框都告诉你哪里出错了
幻夢之葉 2016-05-12
  • 打赏
  • 举报
回复
点击Break进入断点,查看调用栈,看是你代码中的哪个函数调用引发的问题 根据提示你是strcpy_s发生错误,就是你目的字符串分配的内存太小
小灸舞 2016-05-12
  • 打赏
  • 举报
回复
目测是你的strcpy_s
strcpy_s(str, len, ch);改成strcpy_s(str, len+1, ch);

崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。

64,680

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧