Strings in Visual Studio 2008

If you get this error:

error C2784: ‘std:: _String_iterator <_Elem,_Traits,_Alloc> std::operator + (_String_iterator <_Elem,_Traits,_Alloc>::difference_type , std:: _String_iterator <_Elem,_Traits,_Alloc>) “:template argument for ‘std:: _String_iterator <_Elem,_Traits,_Alloc>”could not be derived from’ const char [5].

Then you have to add #include <string> to the header. Linux doesn’t require it, but Windows needs it (especially Visual Studio).

Wtf is a for each loop?

I only learned this a couple months ago. Its actually very simple. Why they didn’t teach this in school, I don’t know. Maybe its more of a java thing than a C++ thing. In any case, heres how you use it.

Simple example, lets add up all of the ints in this array. Normally I would use a regular for loop

int[] intArray = new int[10];
int total;
for (int i = 0; i < 10; i++){
total += intArray[i];

Instead, lets use a for each. Basically, this means “For each int in the intArray…”

for (int tmpInt : intArray){
total += tmpInt;

This work for everything, like Strings, or even objects. It is most commonly used in lists (from what I’ve seen)

ArrayList<String> stringList = new ArrayList<String>();
for (String tmpString : stringList){
// do something with the string

Have questions on wtf a for each loop is? Leave a comment, I’ll try to answer! Have a better explanation of a for each loop? Leave a comment. This is what I came up with off the top of my head. Chances are theres a better way of doing it.

Writing and Deleting on the SD card, simplified

This should be something easy and intuitive. The internet makes this not so. Here you go, functions for writing and deleting on the SD card. Copy and paste these right into your program. Do you want to clutter up your users SD card with unnecessary files? Do you want to delete all files on your users SD cards? Do so to your hearts content! (Please don’t though, its messed up.)

// Saving a file to the SD card
public void writeFile(String filename, String data){
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
// This stores the file on the SD card
File locDataForMachines = new File(root, filename + ".txt");
FileWriter machineWriter = new FileWriter(locDataForMachines, true);
BufferedWriter machineOut = new BufferedWriter(machineWriter);
} catch (Exception e) {
// Do whatever you gotta do, maybe use some other exceptions too
// Deleting a file from the SD card, probably used in conjunction with the function above
public void deleteSDFile(String filename){
File deleteMatchingFiles = new File("/sdcard");
if (deleteMatchingFiles != null){
File[] filenames = deleteMatchingFiles.listFiles();
for (File tmpf : filenames){
if (tmpf.getName().startsWith(filename)){ // can change starts with to anything you want to compare with
}catch(Exception e){
// Do whatever you gotta do, maybe use some other exceptions too

Also, add this to your manifest. I hate how people never remind you what to add to the manifest. Its like they want you to fail.

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”>

Post below and let me know how bad you think this implementation is, or let me know how it helped you. Thanks!

addContentView example simplified

Welcome to! You were probably directed here from my portfolio site,, or by some miracle you were looking for some android code on Google and you happened to stumble here. Basically this site is a way I’m contributing back to the internet by sharing the code I’ve created and found to help your android producing experience easier.

Today, for a first post, I will make a simple yet demanded contribution. First, let me type out all of the searches I made before I got close to my answer

addContentView help, addContentView Android, addContentView Code Android, Adding a xml layout onto a canvas android, how to inflate a view with an xml file, multiple layout xml android, etc, etc.

The list goes on, next time I will actually better document what I searched for, so that hopefully those same searches will direct you here! In any case, by reading those searches, maybe you can see what I wanted to do. I want to draw a couple of buttons from an xml layout onto a canvas. This solution will work with drawing any xml layout onto anything else however.

LayoutInflater inflater = getLayoutInflater();
View tmpView;
tmpView = inflater.inflate(R.layout.extra_layout, null);
getWindow().addContentView(tmpView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,

Alternatively, if you want the code to be a little slimmer, just put the inflater return directly into the addcontent function, as so

LayoutInflater inflater = getLayoutInflater();
getWindow().addContentView(inflater.inflate(R.layout.extra_layout, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,

This code goes into your onCreate(Bundle whateever), preferably right underneath where you just called “setContentView(R.layout.main);”

If you found this helpful, leave a comment! I make no claim that this is the best implementation for this idea, but it works!

Buttons xml overlay


-Kevin Grant