C++: cannot define member function
Could you please advise why I am getting the error in the code below?
error: cannot define member function ‘Test<int>::Printer::Print’ within ‘Test<int>’
I am using gcc
version 8.1.1
and compile the code as g++ -std=c++11
.
Although, if I move the definition of function Print
under the definition of struct Printer
(i.e. making it inline implicitly), the compiler does not produce any error.
#include <iostream>
template <typename Type>
struct TestBase {
struct Printer {
template <typename T>
void Print(const T& t) {
std::cout << t << std::endl;
}
};
};
template <typename Type>
struct Test;
template<>
struct Test<int> : public TestBase<int> {
struct Printer : public TestBase<int>::Printer {
template <typename T>
void Print(int i, const T& t);
};
template <typename T>
void Printer::Print(int i, const T& t) {
std::cout << i << t << std::endl;
}
};
int main() {
Test<int> t;
}
UPDATE:
Brian pointed out the exact reason why it is the case: "... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition..."
Brian not only answered the main question that started this topic but also an additional question that I asked in the comment to the accepted answer of him.
c++ c++11
add a comment |
Could you please advise why I am getting the error in the code below?
error: cannot define member function ‘Test<int>::Printer::Print’ within ‘Test<int>’
I am using gcc
version 8.1.1
and compile the code as g++ -std=c++11
.
Although, if I move the definition of function Print
under the definition of struct Printer
(i.e. making it inline implicitly), the compiler does not produce any error.
#include <iostream>
template <typename Type>
struct TestBase {
struct Printer {
template <typename T>
void Print(const T& t) {
std::cout << t << std::endl;
}
};
};
template <typename Type>
struct Test;
template<>
struct Test<int> : public TestBase<int> {
struct Printer : public TestBase<int>::Printer {
template <typename T>
void Print(int i, const T& t);
};
template <typename T>
void Printer::Print(int i, const T& t) {
std::cout << i << t << std::endl;
}
};
int main() {
Test<int> t;
}
UPDATE:
Brian pointed out the exact reason why it is the case: "... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition..."
Brian not only answered the main question that started this topic but also an additional question that I asked in the comment to the accepted answer of him.
c++ c++11
Read the error message carefully. The compiler is telling you the truth. If you want to know why, it's because that's specified by the C++ standard.
– paddy
Nov 15 '18 at 20:55
I think I understand the compiler's message but I don't understand which rule is violated. Could you please help me to point that out?
– TruLa
Nov 15 '18 at 20:58
add a comment |
Could you please advise why I am getting the error in the code below?
error: cannot define member function ‘Test<int>::Printer::Print’ within ‘Test<int>’
I am using gcc
version 8.1.1
and compile the code as g++ -std=c++11
.
Although, if I move the definition of function Print
under the definition of struct Printer
(i.e. making it inline implicitly), the compiler does not produce any error.
#include <iostream>
template <typename Type>
struct TestBase {
struct Printer {
template <typename T>
void Print(const T& t) {
std::cout << t << std::endl;
}
};
};
template <typename Type>
struct Test;
template<>
struct Test<int> : public TestBase<int> {
struct Printer : public TestBase<int>::Printer {
template <typename T>
void Print(int i, const T& t);
};
template <typename T>
void Printer::Print(int i, const T& t) {
std::cout << i << t << std::endl;
}
};
int main() {
Test<int> t;
}
UPDATE:
Brian pointed out the exact reason why it is the case: "... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition..."
Brian not only answered the main question that started this topic but also an additional question that I asked in the comment to the accepted answer of him.
c++ c++11
Could you please advise why I am getting the error in the code below?
error: cannot define member function ‘Test<int>::Printer::Print’ within ‘Test<int>’
I am using gcc
version 8.1.1
and compile the code as g++ -std=c++11
.
Although, if I move the definition of function Print
under the definition of struct Printer
(i.e. making it inline implicitly), the compiler does not produce any error.
#include <iostream>
template <typename Type>
struct TestBase {
struct Printer {
template <typename T>
void Print(const T& t) {
std::cout << t << std::endl;
}
};
};
template <typename Type>
struct Test;
template<>
struct Test<int> : public TestBase<int> {
struct Printer : public TestBase<int>::Printer {
template <typename T>
void Print(int i, const T& t);
};
template <typename T>
void Printer::Print(int i, const T& t) {
std::cout << i << t << std::endl;
}
};
int main() {
Test<int> t;
}
UPDATE:
Brian pointed out the exact reason why it is the case: "... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition..."
Brian not only answered the main question that started this topic but also an additional question that I asked in the comment to the accepted answer of him.
c++ c++11
c++ c++11
edited Nov 15 '18 at 21:19
TruLa
asked Nov 15 '18 at 20:51
TruLaTruLa
358212
358212
Read the error message carefully. The compiler is telling you the truth. If you want to know why, it's because that's specified by the C++ standard.
– paddy
Nov 15 '18 at 20:55
I think I understand the compiler's message but I don't understand which rule is violated. Could you please help me to point that out?
– TruLa
Nov 15 '18 at 20:58
add a comment |
Read the error message carefully. The compiler is telling you the truth. If you want to know why, it's because that's specified by the C++ standard.
– paddy
Nov 15 '18 at 20:55
I think I understand the compiler's message but I don't understand which rule is violated. Could you please help me to point that out?
– TruLa
Nov 15 '18 at 20:58
Read the error message carefully. The compiler is telling you the truth. If you want to know why, it's because that's specified by the C++ standard.
– paddy
Nov 15 '18 at 20:55
Read the error message carefully. The compiler is telling you the truth. If you want to know why, it's because that's specified by the C++ standard.
– paddy
Nov 15 '18 at 20:55
I think I understand the compiler's message but I don't understand which rule is violated. Could you please help me to point that out?
– TruLa
Nov 15 '18 at 20:58
I think I understand the compiler's message but I don't understand which rule is violated. Could you please help me to point that out?
– TruLa
Nov 15 '18 at 20:58
add a comment |
1 Answer
1
active
oldest
votes
[class.mfct]/1, emphasis mine:
... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition. ...
An enclosing class scope is thus not an allowed location for the definition.
Brian, thank you very much for such a prompt and precise answer. I have a suspicion that this might not be the end of a story. Let's move the member functionPrint
definition inside thestruct Printer
definition and change themain
function asTest<int>::Printer p; p.Print(3);
. I'll start getting theno matching function for call to
error message. Is it somehow related to your answer? Is it becausevoid Print(const T& t)
is defined outside of the class definition?
– TruLa
Nov 15 '18 at 21:10
1
@TruLa This is becauseTest::Printer::Print
takes two arguments and it hidesTestBase::Printer::Print
. See stackoverflow.com/questions/1896830/…
– Brian
Nov 15 '18 at 21:16
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%2f53327727%2fc-cannot-define-member-function%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
[class.mfct]/1, emphasis mine:
... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition. ...
An enclosing class scope is thus not an allowed location for the definition.
Brian, thank you very much for such a prompt and precise answer. I have a suspicion that this might not be the end of a story. Let's move the member functionPrint
definition inside thestruct Printer
definition and change themain
function asTest<int>::Printer p; p.Print(3);
. I'll start getting theno matching function for call to
error message. Is it somehow related to your answer? Is it becausevoid Print(const T& t)
is defined outside of the class definition?
– TruLa
Nov 15 '18 at 21:10
1
@TruLa This is becauseTest::Printer::Print
takes two arguments and it hidesTestBase::Printer::Print
. See stackoverflow.com/questions/1896830/…
– Brian
Nov 15 '18 at 21:16
add a comment |
[class.mfct]/1, emphasis mine:
... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition. ...
An enclosing class scope is thus not an allowed location for the definition.
Brian, thank you very much for such a prompt and precise answer. I have a suspicion that this might not be the end of a story. Let's move the member functionPrint
definition inside thestruct Printer
definition and change themain
function asTest<int>::Printer p; p.Print(3);
. I'll start getting theno matching function for call to
error message. Is it somehow related to your answer? Is it becausevoid Print(const T& t)
is defined outside of the class definition?
– TruLa
Nov 15 '18 at 21:10
1
@TruLa This is becauseTest::Printer::Print
takes two arguments and it hidesTestBase::Printer::Print
. See stackoverflow.com/questions/1896830/…
– Brian
Nov 15 '18 at 21:16
add a comment |
[class.mfct]/1, emphasis mine:
... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition. ...
An enclosing class scope is thus not an allowed location for the definition.
[class.mfct]/1, emphasis mine:
... A member function definition that appears outside of the class definition shall appear in a namespace scope enclosing the class definition. ...
An enclosing class scope is thus not an allowed location for the definition.
answered Nov 15 '18 at 21:03
BrianBrian
65.4k797185
65.4k797185
Brian, thank you very much for such a prompt and precise answer. I have a suspicion that this might not be the end of a story. Let's move the member functionPrint
definition inside thestruct Printer
definition and change themain
function asTest<int>::Printer p; p.Print(3);
. I'll start getting theno matching function for call to
error message. Is it somehow related to your answer? Is it becausevoid Print(const T& t)
is defined outside of the class definition?
– TruLa
Nov 15 '18 at 21:10
1
@TruLa This is becauseTest::Printer::Print
takes two arguments and it hidesTestBase::Printer::Print
. See stackoverflow.com/questions/1896830/…
– Brian
Nov 15 '18 at 21:16
add a comment |
Brian, thank you very much for such a prompt and precise answer. I have a suspicion that this might not be the end of a story. Let's move the member functionPrint
definition inside thestruct Printer
definition and change themain
function asTest<int>::Printer p; p.Print(3);
. I'll start getting theno matching function for call to
error message. Is it somehow related to your answer? Is it becausevoid Print(const T& t)
is defined outside of the class definition?
– TruLa
Nov 15 '18 at 21:10
1
@TruLa This is becauseTest::Printer::Print
takes two arguments and it hidesTestBase::Printer::Print
. See stackoverflow.com/questions/1896830/…
– Brian
Nov 15 '18 at 21:16
Brian, thank you very much for such a prompt and precise answer. I have a suspicion that this might not be the end of a story. Let's move the member function
Print
definition inside the struct Printer
definition and change the main
function as Test<int>::Printer p; p.Print(3);
. I'll start getting the no matching function for call to
error message. Is it somehow related to your answer? Is it because void Print(const T& t)
is defined outside of the class definition?– TruLa
Nov 15 '18 at 21:10
Brian, thank you very much for such a prompt and precise answer. I have a suspicion that this might not be the end of a story. Let's move the member function
Print
definition inside the struct Printer
definition and change the main
function as Test<int>::Printer p; p.Print(3);
. I'll start getting the no matching function for call to
error message. Is it somehow related to your answer? Is it because void Print(const T& t)
is defined outside of the class definition?– TruLa
Nov 15 '18 at 21:10
1
1
@TruLa This is because
Test::Printer::Print
takes two arguments and it hides TestBase::Printer::Print
. See stackoverflow.com/questions/1896830/…– Brian
Nov 15 '18 at 21:16
@TruLa This is because
Test::Printer::Print
takes two arguments and it hides TestBase::Printer::Print
. See stackoverflow.com/questions/1896830/…– Brian
Nov 15 '18 at 21:16
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%2f53327727%2fc-cannot-define-member-function%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
Read the error message carefully. The compiler is telling you the truth. If you want to know why, it's because that's specified by the C++ standard.
– paddy
Nov 15 '18 at 20:55
I think I understand the compiler's message but I don't understand which rule is violated. Could you please help me to point that out?
– TruLa
Nov 15 '18 at 20:58