CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2009
    Posts
    8

    Simple Template Class problems

    Below is a very simple Template class. I tested it without the use of templates, and it works fine. I am trying to convert it to a template class.

    #include <iostream>
    #include <vector>
    #include <string>
    #include <cmath>

    using namespace std;

    template <class T>
    class Stack
    {
    private:
    int position;
    int input;
    public:
    Stack();
    void push(int, int, vector<T> &);
    void display(int, vector<T>);
    };
    template <class T>
    Stack<T>::Stack()
    {
    //nothing

    }
    template <class T>
    void Stack<T>:ush(int position, int input, vector<T> &mainStack)
    {
    mainStack[position] = input;
    return;

    }
    template <class T>
    void Stack<T>:isplay(int position, vector<T> mainStack)
    {
    cout <<mainStack[position]<<endl;
    }

    template <class T>
    int main()
    {

    vector<int> values (25);// declares a vector of 25 integers
    values[23] = 8888;
    Stack a;
    a.push(22, 9999, values);
    a.display(22, values);
    a.display(23, values);
    return 0;
    }

    I get 2 errors: Can somebody point me in the right direction?

    1>Linking...
    1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
    1>C:\Users\Cesar\Documents\Visual Studio 2008\Projects\BurrisStuff\DataStructures\Debug\DataStructures.exe : fatal error LNK1120: 1 unresolved externals
    1>Build log was saved at "file://c:\Users\Cesar\Documents\Visual Studio 2008\Projects\BurrisStuff\DataStructures\DataStructures\Debug\BuildLog.htm"
    1>DataStructures - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple Template Class problems

    Obviously, main() can't be a template function.

  3. #3
    Join Date
    Nov 2009
    Posts
    8

    Re: Simple Template Class problems

    Quote Originally Posted by Lindley View Post
    Obviously, main() can't be a template function.
    XD wow, I'm an idiot. It works just fine now. Thanks!

    EDIT: also, this is what I ended up with.

    #include <iostream>
    #include <vector>
    #include <string>
    #include <cmath>

    using namespace std;

    template <class T>
    class Stack
    {
    private:
    int position;
    T input;
    vector<T> mainStack;
    int M;
    public:
    Stack(int);
    void push(int, T);
    void display(int);
    };
    template <class T>
    Stack<T>::Stack(int M)
    {
    mainStack.resize(M);

    }
    template <class T>
    void Stack<T>:ush(int position, T input)
    {
    mainStack[position] = input;
    return;

    }
    template <class T>
    void Stack<T>:isplay(int position)
    {
    cout <<mainStack[position]<<endl;
    }


    int main()
    {


    Stack<int> a(25);
    Stack<char> b(25);
    Stack<string> c(25);
    a.push(22, 9999);
    a.display(22);
    b.push(22, 'a');
    b.display(22);
    c.push(22, "Adam sucks");
    c.display(22);




    return 0;
    }
    Last edited by EmperorCesar; April 7th, 2010 at 06:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured