Optional parameter vector
How to make optional vector parameter? I read in some thread to use pointer to vector but I get following error:
redefinition of default argument: parameter 1
with code error C2572.
void EnumerateProcessInformations(std::vector<DWORD> &o_processId, const std::vector<std::wstring> *o_processName = nullptr)
{
HANDLE hSnapProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32W process;
process.dwSize = sizeof(PROCESSENTRY32W);
Process32FirstW(hSnapProcess, &process);
do
{
if (process.th32ProcessID != 0)
{
o_processId.emplace_back(process.th32ProcessID);
if (*o_processName != nullptr) { *o_processName.emplace_back(process.szExeFile); }
}
} while (Process32NextW(hSnapProcess, &process));
CloseHandle(hSnapProcess);
}
int main()
{
std::vector<DWORD> processId;
EnumerateProcessInformations(processId, nullptr);
for (auto& p : processId)
{
std::cout << p << std::endl;
}
getchar();
return 0;
}
c++
add a comment |
How to make optional vector parameter? I read in some thread to use pointer to vector but I get following error:
redefinition of default argument: parameter 1
with code error C2572.
void EnumerateProcessInformations(std::vector<DWORD> &o_processId, const std::vector<std::wstring> *o_processName = nullptr)
{
HANDLE hSnapProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32W process;
process.dwSize = sizeof(PROCESSENTRY32W);
Process32FirstW(hSnapProcess, &process);
do
{
if (process.th32ProcessID != 0)
{
o_processId.emplace_back(process.th32ProcessID);
if (*o_processName != nullptr) { *o_processName.emplace_back(process.szExeFile); }
}
} while (Process32NextW(hSnapProcess, &process));
CloseHandle(hSnapProcess);
}
int main()
{
std::vector<DWORD> processId;
EnumerateProcessInformations(processId, nullptr);
for (auto& p : processId)
{
std::cout << p << std::endl;
}
getchar();
return 0;
}
c++
Please post the exact error message you get. "redifinition" is not a word. If you are running Visual Studio on a non-English system, install the English language pack to get English error messages.
– IInspectable
Nov 17 '18 at 19:32
@IInspectable the error nessage redefinition of default argument: parameter 1 with code error C2572. as i checked to solve it default paremeter should undefined. how make default paremeter undefined?
– stephen ju
Nov 17 '18 at 19:37
@stephenju post complete code somewhere. Do you have EnumerateProcessInformations() already declared?
– bunglehead
Nov 17 '18 at 21:26
add a comment |
How to make optional vector parameter? I read in some thread to use pointer to vector but I get following error:
redefinition of default argument: parameter 1
with code error C2572.
void EnumerateProcessInformations(std::vector<DWORD> &o_processId, const std::vector<std::wstring> *o_processName = nullptr)
{
HANDLE hSnapProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32W process;
process.dwSize = sizeof(PROCESSENTRY32W);
Process32FirstW(hSnapProcess, &process);
do
{
if (process.th32ProcessID != 0)
{
o_processId.emplace_back(process.th32ProcessID);
if (*o_processName != nullptr) { *o_processName.emplace_back(process.szExeFile); }
}
} while (Process32NextW(hSnapProcess, &process));
CloseHandle(hSnapProcess);
}
int main()
{
std::vector<DWORD> processId;
EnumerateProcessInformations(processId, nullptr);
for (auto& p : processId)
{
std::cout << p << std::endl;
}
getchar();
return 0;
}
c++
How to make optional vector parameter? I read in some thread to use pointer to vector but I get following error:
redefinition of default argument: parameter 1
with code error C2572.
void EnumerateProcessInformations(std::vector<DWORD> &o_processId, const std::vector<std::wstring> *o_processName = nullptr)
{
HANDLE hSnapProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
PROCESSENTRY32W process;
process.dwSize = sizeof(PROCESSENTRY32W);
Process32FirstW(hSnapProcess, &process);
do
{
if (process.th32ProcessID != 0)
{
o_processId.emplace_back(process.th32ProcessID);
if (*o_processName != nullptr) { *o_processName.emplace_back(process.szExeFile); }
}
} while (Process32NextW(hSnapProcess, &process));
CloseHandle(hSnapProcess);
}
int main()
{
std::vector<DWORD> processId;
EnumerateProcessInformations(processId, nullptr);
for (auto& p : processId)
{
std::cout << p << std::endl;
}
getchar();
return 0;
}
c++
c++
edited Nov 17 '18 at 23:05
user6910411
33.8k976100
33.8k976100
asked Nov 17 '18 at 18:36
stephen justephen ju
2
2
Please post the exact error message you get. "redifinition" is not a word. If you are running Visual Studio on a non-English system, install the English language pack to get English error messages.
– IInspectable
Nov 17 '18 at 19:32
@IInspectable the error nessage redefinition of default argument: parameter 1 with code error C2572. as i checked to solve it default paremeter should undefined. how make default paremeter undefined?
– stephen ju
Nov 17 '18 at 19:37
@stephenju post complete code somewhere. Do you have EnumerateProcessInformations() already declared?
– bunglehead
Nov 17 '18 at 21:26
add a comment |
Please post the exact error message you get. "redifinition" is not a word. If you are running Visual Studio on a non-English system, install the English language pack to get English error messages.
– IInspectable
Nov 17 '18 at 19:32
@IInspectable the error nessage redefinition of default argument: parameter 1 with code error C2572. as i checked to solve it default paremeter should undefined. how make default paremeter undefined?
– stephen ju
Nov 17 '18 at 19:37
@stephenju post complete code somewhere. Do you have EnumerateProcessInformations() already declared?
– bunglehead
Nov 17 '18 at 21:26
Please post the exact error message you get. "redifinition" is not a word. If you are running Visual Studio on a non-English system, install the English language pack to get English error messages.
– IInspectable
Nov 17 '18 at 19:32
Please post the exact error message you get. "redifinition" is not a word. If you are running Visual Studio on a non-English system, install the English language pack to get English error messages.
– IInspectable
Nov 17 '18 at 19:32
@IInspectable the error nessage redefinition of default argument: parameter 1 with code error C2572. as i checked to solve it default paremeter should undefined. how make default paremeter undefined?
– stephen ju
Nov 17 '18 at 19:37
@IInspectable the error nessage redefinition of default argument: parameter 1 with code error C2572. as i checked to solve it default paremeter should undefined. how make default paremeter undefined?
– stephen ju
Nov 17 '18 at 19:37
@stephenju post complete code somewhere. Do you have EnumerateProcessInformations() already declared?
– bunglehead
Nov 17 '18 at 21:26
@stephenju post complete code somewhere. Do you have EnumerateProcessInformations() already declared?
– bunglehead
Nov 17 '18 at 21:26
add a comment |
1 Answer
1
active
oldest
votes
There are two problems with your use of a pointer:
you are dereferencing the pointer before checking it for
nullptr
. You are not checking if the pointer is null, you are checking if the vector being pointer to is null.you are declaring it as pointing to a
const vector
, which means you can't call any mutating methods on the vector, likeemplace_back
.
Try this instead:
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName = nullptr);
...
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName)
{
...
if (o_processName) o_processName->emplace_back(process.szExeFile);
...
}
Also, the error quoted in the question is the result of applying default arguments in the function definition. Default arguments can only be used in function declarations.
– IInspectable
Nov 17 '18 at 19:14
some result redifinition of default argument 1
– stephen ju
Nov 17 '18 at 19:24
@IInspectable yes, but in the case of a standalone function that is implemented solely in the same cpp file it is used in, its declaration and definition may be one and the same thing. And the way the OP's code is presented, that may be the case in this situation.
– Remy Lebeau
Nov 17 '18 at 22:10
The fact that the OP presented a compiler error is enough information to infer, that the code the OP posted is incomplete, and there is a declaration preceding the definition.
– IInspectable
Nov 18 '18 at 3:56
@IInspectable I updated my answer
– Remy Lebeau
Nov 18 '18 at 19:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354298%2foptional-parameter-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are two problems with your use of a pointer:
you are dereferencing the pointer before checking it for
nullptr
. You are not checking if the pointer is null, you are checking if the vector being pointer to is null.you are declaring it as pointing to a
const vector
, which means you can't call any mutating methods on the vector, likeemplace_back
.
Try this instead:
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName = nullptr);
...
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName)
{
...
if (o_processName) o_processName->emplace_back(process.szExeFile);
...
}
Also, the error quoted in the question is the result of applying default arguments in the function definition. Default arguments can only be used in function declarations.
– IInspectable
Nov 17 '18 at 19:14
some result redifinition of default argument 1
– stephen ju
Nov 17 '18 at 19:24
@IInspectable yes, but in the case of a standalone function that is implemented solely in the same cpp file it is used in, its declaration and definition may be one and the same thing. And the way the OP's code is presented, that may be the case in this situation.
– Remy Lebeau
Nov 17 '18 at 22:10
The fact that the OP presented a compiler error is enough information to infer, that the code the OP posted is incomplete, and there is a declaration preceding the definition.
– IInspectable
Nov 18 '18 at 3:56
@IInspectable I updated my answer
– Remy Lebeau
Nov 18 '18 at 19:34
add a comment |
There are two problems with your use of a pointer:
you are dereferencing the pointer before checking it for
nullptr
. You are not checking if the pointer is null, you are checking if the vector being pointer to is null.you are declaring it as pointing to a
const vector
, which means you can't call any mutating methods on the vector, likeemplace_back
.
Try this instead:
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName = nullptr);
...
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName)
{
...
if (o_processName) o_processName->emplace_back(process.szExeFile);
...
}
Also, the error quoted in the question is the result of applying default arguments in the function definition. Default arguments can only be used in function declarations.
– IInspectable
Nov 17 '18 at 19:14
some result redifinition of default argument 1
– stephen ju
Nov 17 '18 at 19:24
@IInspectable yes, but in the case of a standalone function that is implemented solely in the same cpp file it is used in, its declaration and definition may be one and the same thing. And the way the OP's code is presented, that may be the case in this situation.
– Remy Lebeau
Nov 17 '18 at 22:10
The fact that the OP presented a compiler error is enough information to infer, that the code the OP posted is incomplete, and there is a declaration preceding the definition.
– IInspectable
Nov 18 '18 at 3:56
@IInspectable I updated my answer
– Remy Lebeau
Nov 18 '18 at 19:34
add a comment |
There are two problems with your use of a pointer:
you are dereferencing the pointer before checking it for
nullptr
. You are not checking if the pointer is null, you are checking if the vector being pointer to is null.you are declaring it as pointing to a
const vector
, which means you can't call any mutating methods on the vector, likeemplace_back
.
Try this instead:
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName = nullptr);
...
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName)
{
...
if (o_processName) o_processName->emplace_back(process.szExeFile);
...
}
There are two problems with your use of a pointer:
you are dereferencing the pointer before checking it for
nullptr
. You are not checking if the pointer is null, you are checking if the vector being pointer to is null.you are declaring it as pointing to a
const vector
, which means you can't call any mutating methods on the vector, likeemplace_back
.
Try this instead:
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName = nullptr);
...
void EnumerateProcessInformations(..., std::vector<std::wstring> *o_processName)
{
...
if (o_processName) o_processName->emplace_back(process.szExeFile);
...
}
edited Nov 18 '18 at 19:34
answered Nov 17 '18 at 19:09
Remy LebeauRemy Lebeau
335k18256450
335k18256450
Also, the error quoted in the question is the result of applying default arguments in the function definition. Default arguments can only be used in function declarations.
– IInspectable
Nov 17 '18 at 19:14
some result redifinition of default argument 1
– stephen ju
Nov 17 '18 at 19:24
@IInspectable yes, but in the case of a standalone function that is implemented solely in the same cpp file it is used in, its declaration and definition may be one and the same thing. And the way the OP's code is presented, that may be the case in this situation.
– Remy Lebeau
Nov 17 '18 at 22:10
The fact that the OP presented a compiler error is enough information to infer, that the code the OP posted is incomplete, and there is a declaration preceding the definition.
– IInspectable
Nov 18 '18 at 3:56
@IInspectable I updated my answer
– Remy Lebeau
Nov 18 '18 at 19:34
add a comment |
Also, the error quoted in the question is the result of applying default arguments in the function definition. Default arguments can only be used in function declarations.
– IInspectable
Nov 17 '18 at 19:14
some result redifinition of default argument 1
– stephen ju
Nov 17 '18 at 19:24
@IInspectable yes, but in the case of a standalone function that is implemented solely in the same cpp file it is used in, its declaration and definition may be one and the same thing. And the way the OP's code is presented, that may be the case in this situation.
– Remy Lebeau
Nov 17 '18 at 22:10
The fact that the OP presented a compiler error is enough information to infer, that the code the OP posted is incomplete, and there is a declaration preceding the definition.
– IInspectable
Nov 18 '18 at 3:56
@IInspectable I updated my answer
– Remy Lebeau
Nov 18 '18 at 19:34
Also, the error quoted in the question is the result of applying default arguments in the function definition. Default arguments can only be used in function declarations.
– IInspectable
Nov 17 '18 at 19:14
Also, the error quoted in the question is the result of applying default arguments in the function definition. Default arguments can only be used in function declarations.
– IInspectable
Nov 17 '18 at 19:14
some result redifinition of default argument 1
– stephen ju
Nov 17 '18 at 19:24
some result redifinition of default argument 1
– stephen ju
Nov 17 '18 at 19:24
@IInspectable yes, but in the case of a standalone function that is implemented solely in the same cpp file it is used in, its declaration and definition may be one and the same thing. And the way the OP's code is presented, that may be the case in this situation.
– Remy Lebeau
Nov 17 '18 at 22:10
@IInspectable yes, but in the case of a standalone function that is implemented solely in the same cpp file it is used in, its declaration and definition may be one and the same thing. And the way the OP's code is presented, that may be the case in this situation.
– Remy Lebeau
Nov 17 '18 at 22:10
The fact that the OP presented a compiler error is enough information to infer, that the code the OP posted is incomplete, and there is a declaration preceding the definition.
– IInspectable
Nov 18 '18 at 3:56
The fact that the OP presented a compiler error is enough information to infer, that the code the OP posted is incomplete, and there is a declaration preceding the definition.
– IInspectable
Nov 18 '18 at 3:56
@IInspectable I updated my answer
– Remy Lebeau
Nov 18 '18 at 19:34
@IInspectable I updated my answer
– Remy Lebeau
Nov 18 '18 at 19:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354298%2foptional-parameter-vector%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please post the exact error message you get. "redifinition" is not a word. If you are running Visual Studio on a non-English system, install the English language pack to get English error messages.
– IInspectable
Nov 17 '18 at 19:32
@IInspectable the error nessage redefinition of default argument: parameter 1 with code error C2572. as i checked to solve it default paremeter should undefined. how make default paremeter undefined?
– stephen ju
Nov 17 '18 at 19:37
@stephenju post complete code somewhere. Do you have EnumerateProcessInformations() already declared?
– bunglehead
Nov 17 '18 at 21:26