【发布时间】:2014-02-03 13:53:15
【问题描述】:
我正在使用 gnuplot 在 C++ 中绘制图形。该图正在按预期绘制,但在编译期间出现警告。警告是什么意思?
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
这是我正在使用的功能:
void plotgraph(double xvals[],double yvals[], int NUM_POINTS)
{
char * commandsForGnuplot[] = {"set title \"Probability Graph\"",
"plot 'data.temp' with lines"};
FILE * temp = fopen("data.temp", "w");
FILE * gnuplotPipe = popen ("gnuplot -persistent ", "w");
int i;
for (i=0; i < NUM_POINTS; i++)
{
fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]);
//Write the data to a te mporary file
}
for (i=0; i < NUM_COMMANDS; i++)
{
fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]);
//Send commands to gn uplot one by one.
}
fflush(gnuplotPipe);
}
【问题讨论】:
-
这是 C,不是 C++
-
@Davidbrcz 首先,尽管它是 C 风格,但它仍然是有效的 C++ 代码。其次,将标签更改为 C 是不正确的,因为这不是 C 会发生的问题。在 C 中,字符串文字不是
const类型。 -
@Davidbrcz 另请参阅Retagging C++ questions as C without consulting asker。
-
@余浩:在语法上是有效的。但是,编写该代码并声称它是用 C++ 编写的,在道德上是错误的。如果你在我的部门写了这个,你会立即被解雇。我的坏处是重新编排。但是我仍然认为让C++标签带有它是错误的。这维持了您可以像做 C 一样做 C++ 的错误想法。它们是 2 种独立的语言,不应同时标记任何问题,因为规范、习语和工具并不相同。要么是 C,要么是 C++,而不是两者。
-
两者都没有标记,它被标记为 C++。此外,C++ 支持许多不同的范例。 IMO 如果程序正确且整洁,则没有理由说代码错误,因为应该使用不同的范例。 OP的代码易读易懂。
标签: c++ string-literals