【发布时间】:2021-05-22 19:58:36
【问题描述】:
为什么 print() 函数不能访问 msg 变量?
#include <iostream>
void print()
{
std::cout << msg << std::endl;
}
int main()
{
std::string msg{"Hello"};
print();
}
Error: 'msg' was not declared in this scope
【问题讨论】:
-
因为你不能,在 C++ 中?为什么您希望它以这种方式可以访问?
-
你来自什么语言?
-
@KPCT 来自Python,昨天刚开始学C++。
-
我不认为你可以在 Python 中做到这一点......
-
你应该了解变量的作用域,有很多资料可以解释这一点,比如tutorialspoint.com/cplusplus/cpp_variable_scope.htm
标签: c++