【发布时间】:2019-05-25 16:45:33
【问题描述】:
我在 CLion 中遇到了一种奇怪的行为。我有两个函数将std::string 和const std::string& 作为参数。我称它们为传递字符串文字。
我尝试过使用不同版本的 C++:C++17 和 C++14
#include <iostream>
#include <string>
void print(std::string str){
std::cout<<str<<std::endl;
}
void print_ref(const std::string& str){
std::cout<<str<<std::endl;
}
int main() {
print("Hello World!");
print_ref("Hello World!");
return 0;
}
如果代码是 C++17,CLion(或更好的 IntelliSense)会报告此警告 Paramemeter type mismatch: Types 'std::string' and 'const char[13]' are not compatible,但如果我切换到 C++14,则警告会消失。代码正确吗?
【问题讨论】:
-
是编译器警告(即在您构建代码后显示)还是 IntelliSense-whatever-it's-call-there 警告(将鼠标悬停在代码上时显示) ?
-
你有什么问题?
-
你需要
#include <string> -
@NeilButterworth 它不能解决问题
-
@NeilButterworth @eeroika @πάντα 谢谢。我已经添加了
#include <string>,但是IntelliSense的警告仍然存在,那么我认为这只是一个CLion问题。
标签: c++ cmake c++14 c++17 clion