Problem with Template

B

B. Williams

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>(int,double,double,char)"

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

Victor Bazarov

B. Williams said:
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
 
P

Pete Becker

B. Williams said:
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.
 
D

David Harmon

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/
 
S

Salt_Peter

B. Williams said:
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>(int,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<int> a(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<int> a(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.
 
B

B. Williams

Victor Bazarov said:
What exactly don't you understand? Your constructor doesn't exist,
essentially. You need to give it a BODY.

^^^^^^^^^^^^
Here you declare the constructor. Where is it *defined*?

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Here you *use* the constructor.

^^^^^^^^^^^^^^^^^^^^^^^^^^
The linker needs to know where to find the constructor.


V
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)

{

}
 
V

Victor Bazarov

B. Williams said:
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>



{

}

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top