我编写一个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;
}