Template class problem

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 827 Views - Last Post: 03 July 2013 - 07:16 PM Rate Topic: -----

#1 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Template class problem

Posted 29 June 2013 - 12:30 AM

Hello, I have a problem with this block of code. Can someone please help me fix it. Thanks

template <typename T>
void print_set(set<T> s)
{
	set<T>::iterator iter;
	for(iter = s.begin(); iter != s.end(); ++iter)
	{
		cout << *iter << " ";
	}
	cout << endl;		
}



It will not compile.

This post has been edited by salazar: 29 June 2013 - 12:32 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Template class problem

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3717
  • View blog
  • Posts: 5,981
  • Joined: 08-June 10

Re: Template class problem

Posted 29 June 2013 - 03:28 AM

View Postsalazar, on 29 June 2013 - 07:30 AM, said:

It will not compile.

And is it for some reason refusing to give you an error message to explain that?
Was This Post Helpful? 2
  • +
  • -

#3 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Template class problem

Posted 29 June 2013 - 12:55 PM

The error message in this case is pretty irrelevant. All I know is that it has something to do with the template parameter. I want someone that knows what the problems is to tell mw whether what I can do to fix it. When I replace the template param T with string it compiles. However, with T the compiler gives me some errors. If you need me to post the errors, I will, but if you know your stuff, this shouldn't be a problem identify what is wrong.

I will vote up whoever can solve this problem.
Was This Post Helpful? -5
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Template class problem

Posted 29 June 2013 - 01:41 PM

*
POPULAR

Quote

The error message in this case is pretty irrelevant.

An error message is never irrelevant. They always tell you what your problem is, the problem is you probably don't understand this message. When you get an error message post the complete error messages, exactly as they appear in your development environment.

Also when dealing with templates it is better to post the smallest complete program that illustrates your problems.

By the way your snotty, demanding attitude will, in general, lead to your posts being ignored. We are not your employees or slaves so leave your condescending attitude at home.


Jim

This post has been edited by jimblumberg: 29 June 2013 - 01:41 PM

Was This Post Helpful? 8
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,799
  • Joined: 27-December 08

Re: Template class problem

Posted 29 June 2013 - 04:38 PM

Quote

If you need me to post the errors, I will, but if you know your stuff, this shouldn't be a problem identify what is wrong.

Posting an error message is helpful because it tells us where to look rather than wasting our time trying to figure it out.
Was This Post Helpful? 1
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Template class problem

Posted 29 June 2013 - 08:51 PM

Ok ...

now go ahead ...

but only after carefully reading the above and taking it to heart ...

your new wisely written appeal for help from volunteers here at DIC ...

may result with ... 'your eyes opened some more' ...

to help you 'to see more' of what 'you are still missing'?

But firstly, why not try to figure out by yourself, what is wrong ... using the error message clues you were given by your compiler ... and also try to compile on an other compiler with all error messages there also turned on ... it may have a little different message that may 'speak some word you recognize' to get you started on the 'fix up your code syntax' path.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: Template class problem

Posted 29 June 2013 - 08:57 PM

The error messages generated by my compiler are quite useful, they tell me the line where the problem was detected, plus they even tell me how to fix the problem.

So post your error messages so we can help you understand what your compiler is telling you. Learning to decipher these messages is an important part of programming.

Jim
Was This Post Helpful? 2
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3551
  • View blog
  • Posts: 10,997
  • Joined: 05-May 12

Re: Template class problem

Posted 29 June 2013 - 09:58 PM

As an aside, the your template function compiled just fine for me:
#include <iostream>
#include <set>
#include <string>

using namespace std;

template <typename T>
void print_set(set<T> s)
{
	set<T>::iterator iter;
	for(iter = s.begin(); iter != s.end(); ++iter)
	{
		cout << *iter << " ";
	}
	cout << endl;		
}

int main()
{
    set<int> evens;

    for(int i = 0; i < 10; i++)
        evens.insert(2*(i + 1));

    print_set(evens);

    set<string> names;
    names.insert("Picard");
    names.insert("Riker");
    names.insert("Troi");
    names.insert("Crusher");
    names.insert("Worf");
    
    print_set(names);

    return 0;
}



And outputs:
2 4 6 8 10 12 14 16 18 20
Crusher Picard Riker Troi Worf



As mentioned above, the error messages are relevant because it will point to what class you pass into the template.
Was This Post Helpful? 4
  • +
  • -

#9 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Template class problem

Posted 30 June 2013 - 03:37 PM

level_of_difference.cpp: In function 'void print_set(std::set<T, std::less<_Key>, std::allocato
r<_CharT> >)':
level_of_difference.cpp:26: error: expected ';' before 'iter'
level_of_difference.cpp:27: error: 'iter' was not declared in this scope
level_of_difference.cpp: In function 'void print_set(std::set<T, std::less<_Key>, std::allocato
r<_CharT> >) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]'
:
level_of_difference.cpp:39: instantiated from here
level_of_difference.cpp:26: error: dependent-name 'std::set::iterator' is parsed as a non-type,
but instantiation yields a type
level_of_difference.cpp:26: note: say 'typename std::set::iterator' if a type is meant


these were the errors I received after compiling with g++

#include <iostream>
#include <set>
using namespace std;
set<string> generate_all_substrings(string s)
{
	
	set<string> substrings;
	string substring = "";
	string::iterator iter;
	for(iter = s.begin(); iter != s.end(); ++iter, substring = "")
	{
		for(string::iterator iter_2 = iter; iter_2 != s.end(); ++iter_2)
		{
			substring += *iter_2;
			substrings.insert(substring);	
		}		
		substrings.insert(substring);				
	}
	return substrings;
	
}


void print_set(set<string> s)
{
	set<string>::iterator iter;
	for(iter = s.begin(); iter != s.end(); ++iter)
	{
		cout << *iter << " ";
	}
	cout << endl;		
}


int main() 
{
	string s = "abcd";
	set<string> substrings = generate_all_substrings(s);
	print_set(substrings);
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3551
  • View blog
  • Posts: 10,997
  • Joined: 05-May 12

Re: Template class problem

Posted 30 June 2013 - 03:43 PM

You are using the class string, but you never included the header string.
Was This Post Helpful? 1
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Template class problem

Posted 30 June 2013 - 05:47 PM

If you are using the template as a dependent name, then you must prefix the declartion with the typename keyword. There is a difference between types and non-types when it comes to templates, each of which effects how the template code is expanded and parsed, I suggest you look into it.

template <typename T>
void print_set(set<T> s)
{
   typename set<T>::iterator iter; /* added typename prefix */
   for(iter = s.begin(); iter != s.end(); ++iter)
   {
      cout << *iter << " ";
   }
   cout << endl;		
}



Some compilers make assumptions and some don't, hence why some people were able to compile your code just fine while you could not without error. The standard however, states the following

Quote

Some names denote types or templates. In general, whenever a name is encountered it is necessary to determine whether that name denotes one of these entities before continuing to parse the program that contains it. The process that determines this is called name lookup.


Quote

As an aside, the your template function compiled just fine for me:


What compiler were you using? Microsoft's CL compiler? I receive compiler errors with g++ and (clang++/ llvm)

This post has been edited by jjl: 30 June 2013 - 06:01 PM

Was This Post Helpful? 2
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3551
  • View blog
  • Posts: 10,997
  • Joined: 05-May 12

Re: Template class problem

Posted 30 June 2013 - 07:43 PM

I was using VS2012. Of course, it's well known that VS2012's C++11 compliance is "lacking".
Was This Post Helpful? 0
  • +
  • -

#13 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Template class problem

Posted 02 July 2013 - 09:02 PM

View Postjjl, on 30 June 2013 - 05:47 PM, said:

If you are using the template as a dependent name, then you must prefix the declartion with the typename keyword. There is a difference between types and non-types when it comes to templates, each of which effects how the template code is expanded and parsed, I suggest you look into it.

template <typename T>
void print_set(set<T> s)
{
   typename set<T>::iterator iter; /* added typename prefix */
   for(iter = s.begin(); iter != s.end(); ++iter)
   {
      cout << *iter << " ";
   }
   cout << endl;		
}



Some compilers make assumptions and some don't, hence why some people were able to compile your code just fine while you could not without error. The standard however, states the following

Quote

Some names denote types or templates. In general, whenever a name is encountered it is necessary to determine whether that name denotes one of these entities before continuing to parse the program that contains it. The process that determines this is called name lookup.


Quote

As an aside, the your template function compiled just fine for me:


What compiler were you using? Microsoft's CL compiler? I receive compiler errors with g++ and (clang++/ llvm)



So basically, when you use a typename as an argument to a template you must use a prefix of 'typename'.
Okay, I will definitely look into it; this was incredibly helpful.
Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3551
  • View blog
  • Posts: 10,997
  • Joined: 05-May 12

Re: Template class problem

Posted 02 July 2013 - 09:32 PM

View Postsalazar, on 03 July 2013 - 12:02 AM, said:

So basically, when you use a typename as an argument to a template you must use a prefix of 'typename'.
Okay, I will definitely look into it; this was incredibly helpful.
Thanks for the help.

Not quite. Notice that if you change typename on line 1, to class, you would still run into the same issue without the typename prefix on line 4.
Was This Post Helpful? 1
  • +
  • -

#15 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Template class problem

Posted 02 July 2013 - 11:20 PM

It sill compiles fine using g++. Maybe its the compiler.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2