1

Hello for all people...

Sorry for my english, but speak spanish...

In this week, study and work for this proyect, I want create a software to make files(.us)...

Example

char name[50]; //Or string
cin>>name;

ofstream PlayerPawn("D:\\UDK\\UDK_XXX\\Development\\Src\\" + name+"\\Classes\\_PlayerPawn.us");

But the compiler has error in the Operator binary plus

Any alternative, examples or something for create the file in specific directory

Good bye and Thx!

Warkanlock
  • 156
  • 1
  • 1
  • 11

2 Answers2

3

I believe you want name to be a std::string - otherwise, name + [suffix] will try to add the suffix string to the array and will not compile. If you really want to keep the name as an array, you should use strcat to append the strings together.

Matt
  • 10,434
  • 1
  • 36
  • 45
  • But, to change de Variable to String and has same error! `D:\Users\Warkanlock\Documents\Code Blocks\UDK_ProyectSimple\main.cpp|20|error: no matching function for call to 'std::basic_ofstream >::basic_ofstream(std::basic_string, std::allocator >)'|` – Warkanlock Feb 27 '12 at 19:33
  • Well, that's not the same error - you need to `#include ` to define what an "ofstream" is – Matt Feb 27 '12 at 19:38
  • Ya, this include that include in this file :o – Warkanlock Feb 27 '12 at 19:40
  • See my comment at the tail of @Seth Carnegie's answer – Matt Feb 27 '12 at 19:52
3

Either side of operator+ must be a std::string1 for operator+ to concatenate strings:

string name;
cin >> name;

ofstream PlayerPawn("D:\\UDK\\UDK_XXX\\Development\\Src\\" + name + "\\Classes\\_PlayerPawn.us");

And use std::string for this stuff; with std::string there's no danger of buffer overflows that you get with char*.


1 Actually it just needs to be a class type that supports operator+, not specifically std::string, but then you have no idea what it will do.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • But, change the variable to std::string but has this error... `D:\Users\Warkanlock\Documents\Code Blocks\UDK_ProyectSimple\main.cpp|20|error: no matching function for call to 'std::basic_ofstream >::basic_ofstream(std::basic_string, std::allocator >)'|` – Warkanlock Feb 27 '12 at 19:30
  • @Warkanlock make sure you put `#include ` at the top of your code. – Seth Carnegie Feb 27 '12 at 19:31
  • @Warkanlock your error is coming from somewhere else where you are using an `std::ofstream`. You'll need to paste the rest of your code. – Seth Carnegie Feb 27 '12 at 19:38
  • @Warkanlock the constructor for `ofstream` takes a `const char*`, not a `string`. Put the `"..." + nombre + "..."` in parentheses, then after the end of the parentheses call `c_str` on the string. Like this: `ofstream PlayerPawn(("D:\\UDK\\UDK_XXX\\Development\\Src\\" + nombre + "\\Classes\\_PlayerPawn.us").c_str());` – Seth Carnegie Feb 27 '12 at 19:45
  • Here is another [Stack Overflow question](http://stackoverflow.com/questions/32332/why-dont-the-stdfstream-classes-take-a-stdstring) about this oddity. Good catch Seth. – Matt Feb 27 '12 at 19:51
  • @Matt [Actually I asked the exact same question](http://stackoverflow.com/questions/5972151/why-does-iofstream-take-a-const-char-parameter-for-a-file-name) a long time ago too :) – Seth Carnegie Feb 27 '12 at 19:52
  • Thx! men! I REALY WORK THIS!! Ps: When finish make this software, i send the exe for MP hhaha Thx realy! – Warkanlock Feb 28 '12 at 02:08