Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I took a quick look at the C++ documentation on templates and classes. I'm not entirely sure if I'm using the features(read: syntax) properly, but I get the concepts.

I have to admit that scope is a bit confusing, but I'll just let you take a look at the code and tell me how wrong I am. (It's obviously not finished)

#include <iostream>

namespace myscope
{
    template <class T>
    class list {
        private:
            struct node_t {
                T element;
                int index;
                struct node_t * next;
                struct node_t * prev;
            };

            struct node_t * head;
            struct node_t * tail;

            list() : head(new node_t()) , tail(new node_t()) {}
            ~list(){}
        public:
            struct node_t * newnode(T element);
            struct node_t * delnode(int index);

            struct node_t * search_f(T element);
            struct node_t * search_r(T element);

    };

    template <class T>
    struct node_t * newnode(T element){

        return NULL;
    }

    struct node_t * delnode(int index){

        return NULL;
    }
}

using namespace std;

int main(void){

    return 0;
}
share|improve this question
    
Note quite. But unfortunately this is off-topic. You should ask on stackoverflow.com when it comes to question about correct syntax. –  Loki Astari Feb 19 '13 at 22:41
    
Darn! I thought since it was a piece of code that I was asking about in particular it was to go here. (I already made the mistake of putting a codereview item on stackoverflow.) Thanks. –  GRAYgoose124 Feb 19 '13 at 23:04
    
@GRAYgoose124 It might help to read the FAQ. –  svick Feb 20 '13 at 0:10
add comment

closed as off topic by Loki Astari, Jeff Vanzella, svick, palacsint, mnhg Feb 20 '13 at 9:47

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 3 down vote accepted

When you define a method outside the class declaration:

    template <class T>
    /*struct*/ list<T>::node_t * list<T>::newnode(T element){
//  ^^^^^^^^^^ Don't need struct here.
//             ^^^^^^^  node_t is defined inside list<T>
//                               ^^^^^^^  newnode() is a member of list<T>  

        return NULL;
    }

Because it is a template class and you define the function outside the declaration you need to make sure you use list<T>. Inside you would just use list.

Don't do this: http://stackoverflow.com/q/1452721/14065

using namespace std;

The function main() is special so you don't need return 0;
Personally I only return a value from main() when there is a possibility of error (when an error code is returned to the shell). If this never happens don't return anything as an indication that the application does not have an error state.

share|improve this answer
    
Thanks! So, other than those functions outside the class declaration it seems fine? I knew about list<T>::, but I saw somewhere that it was okay to leave it off(It's just lazy I suppose). Anyways, I won't use using namespace std; it gives me many more options for function names. Thanks for the answer. –  GRAYgoose124 Feb 19 '13 at 23:09
    
Sorry Loki, could you take a look at this: codebin.org/view/b91e02a2 It's the revised version after I followed your rules, but I wasn't sure about the second function that didn't use the template. If I put list::, I get a no argument list error. list<>:: is obviously an invalid number of arguments and list<T>:: gives me a declaration error. –  GRAYgoose124 Feb 19 '13 at 23:38
1  
Both methods are part of the template class list. So before each you need template<class T>. Just copy the newnode code and rename the method: codebin.org/view/b7449273 –  Loki Astari Feb 20 '13 at 0:20
    
Thank you Loki. I appreciate it. –  GRAYgoose124 Feb 20 '13 at 1:52
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.