Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
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:
Code:
#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:
Code:
#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:
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.
 
kainjow said:
Are you compiling with GCC 4?
yup. But it has the same problems under GCC 3.x

it doesn't matter what compiler I use. I get errors.

anyway this is a namespace problem. Do you know of any good reference on namespaces?
 
"greater" and "swap" are both part of the standard library as is "sort" for that matter but the definitions do not clash.

Don't use "using namespace std"

Any (in fact every) c++ reference should tell you this.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.