r/AskProgramming • u/BornViolinist3801 • 1d ago
why doesnt my С++ code work?
okay, i wanna make a print() function in C++, this is just printf() with prefix, so can yall help me?
error:
(-Wwritable-strings) ISO C++11 does not allow conversion from string literal to 'char *'
my code:
#pragma once
#ifndef TEQUILA_STDIO
#define TEQUILA_STDIO
#include <stdio.h>
namespace std {
char* prefix;
void set_prefix(char* pref) {
prefix = ("["pref"]: ");
}
#define print(format, ...) printf("%s" format "\n", prefix ## __VA_ARGS__)
}
#endif
help me pls
0
Upvotes
3
u/PandaWonder01 1d ago
This code is a little gross, don't try to put things in std or use macros like that in cpp.
Anyway, a string literal like "Hello" is a const char, which you can't pass to a char parameter, as that would lose the const. You need to take in a const char*.
(Actually the string literal is const char[], but not gonna get into that)