Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 22nd, 2006, 10:08 AM   #1
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
C++ template and namespace question

First of all, I want to declare that I am relatively new to programming.
Now on to the question:

I am stydying C++ for my exams. So, I am trying to do an exercise concerning templates . You don't need to know the question, since my question is not related to how can I do the exercise but rather a strange thing I want to verify:

My code is this:
#include <iostream>
#include <string>
using namespace std;


template<class T>
void set(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=";
		cin >> p[i];
	}
}

template<class T>
void show(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=" << p[i] << "\n";
	}
}

template<class T>
int greater(T a, T b){return(a>b);}
int greater(char *a, char *b){return strcmp(a,b)>0;}

template<class T>
void swap(T &a, T &b){
	T temp = a;
	a = b;
	b = temp;
}
void swap(char *a, char *b){
	char temp[20];
	strcpy(temp, a);
	strcpy(a,b);
	strcpy(b,temp);
}

template <class T>

void sort(T p[], int size){
	int i,k;
	for(k=1;k<size;k++)
		for(i=0;i<size-k;i++)
			if (greater(p[i],p[i+1])>0){
				swap(p[i],p[i+1]);
			}
}


int main(){
	char names[4][21]; int n=4;
	set(names,n,"names");
	cout<<"\nunsorted names a\n";
	show(names,n,"names");
	sort(names,n); 
	cout<<"\nsorted names\n"; 
	show(names,n,"names");
	int  numbers[4];
	set(numbers,3,"numbers"); 
	cout<<"\nunsorted numbers b\n";
	show(numbers,3,"numbers");
	sort(numbers,3);
	cout<<"\nsorted numbers\n"; 
	show(numbers,3,"numbers");
	
}
And it shows many errors in xCode. The errors have something to do with ambiguous declarations.

I change my code to this:
#include <iostream.h>
#include <string.h>


template<class T>
void set(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=";
		cin >> p[i];
	}
}

template<class T>
void show(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=" << p[i] << "\n";
	}
}

template<class T>
int greater(T a, T b){return(a>b);}
int greater(char *a, char *b){return strcmp(a,b)>0;}

template<class T>
void swap(T &a, T &b){
	T temp = a;
	a = b;
	b = temp;
}
void swap(char *a, char *b){
	char temp[20];
	strcpy(temp, a);
	strcpy(a,b);
	strcpy(b,temp);
}

template <class T>

void sort(T p[], int size){
	int i,k;
	for(k=1;k<size;k++)
		for(i=0;i<size-k;i++)
			if (greater(p[i],p[i+1])>0){
				swap(p[i],p[i+1]);
			}
}


int main(){
	char names[4][21]; int n=4;
	set(names,n,"names");
	cout<<"\nunsorted names a\n";
	show(names,n,"names");
	sort(names,n); 
	cout<<"\nsorted names\n"; 
	show(names,n,"names");
	int  numbers[4];
	set(numbers,3,"numbers"); 
	cout<<"\nunsorted numbers b\n";
	show(numbers,3,"numbers");
	sort(numbers,3);
	cout<<"\nsorted numbers\n"; 
	show(numbers,3,"numbers");
	
}
and it compiles fine. (Note that I have changed the headers and removed the namespace). Lastly, with a little thought, I ended up using this code:
#include <iostream>
#include <string>

using std::cout;
using std::cin;

template<class T>
void set(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=";
		cin >> p[i];
	}
}

template<class T>
void show(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=" << p[i] << "\n";
	}
}

template<class T>
int greater(T a, T b){return(a>b);}
int greater(char *a, char *b){return strcmp(a,b)>0;}

template<class T>
void swap(T &a, T &b){
	T temp = a;
	a = b;
	b = temp;
}
void swap(char *a, char *b){
	char temp[20];
	strcpy(temp, a);
	strcpy(a,b);
	strcpy(b,temp);
}

template <class T>

void sort(T p[], int size){
	int i,k;
	for(k=1;k<size;k++)
		for(i=0;i<size-k;i++)
			if (greater(p[i],p[i+1])>0){
				swap(p[i],p[i+1]);
			}
}


int main(){
	char names[4][21]; int n=4;
	set(names,n,"names");
	cout<<"\nunsorted names a\n";
	show(names,n,"names");
	sort(names,n); 
	cout<<"\nsorted names\n"; 
	show(names,n,"names");
	int  numbers[4];
	set(numbers,3,"numbers"); 
	cout<<"\nunsorted numbers b\n";
	show(numbers,3,"numbers");
	sort(numbers,3);
	cout<<"\nsorted numbers\n"; 
	show(numbers,3,"numbers");
	
}
(note that I included the specific statements to be used from the std namespace).

So i figured out it is a namespace problem. What are the commands that conflict with each other exactly?

Finally, do you know of any good reference to the commands each available namespace includes? I figured out that if I want to do some serious programming, I should get involved with namespaces and templates a lot, even if my exams don't require that.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Jan 22nd, 2006, 1:43 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 259
Rep Power: 3 Cache is on a distinguished road
From 'stl_functions.h' which is uses the 'std' namespace:

/// One of the @link s20_3_3_comparisons comparison functors@endlink.
  template <class _Tp>
	struct greater : public binary_function<_Tp, _Tp, bool>
	{
	  bool
	  operator()(const _Tp& __x, const _Tp& __y) const
	  { return __x > __y; }
	};
That's conflicting with your 'greater' template function.

You can either scrap the 'using namespace std' - and go for std::string (or whatever), or make your own namespace:

namespace MySpace
{
   template<class T>
   int greater(T a, T b){return(a>b);}
   int greater(char *a, char *b){return strcmp(a,b)>0;}
}

template <class T>
void sort(T p[], int size){
   
	int i,k;
	for(k=1;k<size;k++)
		for(i=0;i<size-k;i++)
			if (MySpace::greater(p[i],p[i+1])>0){
				swap(p[i],p[i+1]);
			}
}

That still leaves: "call of overloaded `swap(int&, int&)' is ambiguous", though. I haven't messed with templates enough (yet) to suggest anything for that.

EDIT: It seems to work when the whole thing is namespaced up:

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

namespace MySpace
{

template<class T>
void set(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=";
		cin >> p[i];
	}
}

template<class T>
void show(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=" << p[i] << "\n";
	}
}


   template<class T>
   int greater(T a, T b){return(a>b);}
   int greater(char *a, char *b){return strcmp(a,b)>0;}


template<class T>
void swap(T &a, T &b){
	T temp = a;
	a = b;
	b = temp;
}
void swap(char *a, char *b){
	char temp[20];
	strcpy(temp, a);
	strcpy(a,b);
	strcpy(b,temp);
}

template <class T>
void sort(T p[], int size){
   
	int i,k;
	for(k=1;k<size;k++)
		for(i=0;i<size-k;i++)
			if (greater(p[i],p[i+1])>0){
				swap(p[i],p[i+1]);
			}
}
}


int main(){
	char names[4][21]; int n=4;
	MySpace::set(names,n,"names");
	cout<<"\nunsorted names a\n";
	MySpace::show(names,n,"names");
	MySpace::sort(names,n); 
	cout<<"\nsorted names\n"; 
	MySpace::show(names,n,"names");
	int  numbers[4];
	MySpace::set(numbers,3,"numbers"); 
	cout<<"\nunsorted numbers b\n";
	MySpace::show(numbers,3,"numbers");
	MySpace::sort(numbers,3);
	cout<<"\nsorted numbers\n"; 
	MySpace::show(numbers,3,"numbers");
	
}

Last edited by Cache; Jan 22nd, 2006 at 1:53 PM.
Cache is offline   Reply With Quote
Old Jan 22nd, 2006, 2:36 PM   #3
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 234
Rep Power: 3 Soulstorm is on a distinguished road
Thanks a lot!

So, I figured right. Because it was an examination exercise, I decided to keep the code as unchanged as I could, so I have written this code:
#include <iostream>
#include <string>

using std::cout;
using std::cin;

template<class T>
void set(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=";
		cin >> p[i];
	}
}

template<class T>
void show(T p[], int num, char *name){
	int i;
	for(i=0; i<num; i++){
		cout << name << "[" << i << "]=" << p[i] << "\n";
	}
}

template<class T>
int greater(T a, T b){return(a>b);}
int greater(char *a, char *b){return strcmp(a,b)>0;}

template<class T>
void swap(T &a, T &b){
	T temp = a;
	a = b;
	b = temp;
}
void swap(char *a, char *b){
	char temp[20];
	strcpy(temp, a);
	strcpy(a,b);
	strcpy(b,temp);
}

template <class T>

void sort(T p[], int size){
	int i,k;
	for(k=1;k<size;k++)
		for(i=0;i<size-k;i++)
			if (greater(p[i],p[i+1])>0){
				swap(p[i],p[i+1]);
			}
}


int main(){
	char names[4][21]; int n=4;
	set(names,n,"names");
	cout<<"\nunsorted names a\n";
	show(names,n,"names");
	sort(names,n); 
	cout<<"\nsorted names\n"; 
	show(names,n,"names");
	int  numbers[4];
	set(numbers,3,"numbers"); 
	cout<<"\nunsorted numbers b\n";
	show(numbers,3,"numbers");
	sort(numbers,3);
	cout<<"\nsorted numbers\n"; 
	show(numbers,3,"numbers");
	
}
Anyway, last question still unanswered, have you got any online references to namespaces? How am I supposed to know which commands confront others in certain namespaces?
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Jan 22nd, 2006, 2:46 PM   #4
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 259
Rep Power: 3 Cache is on a distinguished road
Quote:
Originally Posted by Soulstorm
So, I figured right.
Anyway, last question still unanswered, have you got any online references to namespaces? How am I supposed to know which commands confront others in certain namespaces?
Yup, and I just noticed you never even had the problem I generated lol.

For your last question: anything in the STL (Standard Template Library) in under the std namespace.

http://www.cppreference.com/cppstl.html
Cache is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC