template inheritance and the output operator

W

W. Cerven

I am using the C++ STL container "list" to create a template nested
list. Attached are my basic code and compiler errors. Oddly enough,
I when I create a non-template version of this nested list, I do not
get this problem. Any help someone could provide would be great.

Thanks.

nestedlisttemplate.h:
----------------------------
using namespace std;

#ifndef _nestedlisttemplate_h
#define _nestedlisttemplate_h

#include<iostream>
#include<list>

template<typename Etype>
class NestedListTemplate:public list<Etype>{

public:
NestedListTemplate();
~NestedListTemplate();
friend ostream& operator<<(ostream & Out, NestedListTemplate&
llists);

private:
};
#endif


nestedlisttemplate.C:
-----------------------------
using namespace std;

#include "nestedlisttemplate.h"
#include<iostream>
#include<list>

template<typename Etype>
NestedListTemplate<Etype>::NestedListTemplate(){};

template<typename Etype>
NestedListTemplate<Etype>::~NestedListTemplate(){};

template<typename Etype>
ostream& operator<<(ostream & Out, NestedListTemplate<Etype>& llists){
Out<<"Test Complete \n";
return Out;
}


using namespace std;

#include "nestedlisttemplate.h"
#include<iostream>
#include<list>

int main()
{
cout<<"\nTesting Constructors ...\n";
NestedListTemplate<int> alist;

cout<<"\nTesting output operator ...\n";
cout<<alist<<"\n";

return 0;
};


compiler command and error:
----------------------------------------

CC -c nestedlisttemplate.C
CC test_nestedlist.C nestedlist.o nestedlisttemplate.o -o
test_nestedlist
Undefined first referenced
symbol in file
std::eek:stream &operator<<(std::eek:stream &,NestedListTemplate<int>&)
test_nestedlist.o
ld: fatal: Symbol referencing errors. No output written to
test_nestedlist
 
J

John Harrison

W. Cerven said:
I am using the C++ STL container "list" to create a template nested
list. Attached are my basic code and compiler errors. Oddly enough,
I when I create a non-template version of this nested list, I do not
get this problem. Any help someone could provide would be great.

Well that's the clue. I reckon this is the most commonly asked question on
this group.

ALL template code should go in header files, throw away nestedtemplatelist.C
and put all the code in nestedtemplatelist.h.

See the FAQ

http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.13

john
 
T

Truls Haaland

I am using the C++ STL container "list" to create a template nested
list. Attached are my basic code and compiler errors. Oddly enough,
I when I create a non-template version of this nested list, I do not
get this problem. Any help someone could provide would be great.

Thanks.

nestedlisttemplate.h:
----------------------------
using namespace std;

#ifndef _nestedlisttemplate_h
#define _nestedlisttemplate_h

#include<iostream>
#include<list>
[snip]

Try moving the "using namespace std;" after the includes, or even
better - use the std:: prefix. So that it looks like:

#include <iostream>
#include <list>
using namespace std;

Or the better:
std::eek:stream& ...

T.
 
P

Prateek R Karandikar

nestedlisttemplate.h:

Putting a global using-directive in a header file is a very bad idea.
It defeats the namespace mechanism. Anyone who includes
"nestedlisttemplate.h" will automaticaly get the "using namespace
std;", whether he wants it or not.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
To iterate is human, to recurse divine.
-L. Peter Deutsch
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 

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

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top