I created a template function called add like this template(class T) T add(T a, T b) { return a + b; } now this will work for number types like int float, and string , but will not work for char . how can i say if ( T is char ) or (a is char) (b is char ) then concatnate(a,b)
It is not working for char, so i want to modify this template function when it is a type of char then it will join(a,b) . Like add('h','i') should give hi .
dawei, thank you very very much. Can you please tell me what is the c or c++ function to join to char . For example in above example i want to join a and b char.
hello dawei, there is a problem the defination of strcat is Code: strcat(int * a, int *b) it will only take inside pointers ,so if you want it to work with add it will be Code: #include<iostream> #include<cstring> char *add(int *a, int *b) { strcat(a,b); return *a; } int main() { char a[] = "hi"; char b[] = "world"; cout<<endl<<add(a,b); return 0; } OUTPUT: hiworld but with error and terminated
Son, you're in a world of hurt. You really need to get some decent documentation. The declaration of strcat is as follows: char * strcat ( char * destination, const char * source ); You are also writing C code in your C++. Why the hell would you do that? Although the C string functions will work, they have been deprecated. Your compiler is free to quit supporting them at any time. C++ has a string class. It does all that you need. It even overloads the '+' operator and uses it for concatenation. Further, it grows as needed, so you don't have to worry about overflow. If you are just messing around in an attempt to learn, that's fine. Write your own string class. Overload the "=", "==", "+", and "[]" operators. You'll learn a lot. Here is some functioning code that illustrates both approaches. Code: #include <iostream> #include <string> #include <cstring> using std::cout; using std::endl; using std::string; int main() { // The following is actually C code. // Why write C code in C++ ???? char first [] = "Hello"; // In most modern systems, these will be write protected char second [] = "Bubba"; // In other words, you can't use them as a destination char destination [16]; // This must be large enough to hold the result strcpy (destination, first); strcat (destination, ", "); strcat (destination, second); // This is C++ code cout << first << "\n" << second << "\n" << destination << endl; string newDestination = first; newDestination += ", "; newDestination += second; cout << "\n" << newDestination << endl; return 0; } Here is the output:
I understand now that it's better to use strings. I will try to convert it string and then concatenate. The problem i had is can we create something like char[] add(char a[],char b[]) or can we convert string to char array . The reason i am asking is because i have old projects containing char arrays , i sometimes need to concatenate them. Thank you very much for your help .
Well, as you can see, my program concatenated char arrays to both a char array and a string. You can get a char array from the string by using the .c_str () method.
I am sorry i tried doing that but here is the code Code: #include<iostream> #include<string> using namespace std; const char add(char a[], char b[]) { string str = a; str+= b; const char *c_str1 = str.c_str ( ); return *c_str1; } int main() { char a[] = "hi"; char b[] = "world"; cout<<endl<<add(a,b); return 0; } OUTPUT: h you see output gives h when i want "hi world ",please help how should i change the function.
You have defined your add function to return a single char. You are then returning the dereferenced value of the string. The first byte of that string is a single char, precisely as you have asked it to do. At some point you are going to have to realize that your code does as you instruct it to do, not as you wish that it would do in the absence of explicit instructions.
You cannot declare a local variable in a function and return a pointer to it. The variable goes out of scope when the function returns. The pointer then points to junk. You either need to declare the variable outside the function (as I did with "destination"), or you need to dynamically allocate memory for it inside the function. That memory, not being local, will persist. In the latter case, you must remember to free the memory. I suggest you make a class for string manipulation and have the add method be part of that class. Then the destructor would be used to free the memory. You are missing some very basic understanding of the language. You need to study. It will be more time-effective then bopping in and out of a forum for every little thing you do wrong.
Code: #include<iostream> #include<string> using namespace std; int main() { char a[] = "hi "; char b[] = "world"; cout<<endl<<strcat(a,b); return 0; }
JW, your solution corrupts variable a and relies on a specific method of auto-variable generation to prevent it from corrupting other memory locations and causing a crash.