Syntax for Function Return Type in a Header File for a Nested Template Class Return Type in C++












0















I am working on building my own vector class similar to the vector you would find the C++ STL. In my header file have two classes, my vector class and my iterator class, which is a nested template class. I am wondering what is the proper syntax is for the return type for a nested iterator class. In this case with my code, I am looking for the right way to write the proper return type syntax for my begin() function and my end() function, both of which are in my vector class. Both begin() and end() are supposed to have return type of vector <T> :: iterator but that does not seem to be working. Here is how my code is currently set up.



template <class T>
iterator;

/************************************************
* VECTOR
* A class that holds stuff
***********************************************/
template <class T>
class vector
{
public:
// code removed for brevity

// Methods of the Vector class
vector <T> :: iterator begin();
vector <T> :: iterator end();


private:
// Code removed for brevity
};

/**************************************************
* VECTOR ITERATOR
* An iterator through array
*************************************************/
template <class T>
class vector <T> :: iterator
{
// ...code removed for brevity
};


Further, do I have the right code for the following two lines which are at the top of my .h file?



template <class T>
iterator;


Or would it be this?



template <class T>
vector <T> :: iterator;


Thanks!










share|improve this question

























  • If it's vector::iterator, Get rid of the forward declaration and define it inside vector. en.cppreference.com/w/cpp/language/nested_types

    – user4581301
    Jan 19 at 3:37













  • @user4581301 I am not sure what you mean when you said "If it's vector::iterator". My iterator is defined below as class vector <T> :: iterator.

    – benso8
    Jan 19 at 3:49











  • Templated or not, it doesn't matter. iterator is a class nested in vector. Nest it and walk away. Example: ideone.com/2LyVF3

    – user4581301
    Jan 19 at 3:52













  • In addition to what's already been said, It's hard to say without seeing the code where you use the iterators, but if you involve const you'll have problems. It's often easier to start making a const_iterator and build from there.

    – Ted Lyngmo
    Jan 19 at 3:58








  • 1





    Well, If it's gotta be outside the class, it's gotta be outside the class. Your first and most important job is to pass the class, so do something like this: ideone.com/iYGnO0

    – user4581301
    Jan 19 at 4:34
















0















I am working on building my own vector class similar to the vector you would find the C++ STL. In my header file have two classes, my vector class and my iterator class, which is a nested template class. I am wondering what is the proper syntax is for the return type for a nested iterator class. In this case with my code, I am looking for the right way to write the proper return type syntax for my begin() function and my end() function, both of which are in my vector class. Both begin() and end() are supposed to have return type of vector <T> :: iterator but that does not seem to be working. Here is how my code is currently set up.



template <class T>
iterator;

/************************************************
* VECTOR
* A class that holds stuff
***********************************************/
template <class T>
class vector
{
public:
// code removed for brevity

// Methods of the Vector class
vector <T> :: iterator begin();
vector <T> :: iterator end();


private:
// Code removed for brevity
};

/**************************************************
* VECTOR ITERATOR
* An iterator through array
*************************************************/
template <class T>
class vector <T> :: iterator
{
// ...code removed for brevity
};


Further, do I have the right code for the following two lines which are at the top of my .h file?



template <class T>
iterator;


Or would it be this?



template <class T>
vector <T> :: iterator;


Thanks!










share|improve this question

























  • If it's vector::iterator, Get rid of the forward declaration and define it inside vector. en.cppreference.com/w/cpp/language/nested_types

    – user4581301
    Jan 19 at 3:37













  • @user4581301 I am not sure what you mean when you said "If it's vector::iterator". My iterator is defined below as class vector <T> :: iterator.

    – benso8
    Jan 19 at 3:49











  • Templated or not, it doesn't matter. iterator is a class nested in vector. Nest it and walk away. Example: ideone.com/2LyVF3

    – user4581301
    Jan 19 at 3:52













  • In addition to what's already been said, It's hard to say without seeing the code where you use the iterators, but if you involve const you'll have problems. It's often easier to start making a const_iterator and build from there.

    – Ted Lyngmo
    Jan 19 at 3:58








  • 1





    Well, If it's gotta be outside the class, it's gotta be outside the class. Your first and most important job is to pass the class, so do something like this: ideone.com/iYGnO0

    – user4581301
    Jan 19 at 4:34














0












0








0








I am working on building my own vector class similar to the vector you would find the C++ STL. In my header file have two classes, my vector class and my iterator class, which is a nested template class. I am wondering what is the proper syntax is for the return type for a nested iterator class. In this case with my code, I am looking for the right way to write the proper return type syntax for my begin() function and my end() function, both of which are in my vector class. Both begin() and end() are supposed to have return type of vector <T> :: iterator but that does not seem to be working. Here is how my code is currently set up.



template <class T>
iterator;

/************************************************
* VECTOR
* A class that holds stuff
***********************************************/
template <class T>
class vector
{
public:
// code removed for brevity

// Methods of the Vector class
vector <T> :: iterator begin();
vector <T> :: iterator end();


private:
// Code removed for brevity
};

/**************************************************
* VECTOR ITERATOR
* An iterator through array
*************************************************/
template <class T>
class vector <T> :: iterator
{
// ...code removed for brevity
};


Further, do I have the right code for the following two lines which are at the top of my .h file?



template <class T>
iterator;


Or would it be this?



template <class T>
vector <T> :: iterator;


Thanks!










share|improve this question
















I am working on building my own vector class similar to the vector you would find the C++ STL. In my header file have two classes, my vector class and my iterator class, which is a nested template class. I am wondering what is the proper syntax is for the return type for a nested iterator class. In this case with my code, I am looking for the right way to write the proper return type syntax for my begin() function and my end() function, both of which are in my vector class. Both begin() and end() are supposed to have return type of vector <T> :: iterator but that does not seem to be working. Here is how my code is currently set up.



template <class T>
iterator;

/************************************************
* VECTOR
* A class that holds stuff
***********************************************/
template <class T>
class vector
{
public:
// code removed for brevity

// Methods of the Vector class
vector <T> :: iterator begin();
vector <T> :: iterator end();


private:
// Code removed for brevity
};

/**************************************************
* VECTOR ITERATOR
* An iterator through array
*************************************************/
template <class T>
class vector <T> :: iterator
{
// ...code removed for brevity
};


Further, do I have the right code for the following two lines which are at the top of my .h file?



template <class T>
iterator;


Or would it be this?



template <class T>
vector <T> :: iterator;


Thanks!







c++ vector data-structures inner-classes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 6:08







benso8

















asked Jan 19 at 3:28









benso8benso8

133




133













  • If it's vector::iterator, Get rid of the forward declaration and define it inside vector. en.cppreference.com/w/cpp/language/nested_types

    – user4581301
    Jan 19 at 3:37













  • @user4581301 I am not sure what you mean when you said "If it's vector::iterator". My iterator is defined below as class vector <T> :: iterator.

    – benso8
    Jan 19 at 3:49











  • Templated or not, it doesn't matter. iterator is a class nested in vector. Nest it and walk away. Example: ideone.com/2LyVF3

    – user4581301
    Jan 19 at 3:52













  • In addition to what's already been said, It's hard to say without seeing the code where you use the iterators, but if you involve const you'll have problems. It's often easier to start making a const_iterator and build from there.

    – Ted Lyngmo
    Jan 19 at 3:58








  • 1





    Well, If it's gotta be outside the class, it's gotta be outside the class. Your first and most important job is to pass the class, so do something like this: ideone.com/iYGnO0

    – user4581301
    Jan 19 at 4:34



















  • If it's vector::iterator, Get rid of the forward declaration and define it inside vector. en.cppreference.com/w/cpp/language/nested_types

    – user4581301
    Jan 19 at 3:37













  • @user4581301 I am not sure what you mean when you said "If it's vector::iterator". My iterator is defined below as class vector <T> :: iterator.

    – benso8
    Jan 19 at 3:49











  • Templated or not, it doesn't matter. iterator is a class nested in vector. Nest it and walk away. Example: ideone.com/2LyVF3

    – user4581301
    Jan 19 at 3:52













  • In addition to what's already been said, It's hard to say without seeing the code where you use the iterators, but if you involve const you'll have problems. It's often easier to start making a const_iterator and build from there.

    – Ted Lyngmo
    Jan 19 at 3:58








  • 1





    Well, If it's gotta be outside the class, it's gotta be outside the class. Your first and most important job is to pass the class, so do something like this: ideone.com/iYGnO0

    – user4581301
    Jan 19 at 4:34

















If it's vector::iterator, Get rid of the forward declaration and define it inside vector. en.cppreference.com/w/cpp/language/nested_types

– user4581301
Jan 19 at 3:37







If it's vector::iterator, Get rid of the forward declaration and define it inside vector. en.cppreference.com/w/cpp/language/nested_types

– user4581301
Jan 19 at 3:37















@user4581301 I am not sure what you mean when you said "If it's vector::iterator". My iterator is defined below as class vector <T> :: iterator.

– benso8
Jan 19 at 3:49





@user4581301 I am not sure what you mean when you said "If it's vector::iterator". My iterator is defined below as class vector <T> :: iterator.

– benso8
Jan 19 at 3:49













Templated or not, it doesn't matter. iterator is a class nested in vector. Nest it and walk away. Example: ideone.com/2LyVF3

– user4581301
Jan 19 at 3:52







Templated or not, it doesn't matter. iterator is a class nested in vector. Nest it and walk away. Example: ideone.com/2LyVF3

– user4581301
Jan 19 at 3:52















In addition to what's already been said, It's hard to say without seeing the code where you use the iterators, but if you involve const you'll have problems. It's often easier to start making a const_iterator and build from there.

– Ted Lyngmo
Jan 19 at 3:58







In addition to what's already been said, It's hard to say without seeing the code where you use the iterators, but if you involve const you'll have problems. It's often easier to start making a const_iterator and build from there.

– Ted Lyngmo
Jan 19 at 3:58






1




1





Well, If it's gotta be outside the class, it's gotta be outside the class. Your first and most important job is to pass the class, so do something like this: ideone.com/iYGnO0

– user4581301
Jan 19 at 4:34





Well, If it's gotta be outside the class, it's gotta be outside the class. Your first and most important job is to pass the class, so do something like this: ideone.com/iYGnO0

– user4581301
Jan 19 at 4:34












1 Answer
1






active

oldest

votes


















3














You can simply move the definition of iterator into vector:



template <class T>
class vector {
public:
class iterator {
/* How you implement a iterator of type T */
};

iterator begin(); // for example

// other fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// whatever
}


If you want to not define the iterator inside, this may do:



template <class T>
class vector {
public:
class iterator; // only a declaration

iterator begin(); // same

// other fantastic stuff
};

template <class T>
class vector<T>::iterator {
// the actual definition goes here
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// same
}


Alternatively, you can also define iterator separately
and make vector::iterator an alias:



template <class T>
class iterator { /* real definition */ };

template <class T>
class vector {
public:
using iterator = ::iterator<T>;
// or typedef ::iterator<T> iterator;

iterator begin(); // still the same

// same fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// all the same
}


Hope this helps.






share|improve this answer
























  • Thank you. I was about to bundle my comments into an answer (because why the <expletive deleted> were they comments?) but it looks like you've just saved me the trouble. I'm going to proofread and then you've probably got an upvote.

    – user4581301
    Jan 19 at 4:36











  • Thank you. The upvote is <meaningless praise word deleted> good! xD

    – L. F.
    Jan 19 at 4:37











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%2f54263834%2fsyntax-for-function-return-type-in-a-header-file-for-a-nested-template-class-ret%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














You can simply move the definition of iterator into vector:



template <class T>
class vector {
public:
class iterator {
/* How you implement a iterator of type T */
};

iterator begin(); // for example

// other fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// whatever
}


If you want to not define the iterator inside, this may do:



template <class T>
class vector {
public:
class iterator; // only a declaration

iterator begin(); // same

// other fantastic stuff
};

template <class T>
class vector<T>::iterator {
// the actual definition goes here
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// same
}


Alternatively, you can also define iterator separately
and make vector::iterator an alias:



template <class T>
class iterator { /* real definition */ };

template <class T>
class vector {
public:
using iterator = ::iterator<T>;
// or typedef ::iterator<T> iterator;

iterator begin(); // still the same

// same fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// all the same
}


Hope this helps.






share|improve this answer
























  • Thank you. I was about to bundle my comments into an answer (because why the <expletive deleted> were they comments?) but it looks like you've just saved me the trouble. I'm going to proofread and then you've probably got an upvote.

    – user4581301
    Jan 19 at 4:36











  • Thank you. The upvote is <meaningless praise word deleted> good! xD

    – L. F.
    Jan 19 at 4:37
















3














You can simply move the definition of iterator into vector:



template <class T>
class vector {
public:
class iterator {
/* How you implement a iterator of type T */
};

iterator begin(); // for example

// other fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// whatever
}


If you want to not define the iterator inside, this may do:



template <class T>
class vector {
public:
class iterator; // only a declaration

iterator begin(); // same

// other fantastic stuff
};

template <class T>
class vector<T>::iterator {
// the actual definition goes here
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// same
}


Alternatively, you can also define iterator separately
and make vector::iterator an alias:



template <class T>
class iterator { /* real definition */ };

template <class T>
class vector {
public:
using iterator = ::iterator<T>;
// or typedef ::iterator<T> iterator;

iterator begin(); // still the same

// same fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// all the same
}


Hope this helps.






share|improve this answer
























  • Thank you. I was about to bundle my comments into an answer (because why the <expletive deleted> were they comments?) but it looks like you've just saved me the trouble. I'm going to proofread and then you've probably got an upvote.

    – user4581301
    Jan 19 at 4:36











  • Thank you. The upvote is <meaningless praise word deleted> good! xD

    – L. F.
    Jan 19 at 4:37














3












3








3







You can simply move the definition of iterator into vector:



template <class T>
class vector {
public:
class iterator {
/* How you implement a iterator of type T */
};

iterator begin(); // for example

// other fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// whatever
}


If you want to not define the iterator inside, this may do:



template <class T>
class vector {
public:
class iterator; // only a declaration

iterator begin(); // same

// other fantastic stuff
};

template <class T>
class vector<T>::iterator {
// the actual definition goes here
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// same
}


Alternatively, you can also define iterator separately
and make vector::iterator an alias:



template <class T>
class iterator { /* real definition */ };

template <class T>
class vector {
public:
using iterator = ::iterator<T>;
// or typedef ::iterator<T> iterator;

iterator begin(); // still the same

// same fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// all the same
}


Hope this helps.






share|improve this answer













You can simply move the definition of iterator into vector:



template <class T>
class vector {
public:
class iterator {
/* How you implement a iterator of type T */
};

iterator begin(); // for example

// other fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// whatever
}


If you want to not define the iterator inside, this may do:



template <class T>
class vector {
public:
class iterator; // only a declaration

iterator begin(); // same

// other fantastic stuff
};

template <class T>
class vector<T>::iterator {
// the actual definition goes here
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// same
}


Alternatively, you can also define iterator separately
and make vector::iterator an alias:



template <class T>
class iterator { /* real definition */ };

template <class T>
class vector {
public:
using iterator = ::iterator<T>;
// or typedef ::iterator<T> iterator;

iterator begin(); // still the same

// same fantastic stuff
};

template <class T>
vector<T>::iterator vector<T>::begin()
{
// all the same
}


Hope this helps.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 19 at 4:34









L. F.L. F.

6219




6219













  • Thank you. I was about to bundle my comments into an answer (because why the <expletive deleted> were they comments?) but it looks like you've just saved me the trouble. I'm going to proofread and then you've probably got an upvote.

    – user4581301
    Jan 19 at 4:36











  • Thank you. The upvote is <meaningless praise word deleted> good! xD

    – L. F.
    Jan 19 at 4:37



















  • Thank you. I was about to bundle my comments into an answer (because why the <expletive deleted> were they comments?) but it looks like you've just saved me the trouble. I'm going to proofread and then you've probably got an upvote.

    – user4581301
    Jan 19 at 4:36











  • Thank you. The upvote is <meaningless praise word deleted> good! xD

    – L. F.
    Jan 19 at 4:37

















Thank you. I was about to bundle my comments into an answer (because why the <expletive deleted> were they comments?) but it looks like you've just saved me the trouble. I'm going to proofread and then you've probably got an upvote.

– user4581301
Jan 19 at 4:36





Thank you. I was about to bundle my comments into an answer (because why the <expletive deleted> were they comments?) but it looks like you've just saved me the trouble. I'm going to proofread and then you've probably got an upvote.

– user4581301
Jan 19 at 4:36













Thank you. The upvote is <meaningless praise word deleted> good! xD

– L. F.
Jan 19 at 4:37





Thank you. The upvote is <meaningless praise word deleted> good! xD

– L. F.
Jan 19 at 4:37


















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%2f54263834%2fsyntax-for-function-return-type-in-a-header-file-for-a-nested-template-class-ret%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

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre