相关疑难解决方法(0)

C++ 11初始化列表失败 - 但仅限于长度为2的列表

我找到了一个不起眼的日志记录错误,事实上长度为2的初始化列表似乎是一个特例!这怎么可能?

代码是使用Apple LLVM版本5.1(clang-503.0.40)编译的CXXFLAGS=-std=c++11 -stdlib=libc++.

#include <stdio.h>

#include <string>
#include <vector>

using namespace std;

typedef vector<string> Strings;

void print(string const& s) {
    printf(s.c_str());
    printf("\n");
}

void print(Strings const& ss, string const& name) {
    print("Test " + name);
    print("Number of strings: " + to_string(ss.size()));
    for (auto& s: ss) {
        auto t = "length = " + to_string(s.size()) + ": " + s;
        print(t);
    }
    print("\n");
}

void test() {
    Strings a{{"hello"}};                  print(a, "a");
    Strings b{{"hello", "there"}};         print(b, "b");
    Strings c{{"hello", …
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list c++11

71
推荐指数
2
解决办法
2933
查看次数

C++警告:不推荐将字符串常量转换为'char*'[-Wwrite-strings]

我正在使用gnuplot在C++中绘制图形.该图正如预期的那样绘制,但在编译期间会出现警告.警告意味着什么?

warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的功能:

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]); …
Run Code Online (Sandbox Code Playgroud)

c++ string-literals

22
推荐指数
1
解决办法
4万
查看次数

标签 统计

c++ ×2

c++11 ×1

initializer-list ×1

string-literals ×1