[英]Default argument of type "const char *" is incompatible with parameter of type "char *"
So I got this code from my teacher but it doesn`t work combined with other code, it works only if it is separatly in a project.所以我从我的老师那里得到了这段代码,但它不能与其他代码结合使用,只有在单独的项目中才有效。 The whole code works great, less this part
整个代码效果很好,少了这部分
"Notes" is an other class which works perfectly “Notes”是另一个完美运行的类
class student
{
char name[30];
notes marks;
public:
student(int = 8, char* =" "); //HERE IS WHERE I GOT THE PROBLEM, AT HIS CHAR*
~student();
void read_name();
void read_marks();
void modif_mark(int, double);
void print();
void check_marks();
};
/*...
...
...
*here is a lot of code working great*
...
...
...
*/
student::student(int nr_marks, char* sir) :
marks(nr_marks)
{
strcpy_s(name, sir);
}
Depending on compiler, C-style string literals may be allocated in readonly memory. 根据编译器,可以在只读内存中分配C样式的字符串文字。 Thus they are
const char[N+1]
( N
is the string length) (which is implicitly convertible to const char*
because of array to pointer decay) . 因此它们是
const char[N+1]
( N
是字符串长度) (由于数组到指针衰减,它可以隐式转换为const char*
) 。
The problem you're having is that it's illegal to drop const
qualifiers (with the exception of the infamous const_cast
or equivalent C-style cast) . 你遇到的问题是删除
const
限定符是非法的(除了臭名昭着的const_cast
或等效的C风格的const_cast
) 。
Since you're only reading from sir
, you can fix this by making sir
be const char*
instead, which doesn't violate const
: 既然你只是从
sir
那里读书,你可以通过使sir
成为const char*
而不违反const
来解决这个问题。
class student {
...
student(int = 8, const char* =" "); // problem solved
...
};
student::student(int nr_marks, const char* sir) : // remember to change it here as well
marks(nr_marks)
{
strcpy(name, sir);
}
About string literals : 关于字符串文字 :
In C, string literals are of type
char[]
, and can be assigned directly to a (non-const)char*
.在C中,字符串文字的类型为
char[]
,可以直接分配给(非const)char*
。 C++03 allowed it as well (but deprecated it, as literals are const in C++).C ++ 03也允许它(但不赞成它,因为文字在C ++中是const)。 C++11 no longer allows such assignments without a cast.
没有强制转换,C ++ 11不再允许这样的赋值。
Your teacher is possibly more versed in C or "dated" C++. 你的老师可能更精通C或“过时”的C ++。 As stated above the language (= modern C++) disallows the assignment / initialization of a
char*
from a string literal. 如上所述,语言(= modern C ++)不允许从字符串文字中赋值/初始化
char*
。
Workarounds: 解决方法:
Use char const *
as type for the parameter. 使用
char const *
作为参数的类型。 That's the reasonable solution when the string is not modified (why would you modify a string literal??) 当字符串未被修改时,这是合理的解决方案(为什么要修改字符串文字?)
Not recommended . 不推荐 。 When you need to write to that pointer: Store (a copy of) the string literal as a (non const)
char[]
and reference that. 当你需要写入该指针时:将字符串文字的(副本)存储为(非常量)
char[]
并引用它。 Issues: thread safety; 问题:线程安全; side effects;
副作用; why would you want to do this??
你为什么想做这个??
Better . 更好 。 If you need to write to / change the string: use
std::string
. 如果需要写入/更改字符串:use
std::string
。
Below is the error version下面是错误版本
print_results("C = ", c);
Below is the Solved Version以下是已解决的版本
print_results((char*)"C = ", c);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.