请帮助:[警告]不推荐将字符串常量转换为 'char*' [-Wwrite-strings]
Posted
技术标签:
【中文标题】请帮助:[警告]不推荐将字符串常量转换为 \'char*\' [-Wwrite-strings]【英文标题】:Please help: [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings]请帮助:[警告]不推荐将字符串常量转换为 'char*' [-Wwrite-strings] 【发布时间】:2015-10-16 23:01:26 【问题描述】:这是我第一次来这里,我有点紧张。我在课堂上有一个作业,询问用户他们想要使用哪种排序算法进行排序,我的教授已经给了我们程序的骨架,我们所要做的就是编写 3 种排序算法的代码。它说在第 41 行我试图传递一个 char 数据类型的字符串(我的教授写了那个),我很难解决它,因为我查看了与有相同错误的人的类似论坛帖子因为我和解决方案不起作用。您能否看一下程序,看看有什么问题以及如何解决?我会非常感激。
#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_SIZE = 1000000;
// Set this to true if you wish the arrays to be printed.
const bool OUTPUT_DATA = false;
void ReadInput(string& sortAlg, int& size);
void GenerateSortedData(int data[], int size);
void GenerateReverselySortedData(int data[], int size);
void GenerateRandomData(int data[], int size);
void GenerateNearlySortedData(int data[], int size);
void Sort(int data[], int size, string sortAlg, char* dataType);
void InsertionSort(int data[], int size);
void MergeSort(int data[], int lo, int hi);
void combine(int data[], int size, int lo, int hi, int mid);
void QuickSort(int data[], int lo, int hi);
int partition(int data[], int lo, int hi);
void Swap(int &x, int &y);
bool IsSorted(int data[], int size);
void printData(int data[], int size, string title);
int main(void)
int size;
string sortAlg;
ReadInput(sortAlg, size);
int * data = new int[size];
GenerateSortedData(data, size);
Sort(data, size, sortAlg, "Sorted Data");
GenerateReverselySortedData(data, size);
Sort(data, size, sortAlg, "Reversely Sorted Data");
GenerateRandomData(data, size);
Sort(data, size, sortAlg, "Random Data");
GenerateNearlySortedData(data, size);
Sort(data, size, sortAlg, "Nearly Sorted Data");
cout << "\nProgram Completed Successfully." << endl;
return 0;
/********************************************************************/
void ReadInput(string& sortAlg, int& size)
cout << " I:\tInsertion Sort" << endl;
cout << " M:\tMergeSort" << endl;
cout << " Q:\tQuickSort" << endl;
cout << "Enter sorting algorithm: ";
cin >> sortAlg;
string sortAlgName;
if(sortAlg == "I")
sortAlgName = "Insertion Sort";
else if(sortAlg == "M")
sortAlgName = "MergeSort";
else if(sortAlg == "Q")
sortAlgName = "QuickSort";
else
cout << "\nUnrecognized sorting algorithm Code: " << sortAlg << endl;
exit(1);
cout << "Enter input size: ";
cin >> size;
if(size < 1 || size > MAX_SIZE)
cout << "\nInvalid input size " << size
<< ". Size should be between 1 and " << MAX_SIZE << endl;
exit(1);
cout << "\nSorting Algorithm: " << sortAlgName;
cout << "\nInput Size = " << size << endl;
cout << endl;
/******************************************************************************/
void GenerateSortedData(int data[], int size)
int i;
for(i=0; i<size; i++)
data[i] = i * 3 + 5;
/*****************************************************************************/
void GenerateReverselySortedData(int data[], int size)
int i;
for(i = 0; i < size; i++)
data[i] = (size-i) * 2 + 3;
/*****************************************************************************/
void GenerateRandomData(int data[], int size)
int i;
for(i = 0; i < size; i++)
data[i] = rand();
/*****************************************************************************/
void GenerateNearlySortedData(int data[], int size)
int i;
GenerateSortedData(data, size);
for(i=0; i<size; i++)
if(i % 10 == 0)
if(i+1 < size)
data[i] = data[i+1] + 9;
/*****************************************************************************/
void Sort(int data[], int size, string sortAlg, char* dataType)
cout << endl << dataType << ":";
if (OUTPUT_DATA)
printData(data, size, "Data before sorting:");
// Sorting is about to begin ... start the timer!
clock_t start = clock();
if(sortAlg == "I")
InsertionSort(data, size);
else if(sortAlg == "M")
MergeSort(data, 0, size-1);
else if(sortAlg == "Q")
QuickSort(data, 0, size-1);
else
cout << "Invalid sorting algorithm!" << endl;
exit(1);
// Sorting has finished ... stop the timer!
clock_t end = clock();
double elapsed = (((double) (end - start)) / CLOCKS_PER_SEC) * 1000;
if (OUTPUT_DATA)
printData(data, size, "Data after sorting:");
if (IsSorted(data, size))
cout << "\nCorrectly sorted " << size << " elements in " << elapsed << "ms";
else
cout << "ERROR!: INCORRECT SORTING!" << endl;
cout << "\n-------------------------------------------------------------\n";
/*****************************************************************************/
bool IsSorted(int data[], int size)
int i;
for(i=0; i<(size-1); i++)
if(data[i] > data[i+1])
return false;
return true;
/*****************************************************************************/
void InsertionSort(int data[], int size)
//Write your code here
int i, j, temp;
for(i = 1; i < size; i++) //first element in the array
temp = data[i]; //Data[i] values are stored in temp.
while(j > 0 && data[j-1] > data[j]) //While j > 0 and the value of j-1 position is greater
//than the value of position j, begin swap.
temp = data[j]; //Value of data[j] is moved to temp.
data[j] = data[j-1]; //The values of data[j-1] is moved to data[j].
data[j-1] = temp; //Then the temp value is moved to the data[j-1] array.
j--; //Decrement j value.
/*****************************************************************************/
void MergeSort(int data[], int size)
//Write your code here
int hi, lo, mid;
if(lo <= hi)
mid = (hi + lo)/2; //Pivot.
MergeSort(data, lo, mid); //recurssively call lowerhalf of the array.
MergeSort(data, mid+1, hi); //recurssively call upperhalf of the array.
combine(data, size, lo, hi, mid); //combine the array.
return;
void combine(int data[], int size, int lo, int hi, int mid)
int temp[size];
int i = lo;
int j = mid+1;
int k = lo;
for(int i = lo; i <= hi; i++)
temp[i] = data[i]; //store the values in data array into the temp array
while(i <= mid && j <= hi)
if(temp[i] = temp[j]) //if i value in the temp array is equal to the j value in the temp array
data[k] = temp[i]; //move the temp[i] values into the main data[k] array
i++; //increment i by 1
else
data[k] = temp[j]; //otherwise, move the temp[j] values into the main array
j++; //increment j by 1
k++;
while(i <= mid)
data[k] = temp[i]; //while i value is <= to mid value, the temp[i] value is moved to data[k]
k++; //increment k
i++; //increment i
/*****************************************************************************/
void QuickSort(int data[], int size, int lo, int hi)
//Write your code here
int q;
if(lo>=hi)
q=partition(data, lo, hi);
QuickSort(data, lo, (q-1));
QuickSort(data, (q+1), hi);
int partition(int data[], int lo, int hi)
int temp; //temp for swaping
int i = lo;
int j = hi;
int pivot = data[(lo+hi)/2]; //pivot takes the end element
while(i<=j)
while(data[i] < pivot)
i++; //left hand side partition
while(data[j] > pivot)
j--; //right hand side partition
if(i <= j) //swaping occurs
temp = data[i]; //take data[i] and put it into temp
data[i] = data[j]; //take array sub data[j] values and put it into array sub data[i]
data[j] = temp; //take the temp values and put it into arra sub data[j]
i++;
j--;
/*****************************************************************************/
void Swap(int &x, int &y)
int temp = x;
x = y;
y = temp;
/*****************************************************************************/
void printData(int data[], int size, string title)
int i;
cout << endl << title << endl;
for(i=0; i<size; i++)
cout << data[i] << " ";
if(i%10 == 9 && size > 10)
cout << endl;
【问题讨论】:
【参考方案1】:字符字面量是char const(&)[]
。
所以你不能将它们传递给char*
。相反,将它们传递为const char*
,例如:
void Sort(int data[], int size, string sortAlg, const char* dataType)
您也在第 265 行有一个可变长度数组 (VLA)。这是 C99/C11 的东西,不在 C++ 中。
这里的赋值错误:
if(temp[i] = temp[j]) //if i value in the temp array is equal to the j value in the temp array
写if(temp[i] == temp[j])
比较整数。
partition
(int partition(int data[], int lo, int hi)
) 具有 Undefined Behaviour,因为您没有返回值(并且已使用)
您缺少代码:
//Write your code here
int hi, lo, mid;
if(lo <= hi)
也是Undefined Behaviour,因为hi
、lo
在那里未初始化。
在void InsertionSort(int data[], int size)
中相同
在void Sort(int*, int, std::string, const char*)
中相同
void MergeSort(int data[], int lo, int hi);
需要定义(已使用)
void QuickSort(int data[], int size, int lo, int hi)
有一个多余的 size
参数,它在声明的原型中不存在(第 27 行)。删除它
至少编译代码,没有为你完成实现:
Live On Coliru
【讨论】:
哇,太棒了。你有所有这些答案的模板吗?在 2 分钟内,这个答案太棒了。 @πάνταῥεῖ 我不知道。我有一个带有-Wall
和-pedantic
的编译器。每个人都应该使用它!
嗨!感谢您发表评论并帮助找到我的错误。很抱歉,我在开场时听起来有点苛刻,因为我的“大家好,这是……”被切断了。我做了一些你建议的改变,现在它编译了!非常感谢,现在我正在解决更多问题,例如当我使用合并排序时,它完成排序后,它崩溃,当它显示排序前后,它都显示它们已经排序。我会问我是否再次卡住,谢谢!
@SteveTam 我从来没有说过听起来要求很高 :) 如果有的话,我们不欣赏噪音meta.stackexchange.com/questions/2950/…以上是关于请帮助:[警告]不推荐将字符串常量转换为 'char*' [-Wwrite-strings]的主要内容,如果未能解决你的问题,请参考以下文章
C++ 警告:不推荐使用从字符串常量到“char*”的转换 [-Wwrite-strings]
带有构造函数的简单类引发警告:ISO C++ 禁止将字符串常量转换为 'char*' [重复]
PyArg_ParseTupleAndKeywords 抛出警告:ISO C++ 禁止将字符串常量转换为‘char*’ [-Wwrite-strings]