Problem with Template

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • B. Williams

    #1

    Problem with Template

    I have a problem dealing with class template where I was to write a class
    and after submitting the class, this is the feedback I got back from the
    instructor. I don't really understand it. Can someone please assist me.

    Your constructor needs to do something. It should take in 4 values and pass
    these to the individual set functions. I think if you go on to write main
    you'll see the compiler errors and they should help.

    I did write something for main, but the compiler errors left me with more
    questions.

    #ifndef TryOME

    #define TryOME

    template <class R, class S, class V>

    class TryOMe{

    public:

    TryOMe(S s , V v1 , V v2 , R r );

    void setvar1(S s) { var1 = s };

    void setvar2(V v1) {var2 = v1};

    void setvar4(V v2) {var4 = v3};

    void setvar3(R r) { var3 = r};


    S getvar1(){ return var1};

    V getvar2(){ return var2};

    V getvar4(){ return var4};

    R getvar3(){ return var3};


    private:

    S var1;

    V var2;

    V var4;

    R var3;

    };

    #endif

    #include <iostream>

    #include <string>

    using std::endl;

    using std::string;

    #include "TryOMe.h"



    int main()

    {

    TryOMe< char, int, double test('can', 1, 48, 11.1);

    }

    This is my error message.

    TryOMe.obj : error LNK2019: unresolved external symbol "public: __thiscall
    TryOMe<char,int ,double>::TryOM e<char,int,doub le>(int,double, double,char)"

    (??0?$TryOMe@DH N@@QAE@HNND@Z) referenced in function _main



  • Victor Bazarov

    #2
    Re: Problem with Template

    B. Williams wrote:
    I have a problem dealing with class template where I was to write a
    class and after submitting the class, this is the feedback I got back
    from the instructor. I don't really understand it. Can someone please
    assist me.
    What exactly don't you understand? Your constructor doesn't exist,
    essentially. You need to give it a BODY.
    >
    Your constructor needs to do something. It should take in 4 values
    and pass these to the individual set functions. I think if you go on
    to write main you'll see the compiler errors and they should help.
    >
    I did write something for main, but the compiler errors left me with
    more questions.
    >
    #ifndef TryOME
    >
    #define TryOME
    >
    template <class R, class S, class V>
    >
    class TryOMe{
    >
    public:
    >
    TryOMe(S s , V v1 , V v2 , R r );
    ^^^^^^^^^^^^
    Here you declare the constructor. Where is it *defined*?
    >
    void setvar1(S s) { var1 = s };
    >
    void setvar2(V v1) {var2 = v1};
    >
    void setvar4(V v2) {var4 = v3};
    >
    void setvar3(R r) { var3 = r};
    >
    >
    S getvar1(){ return var1};
    >
    V getvar2(){ return var2};
    >
    V getvar4(){ return var4};
    >
    R getvar3(){ return var3};
    >
    >
    private:
    >
    S var1;
    >
    V var2;
    >
    V var4;
    >
    R var3;
    >
    };
    >
    #endif
    >
    #include <iostream>
    >
    #include <string>
    >
    using std::endl;
    >
    using std::string;
    >
    #include "TryOMe.h"
    >
    >
    >
    int main()
    >
    {
    >
    TryOMe< char, int, double test('can', 1, 48, 11.1);
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^
    Here you *use* the constructor.
    >
    }
    >
    This is my error message.
    >
    TryOMe.obj : error LNK2019: unresolved external symbol "public:
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^
    The linker needs to know where to find the constructor.
    __thiscall
    TryOMe<char,int ,double>::TryOM e<char,int,doub le>(int,double, double,char)"
    >
    (??0?$TryOMe@DH N@@QAE@HNND@Z) referenced in function _main
    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Pete Becker

      #3
      Re: Problem with Template

      B. Williams wrote:
      I have a problem dealing with class template where I was to write a class
      and after submitting the class, this is the feedback I got back from the
      instructor. I don't really understand it. Can someone please assist me.
      >
      Your constructor needs to do something. It should take in 4 values and pass
      these to the individual set functions. I think if you go on to write main
      you'll see the compiler errors and they should help.
      >
      I did write something for main, but the compiler errors left me with more
      questions.
      >
      When template code produces mysterious error messages, try the same
      thing without the templates. Make TryOMe an ordinary class, and replace
      S, V, and R throughout it with ordinary types. I generally use int
      unless there's a good reason for something else.

      That's a hint: the problem has nothing to do with templates. They're
      just a distraction here. Learn your tools, so that you understand basic
      error messages (like the one you got) before you do advanced things like
      writing templates. (Yes, I know, your instructor wants templates. That's
      unfortunate. Too many people try to write templates too soon.)

      --

      -- Pete

      Author of "The Standard C++ Library Extensions: a Tutorial and
      Reference." For more information about this book, see
      www.petebecker.com/tr1book.

      Comment

      • David Harmon

        #4
        Re: Problem with Template

        On Fri, 13 Oct 2006 17:26:25 -0400 in comp.lang.c++, "B. Williams" <willdrama@hotm ail.comwrote,
        >I have a problem dealing with class template where I was to write a class
        >and after submitting the class, this is the feedback I got back from the
        >instructor. I don't really understand it. Can someone please assist me.
        >
        >Your constructor needs to do something. It should take in 4 values and pass
        >these to the individual set functions. I think if you go on to write main
        >you'll see the compiler errors and they should help.
        This issue is covered in Marshall Cline's C++ FAQ. See the topic
        "[10.6] Should my constructors use "initializa tion lists" or "assignment "?" It is always good to check the FAQ before posting.
        You can get the FAQ at:


        Comment

        • Salt_Peter

          #5
          Re: Problem with Template

          B. Williams wrote:
          I have a problem dealing with class template where I was to write a class
          and after submitting the class, this is the feedback I got back from the
          instructor. I don't really understand it. Can someone please assist me.
          >
          Your constructor needs to do something. It should take in 4 values and pass
          these to the individual set functions. I think if you go on to write main
          you'll see the compiler errors and they should help.
          Your instructor gave you excellent feedback, the error message is
          essentially saying that it sees a signature declaration but its unable
          to resolve its implementation. Ignore that error at your own peril.
          This is my error message.
          >
          TryOMe.obj : error LNK2019: unresolved external symbol "public: __thiscall
          TryOMe<char,int ,double>::TryOM e<char,int,doub le>(int,double, double,char)"
          >
          (??0?$TryOMe@DH N@@QAE@HNND@Z) referenced in function _main
          Why don't you try a simple example and test the theory:

          // --- A.h ---
          #ifndef A_H_
          #define A_H_

          template< typename T >
          class A
          {
          T t;
          public:
          A(T);
          };

          template< typename T >
          A< T >::A(T val) : t(val)
          {
          }

          #endif /* A_H_ */

          // --- test.cpp ---
          #include "A.h"
          int main()
          {
          A<inta(99);
          }

          Now comment out the ctor implementation and try compiling. Note the
          error.
          That compiler is talking to you. Its helping you code. In fact: thats
          its job.
          Uncomment ctor's body... no error generated since its now got
          implementation to execute.

          Add a void get() const to the class declaration, try compiling - no
          error generated !!!
          What the hell? That, by the way, is a common way to disable a copy
          ctor, for example.
          ie: the compiler only generates an error if the call/invocation fails
          to find a function body.

          now add a call to get() in main().

          int main
          {
          A<inta(99);
          a.get();
          }

          try compiling again: error !!!
          Its telling you exactly what is missing unambiguously. Right down to
          the symbol involved.
          On mine it says: test.cpp:19: undefined reference to `A<int>::get()
          const

          now define get() as follows:
          template< typename T >
          T A< T >::A::get() const
          {
          return t;
          }

          Will you recognise now that the compiler's output is actually VERY
          useful?
          Ignoring its output is like walking around blindfolded.

          Comment

          • B. Williams

            #6
            Re: Problem with Template


            "Victor Bazarov" <v.Abazarov@com Acast.netwrote in message
            news:egp228$eju $1@news.datemas .de...
            B. Williams wrote:
            >I have a problem dealing with class template where I was to write a
            >class and after submitting the class, this is the feedback I got back
            >from the instructor. I don't really understand it. Can someone please
            >assist me.
            >
            What exactly don't you understand? Your constructor doesn't exist,
            essentially. You need to give it a BODY.
            >
            >>
            >Your constructor needs to do something. It should take in 4 values
            >and pass these to the individual set functions. I think if you go on
            >to write main you'll see the compiler errors and they should help.
            >>
            >I did write something for main, but the compiler errors left me with
            >more questions.
            >>
            >#ifndef TryOME
            >>
            >#define TryOME
            >>
            >template <class R, class S, class V>
            >>
            >class TryOMe{
            >>
            >public:
            >>
            >TryOMe(S s , V v1 , V v2 , R r );
            ^^^^^^^^^^^^
            Here you declare the constructor. Where is it *defined*?
            >
            >>
            >void setvar1(S s) { var1 = s };
            >>
            >void setvar2(V v1) {var2 = v1};
            >>
            >void setvar4(V v2) {var4 = v3};
            >>
            >void setvar3(R r) { var3 = r};
            >>
            >>
            >S getvar1(){ return var1};
            >>
            >V getvar2(){ return var2};
            >>
            >V getvar4(){ return var4};
            >>
            >R getvar3(){ return var3};
            >>
            >>
            >private:
            >>
            >S var1;
            >>
            >V var2;
            >>
            >V var4;
            >>
            >R var3;
            >>
            >};
            >>
            >#endif
            >>
            >#include <iostream>
            >>
            >#include <string>
            >>
            >using std::endl;
            >>
            >using std::string;
            >>
            >#include "TryOMe.h"
            >>
            >>
            >>
            >int main()
            >>
            >{
            >>
            >TryOMe< char, int, double test('can', 1, 48, 11.1);
            ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^
            Here you *use* the constructor.
            >
            >>
            >}
            >>
            >This is my error message.
            >>
            >TryOMe.obj : error LNK2019: unresolved external symbol "public:
            ^^^^^^^^^^^^^^^ ^^^^^^^^^^^
            The linker needs to know where to find the constructor.
            >
            >__thiscall
            >TryOMe<char,in t,double>::TryO Me<char,int,dou ble>(int,double ,double,char)"
            >>
            >(??0?$TryOMe@D HN@@QAE@HNND@Z) referenced in function _main
            >
            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask
            I have attempted to define the constructor, but I'm not sure I'm doing it
            correctly because of the multiple parameters. I have an example of defining
            the constructor with just one parameter. This is my attempt. Is this even
            remotely correct?

            template< class R, class S, class V>

            TryOMe< R, S, V >::TryOMe(S, V, V, R)

            :s(var1), v1(var2), v2(var4), r(var3)

            {

            }


            Comment

            • Victor Bazarov

              #7
              Re: Problem with Template

              B. Williams wrote:
              I have attempted to define the constructor, but I'm not sure I'm
              doing it correctly because of the multiple parameters. I have an
              example of defining the constructor with just one parameter. This is
              my attempt. Is this even remotely correct?
              >
              template< class R, class S, class V>
              >
              TryOMe< R, S, V >::TryOMe(S, V, V, R)
              >
              >s(var1), v1(var2), v2(var4), r(var3)
              >
              {
              >
              }
              Remotely, maybe. 'var1' through 'var4' are members, right? They
              probably need to be _outside_ the parentheses in the initialiser
              list. And you need to define 's', 'v1', 'v2', 'r'. Aren't those
              _arguments_? How come your argument list doesn't have any real
              arguments, only types?

              V
              --
              Please remove capital 'A's when replying by e-mail
              I do not respond to top-posted replies, please don't ask


              Comment

              Working...