我知道內聯函數是一些函數,它們的主體被插入到被調用的地方。然後在被調用時,爲什麼因此不影響範圍內聯函數的變化:爲什麼內聯函數不能查看內部作用域?
#include <iostream>
inline void alert(const std::string &str) { cout << str; }
int main() {
using namespace std;
alert("Hello World"); // cout << "Hello World";
}
這不工作,因爲我得到的錯誤cout was not declared in this scope
,但如果我不std::cout
它。爲什麼C++不知道cout
是否是std
的成員,如果內聯函數的函數體被插入到作用域中的話?
你不會把它改成'std :: cout'嗎?或者在頂部放置'using namespace std;'?似乎對我更好。 – MartyE 2012-08-14 23:44:47