繁体   English   中英

cpp 左值与右值代码| error: expected ',' or '...' before '&&' token, 错误: 'name' was not declared in this scope

[英]cpp l-value vs r-value code| error: expected ',' or '...' before '&&' token, error: 'name' was not declared in this scope

我是 cpp 中这个主题的新手并试图理解这段代码,所以请告诉我左值和右值在这段代码中有何不同以及它们是如何实现的。 另外为什么我在这里出错(此代码的执行取决于 c++ 版本)?

void printName(const std::string& name){
    std::cout << "[l-value] " << name << std::endl;
}
void printName(std::string&& name){ //Line 8
    std::cout << "[r-value] " << name << std::endl; //Line 9
}
int main(){
    std::string firstname = "Harry";
    std::string lastname = "Singh";

    std::string fullname = firstname + lastname;
    printName(firstname);
    printName(lastname);
    printName(firstname+lastname);
}

我在以下代码中收到以下提到的错误。

8 error: expected ',' or '...' before '&&' token
9 error: 'name' was not declared in this scope

右值引用 ( && ) 是 C++11 及以后的功能。 这是因为您使用的是 C++98 或更早版本:

添加此编译器标志-std=c++11 (或-std=c++14-std=c++17-std=c++20 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM