c++ - 函数声明中的等式

标签 c++ function function-declaration

在 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/

相关文章:

c++ - 动态创建的运算符

c++ - 什么是 -fPIC 编译选项?

C typedef 结构的类型冲突

c - C 中的 Malloc - 返回值

function - OCaml:在定义函数之前声明它

c++ - 避免循环的 timeGetTime 的最佳替代品是什么?

C++/调试(AIX 上的 g++)递归快速排序导致段错误

C程序: Integer array pointer changes values when passed as parameter

c++ - 为什么 MSVS 允许 NULL 作为纯虚函数说明符?

c++ - 将函数声明转换为变量定义的简单技巧