O.K.所以我想要做的是創建一個名爲greeter的類,讓它獲得當前用戶名,然後將它傳遞給一個函數,說「你好(用戶名)」。C++在類中獲取用戶名並將其傳遞給函數
我到目前爲止是這樣的:
#include <iostream>
#include <string>
using namespace std;
class greet
{
public:
void hello(string name)
{
cout << "Hello, " + name + "!" << endl;
}
};
int main()
{
greet user;
user.hello(name);
return 0;
}
「名」是origionally會作爲一個參數來自用戶的輸入來,但
user.hello()
直接傳遞給函數不會接受變量「名稱」,我寧願程序無論如何都得到它自己的用戶名。所以我的問題是如何讓C++自己獲取用戶名並將其從變量傳遞到user.hello()
你靶向什麼平臺?不同的操作系統對用戶進行不同的實現,並提供不同的API來查詢用戶信息。 C++本身或STL沒有任何東西可以爲你處理。 –
最好是跨平臺的解決方案,但我的系統是Linux,因此如果它不能「一刀切」,那麼對於Linux。 – Josh
我不是一個實用的項目,只是爲了幫助我找出課程 – Josh