下面的代码给了我一个编译错误,但我不明白我做错了什么。抱歉问了这么愚蠢的问题。
$ cat swapcstrings.cc
#include <iostream>
void swap(char*& c, char*& d) {
char* temp = c;
c = d;
d = temp;
}
int main() {
char c[] = "abcdef";
char d[] = "ghijkl";
std::cout << "[" << c << "," << d << "]\n";
swap(c, d);
std::cout << "[" << c << "," << d << "]\n";
}
$ g++ swapcstrings.cc
swapcstrings.cc: In function ‘int main()’:
swapcstrings.cc:13: error: invalid initialization of non-const reference of type ‘char*&’ from a temporary of type ‘char*’
swapcstrings.cc:3: error: in passing argument 1 of ‘void swap(char*&, char*&)’
$