By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,595 Members | 3,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,595 IT Pros & Developers. It's quick & easy.

Problem with Template

P: n/a
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>::TryOMe<char,int,double>(i nt,double,double,char)"

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

Oct 13 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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>::TryOMe<char,int,double>(i nt,double,double,char)"

(??0?$TryOMe@DHN@@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
Oct 13 '06 #2

P: n/a
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.
Oct 13 '06 #3

P: n/a
On Fri, 13 Oct 2006 17:26:25 -0400 in comp.lang.c++, "B. Williams" <wi*******@hotmail.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 "initialization lists" or "assignment"?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Oct 14 '06 #4

P: n/a
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>::TryOMe<char,int,double>(i nt,double,double,char)"

(??0?$TryOMe@DHN@@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.

Oct 14 '06 #5

P: n/a

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:eg**********@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,int,double>::TryOMe<char,int,double>( int,double,double,char)"

(??0?$TryOMe@DHN@@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)

{

}
Oct 14 '06 #6

P: n/a
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
Oct 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.