首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这么简单的一段代码,到底错在哪里了?该怎么解决

2012-02-29 
这么简单的一段代码,到底错在哪里了?CStringy[10]for(i0i 10i++){if(strcmp(x[i],x[i+1])0){y[j]x

这么简单的一段代码,到底错在哪里了?
CString   y[10];
        for(i=0;i <10;i++)
{    
if(strcmp(x[i],x[i+1])==0)
{y[j]=x[i];
                        j++;
}
               
}
为什么我一点运行的按钮就报错:XXX.exe遇到问题需要关闭..
我都快奔溃了

[解决办法]
j是什么东西?
会不会是内存越界?
[解决办法]
既然用CString类,就没有必要使用strcmp来进行字符串比较或者这么比较根本就不对,我没有测试过!
直接使用比较运算符就可以!不要混用c字符串函数处理面向对象的字符串对象,这是一个非常不好的习惯。
[解决办法]
OK,让我来猜测一下你的程序吧,
首先我不清楚你的x是什么型别的对象,且让我把他们看成是char* 类型的吧,似乎这样能让你的程序走得通。
你的for 循环是从0到9的,这表示你的x字符串数组最少要拥有11个元素(也就是最少要有11个字符串。另外,你的j并未在上面的代码中给出初始化,又或者是型别定义,所以实在不知道说什么好,总之,我猜想你是需要下面的这段代码。

char* x[11] = { "string 1 ",
"string 2 ",
"string 3 ",
........ }; // 省略4到11,又或者这些你之前的代码能完成
// 记得,你必须得保证这里至少有11个完整的字符串,否则会因为字符串未定义或者是数组
// 上界不够10而访问到非法内存。
CString y[10]; // 这里会调用缺省构造函数产生10个CString对象
for ( int i=0; i <10; ++i ) // 最好采用++的前缀操作,这样可以节省一个临时对象的成本
{
if ( strcmp(x[i], x[i+1]) ) // 虽然不明白你的逻辑,还是按照你原来的写吧...
{
y[i] = x[i]; // 猜想你是想要轮流为y的每个元素赋值,若对应下标i的x和i+1的x是同样的字符串
// 这里赋值没问题,因为CString 拥有以char* 指针为参数的构造函数
}
} // 完事,拍拍屁股走人

// 重点:你得保证你的x内容没有问题,也就是都是完整的字符串,并且至少有11个



[解决办法]
我猜楼主可能是使用冒泡排序将字符串数组进行排序吧!

楼主应该把i,j打印出来就知道那里出了问题

下面我再MSDN中摘出来的基本CString的操作:
Visual C++ Concepts: Adding Functionality

Basic CString OperationsSee Also
Strings
This article explains basic CString operations, including:

Creating CString objects from standard C literal strings
Accessing individual characters in a CString
Concatenating two CString objects
Comparing CString objects
The CString class provides member functions and overloaded operators that duplicate and, in some cases, surpass the string services of the C run-time libraries (for example, strcat).

Creating CString Objects from Standard C Literal Strings
You can assign C-style literal strings to a CString just as you can assign one CString object to another:

Assign the value of a C literal string to a CString object:
CString myString = "This is a test ";
Assign the value of one CString to another CString object:
CString oldString = "This is a test ";
CString newString = oldString;
The contents of a CString object are copied when one CString object is assigned to another. Thus, the two strings do not share a reference to the actual characters that make up the string. For more information on using CString objects as values, see the article CString Semantics.

Note To write your application so that it can be compiled for Unicode or for ANSI, code literal strings using the _T macro. For more information, see the article Unicode and Multibyte Character Set (MBCS) Support.
Accessing Individual Characters in a CString
You can access individual characters within a CString object with the GetAt and SetAt member functions. You can also use the array element, or subscript, operator ( [ ] ) instead of GetAt to get individual characters (this is similar to accessing array elements by index, as in standard C-style strings). Index values for CString characters are zero based.



Concatenating Two CString Objects
To concatenate two CString objects, use the concatenation operators (+ or +=) as follows:

CString s1 = "This "; // Cascading concatenation
s1 += "is a ";
CString s2 = "test ";
CString message = s1 + "big " + s2;
// Message contains "This is a big test ".
At least one argument to the concatenation operators (+ or +=) must be a CString object, but you can use a constant character string (such as "big ") or a char (such as 'x ') for the other argument.

Comparing CString Objects
The Compare member function and the == operator for CString are equivalent. Compare, operator==, and CompareNoCase are MBCS and Unicode aware; CompareNoCase is also case insensitive. The Collate member function of CString is locale sensitive and is often slower than Compare. Collate should be used only where it is necessary to abide by the sorting rules as specified by the current locale.

The following table shows the available CString comparison functions and their equivalent Unicode/MBCS-portable functions in the C run-time library:

CString function MBCS function Unicode function
Compare _mbscmp wcscmp
CompareNoCase _mbsicmp _wcsicmp
Collate _mbscoll wcscoll

The CString class overrides the relational operators ( <, <=, > =, > , ==, and !=).You can compare two CStrings using these operators, as shown here:

CString s1( "Tom " );
CString s2( "Jerry " );
if( s1 < s2 )
...
See Also
Strings


--------------------------------------------

Send feedback on this topic to Microsoft

&copy; Microsoft Corporation. All rights reserved.

热点排行