在 C++(Microsoft Visual Studio)中,我在 mystring.h 中有:main_savitch_4::string::string(const char str[ ] = "") 并添加到 mystring.cpp 中:
main_savitch_4::string::string(const char str[ ] = ""){
allocated = 0;
current_length = 0;
start = new char[0];
}, which defines the private variables.
我收到以下错误:
c:\users\erice\documents\c++_2\lab04\rice_lab04_2\rice_lab04_2\mystring.cpp(13): error C2572: 'main_savitch_4::string::string' : redefinition of default parameter : parameter 1
1> c:\users\erice\documents\c++_2\lab04\rice_lab04_2\rice_lab04_2\mystring.h(88) : see declaration of 'main_savitch_4::string::string'
1>.
到底出了什么问题???我不明白 = 在函数描述中的意思
最佳答案
编译器告诉您在函数的声明和定义中有默认参数。从定义中删除它:
main_savitch_4::string::string(const char str[ ]) { ... }
I don't understand what = means in a function description
这意味着您正在为函数提供 derfault 参数。例如,
void foo(int i = 42); // declaration
上面的意思是你可以在没有参数的情况下调用foo
:
....
void foo(int i) { return i+100; } // definition
// call the function
foo(); // calls it as foo(42);
foo(123);
关于c++ - 函数声明中的等式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14843545/