C++: cannot define member function












-1















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.










share|improve this question

























  • 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
















-1















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.










share|improve this question

























  • 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














-1












-1








-1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












1 Answer
1






active

oldest

votes


















3














[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.






share|improve this answer
























  • 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





    @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











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
});


}
});














draft saved

draft discarded


















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









3














[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.






share|improve this answer
























  • 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





    @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
















3














[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.






share|improve this answer
























  • 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





    @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














3












3








3







[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.






share|improve this answer













[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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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





    @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



















  • 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





    @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

















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python