C++ template error


 
Thread Tools Search this Thread
Top Forums Programming C++ template error
# 1  
Old 05-13-2012
C++ template error

I get some compiling errors about template instantiation Smilie , but I can't find where the syntax errors happens. Can some help me?

Code:
template<typename Type> class SingleList;

template<typename Type> class SingleListNode{
private:
	friend class SingleList<Type>;

	SingleListNode() : next(NULL){}

public:
	friend ostream& operator<< <Type>(ostream& os,SingleListNode<Type>& sln);                  //Error!!!

private:
	SingleListNode *next;
};

template<typename Type> ostream& operator<<(ostream& os,SingleListNode<Type>& out){
	os<<out.data;
	return os;
}

template<typename Type> class SingleList{
public:
	SingleList()
        {
            head =	new SingleListNode<Type> () ;                 //Error!!!
        }
	~SingleList(){
		delete head;
	}

private:
	SingleListNode<Type> *head;
};

Compiler error message:
Code:
|| g++ -g -I ./inc/ -c single_list_test.cpp  -o single_list_test.o
|| single_list.h: In instantiation of ‘SingleListNode<int>’:
single_list.h|25 col 13| instantiated from ‘SingleList<Type>::SingleList() [with Type = int]’
single_list_test.cpp|9 col 18| instantiated from here
single_list.h|10 col 18| error: template-id ‘operator<< <int>’ for ‘std::ostream& operator<<(std::ostream&, SingleListNode<int>&)’ does not match any template declaration
|| make: 
*** [single_list_test.o] Error 1

# 2  
Old 05-26-2012
To my gcc , just modify the following statement


friend ostream& operator<< <Type>(ostream& os,SingleListNode<Type>& sln)
to

friend ostream& operator<< (ostream& os,SingleListNode<Type>& sln)
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Programming

Calling template at once

Hello Again, I am just wanted to know if we can call the Template using "require_once" at PHP? Any views around happy to discuss. Thanks in Advance (2 Replies)
Discussion started by: AimyThomas
2 Replies

2. Shell Programming and Scripting

Help with template like solution

hi experts, i'm trying to do this: file1 is a template. might have kinds of 'funny' characters. sample: <body> <form> <p><input type="text" name="abc"/></p> &nbsp; <p><my_content></p> </form> </body> file2 is a file that contains lots of text. this might be very big. might have... (2 Replies)
Discussion started by: xjohnu
2 Replies

3. Programming

Template problem ...

Hi all, Need your help. I am doing a simple template program , getting some error ... here is the code #include <iostream> #include <stdio.h> #include <stdlib.h> #include<iostream> #include<string> #include <sstream> using namespace std; class Base_class { public: Base_class(){... (1 Reply)
Discussion started by: amartya_sock
1 Replies

4. UNIX for Dummies Questions & Answers

vi calling template

Hello. I want to copy temp files when I make a new file by vi. For example, 09:32:52 ~/ $ mkdir test 09:33:03 ~/ $ cd test/ 09:33:09 ~/test/ $ ls 09:33:16 ~/test/ $ vi test.cpp 09:34:37 ~/test/ $ cat test.cpp #include <iostream> int main() { } 09:34:48 ~/test/ $ vi test.bash 09:35:19... (1 Reply)
Discussion started by: Euler04
1 Replies

5. Programming

About template constraints

Hi, i have class template, 1)can i override the copy constructor 2)can we have virtual function in class template if not plz tel why? I tried , compile error comes for me... Thanks Sarwan (0 Replies)
Discussion started by: sarwan
0 Replies
Login or Register to Ask a Question