error: invalid conversion from 'char' to 'const char*'

I’m completely new to oF and coding in general. I’m having a hard time figuring out these errors. I’m doing a version of text rain, but allowing real time text input and mapping each letter to a sound.

The errors are only in the draw section in pink below.

If anyone can help me out, I’d greatly appreciate it. If you need the all the code, please let me know and I will post. Thanks so much in advance!


void testApp::draw(){

//for drawing beat colored letters
char tempStr[255];

//------------------draw letters

ofSetColor(0xffffff);
grayDiff.draw(0,0);
//display runtime info for debugging
ofSetColor(0xff0000);
ofDrawBitmapString("fps: "+ofToString(ofGetFrameRate()), 10, 10);
ofDrawBitmapString("threshold: "+ofToString(threshold), 10, 20);
//advance the location of each character of the string
int i;
for (i = 0; i<str.length(); i++) {
int max;
if (arrayFilled) max = 100;
else max = currLetter;
for (int i=0; i < max; i++) {

//New positions
x[i] = priorX;
y[i] = y[i]+5;
if (y[i]>480) {
y[i]=0;
}

//Draw the character, get its outline
// draw in green
ofFill();

theFont.drawStringAsShapes(letters[i],x[i],y[i]);
error: invalid conversion from ‘char’ to ‘const char*’
error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]’

ofRectangle theRect = theFont.getStringBoundingBox(letters[i],x[i],y[i]);
error: invalid conversion from ‘char’ to ‘const char*’
error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]’

ofNoFill();
// if colliding with something, backup you are not colliding.
// must be a better approach here that can still avoid jitter.
while (grayDiff.countNonZeroInRegion(theRect.x,theRect.y,theRect.width,theRect.height)>0) {
ofSetColor(255,0,0); //red
theRect.y = theRect.y -1;
y[i] = y[i] - 1;

// play a sound
if (letters[i] == ‘a’){ // do somehing
beatdistorted1.play();
} else if (letters[i] == ‘b’) {
beatdistorted2.play();
} else if (letters[i] == ‘c’) {
beatdistorted3.play();
} else if (letters[i] == ‘d’) {
beatdistorted4.play();
}
break;

if (y[i]<0) {
y[i]=0;
break;
}
}

if (theRect.width>10) {
priorX = priorX + theRect.width;
} else {
priorX = priorX + 10;
ofSetColor(0, 191, 255); //deep sky blue
}
}
// start drawing characters 10 in from the side
priorX = 10;

}
}

try doing:

string(letters[i]) instead.

the problem is letters is an array of chars not an array of strings.
a single char (ie letters[i]) is a very different data type - more like an int.

Theo

Thank you very much for your suggestion.

I tried entering:

theFont.string(letters[i],x[i],y[i]);

and got

error: ‘class ofTrueTypeFont’ has no member named ‘string’

then I tried:

theFont.drawString(&letters[i],x[i],y[i]);
ofRectangle theRect = theFont.getStringBoundingBox(&letters[i],x[i],y[i]);

which did not produce any errors, but it also did not draw any letters when I pressed a key.

Does anyone have any idea why this is or what I could possibly change? Here is the code in its entirety. Many thanks again in advance!

  
#include "testApp.h"  
#include "stdio.h"  
#include <cstdlib>   
  
string str ="";  
  
int priorX=10;  
char letters[100];  
char theChars;  
int currLetter = 0;  
//boolean arrayFilled = false;  
int x[100];  
int y[100];  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	  
	  
	str =""; //initialize it as an empty string  
	  
	  
//-------------------------------load sound samples	  
	beatdistorted1.loadSound("sounds/beat distorted_house1.wav");  
	beatdistorted2.loadSound("sounds/beat distorted_house2.wav");  
	beatdistorted3.loadSound("sounds/beat distorted_house3.wav");  
	beatdistorted4.loadSound("sounds/beat distorted_house4.wav");  
	beatdistorted5.loadSound("sounds/beat distorted_house5.wav");  
	beatdistorted6.loadSound("sounds/beat distorted_house6.wav");  
	digital_dub1.loadSound("sounds/digital_dub1.wav");  
	digital_dub2.loadSound("sounds/digital_dub2.wav");  
	special_fx1.loadSound("sounds/special_fx1.wav");  
	  
	  
    ofSetFrameRate(30);  
    ofBackground(255,255,255); // draw on a white background  
	theFont.loadFont("Arial Black.ttf", 20,true,true,true); // Arial  
	ofSetLogLevel(OF_LOG_WARNING);  
	ofSetWindowShape(640,480);  
	ofSetFullscreen(false);  
	/*int i;  
	for (i = 0; i<str.length(); i++) {  
	theChars[i] = theFont.getCharacterAsPoints(str[i]);  
	}*/  
    vidGrabber.setVerbose(true);  
    vidGrabber.initGrabber(640,480);  
	  
	colorImg.allocate(640,480);  
	grayImage.allocate(640,480);  
	grayBg.allocate(640,480);  
	grayDiff.allocate(640,480);  
	bLearnBakground = true;  
	threshold = 80;  
}  
//--------------------------------------------------------------  
void testApp::update(){  
    bool bNewFrame = false;  
    vidGrabber.grabFrame();  
	bNewFrame = vidGrabber.isFrameNew();  
	  
	if (bNewFrame){  
        colorImg.setFromPixels(vidGrabber.getPixels(), 640,480);  
		colorImg.mirror(false,true);  
        grayImage = colorImg;  
		if (bLearnBakground == true){  
			grayBg = grayImage;  
			bLearnBakground = false;  
		}  
		// take the abs value of the difference between background and incoming and then threshold:  
		grayDiff.absDiff(grayBg, grayImage);  
		grayDiff.threshold(threshold);  
	}  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
	//for drawing beat colored letters  
	char tempStr[255];  
	  
//------------------draw letters	  
	  
	ofSetColor(0xffffff);  
	colorImg.draw(0,0);  
	//display runtime info for debugging  
	ofSetColor(0xff0000);  
	ofDrawBitmapString("fps: "+ofToString(ofGetFrameRate()), 10, 10);  
	ofDrawBitmapString("threshold: "+ofToString(threshold), 10, 20);  
	//advance the location of each character of the string  
	int i;  
    for (i = 0; i<str.length(); i++) {  
	int max;  
	if (arrayFilled) max = 100;  
	else max = currLetter;  
	for (int i=0; i < max; i++) {  
		  
		//New positions  
		x[i] = priorX;  
		y[i] = y[i]+5;  
		if (y[i]>480) {  
			y[i]=0;  
		}  
		  
		//Draw the character, get its outline  
	 // draw in green  
ofFill();  
		  
theFont.drawStringAsShapes(&letters[i],x[i],y[i]);  
ofRectangle theRect = theFont.getStringBoundingBox(&letters[i],x[i],y[i]);  
ofNoFill();  
		// if colliding with something, backup you are not colliding.  
		// must be a better approach here that can still avoid jitter.  
		while (grayDiff.countNonZeroInRegion(theRect.x,theRect.y,theRect.width,theRect.height)>0) {  
			ofSetColor(255,0,0); //red  
			theRect.y = theRect.y -1;  
			y[i] = y[i] - 1;  
			  
			// play a sound  
			if (letters[i] == 'a'){ // do somehing  
				beatdistorted1.play();  
			} else if (letters[i] == 'b') {  
				beatdistorted2.play();  
			} else if (letters[i] == 'c') {  
				beatdistorted3.play();  
			} else if (letters[i] == 'd') {  
				beatdistorted4.play();  
			}  
			break;  
			  
			  
			if (y[i]<0) {  
				y[i]=0;  
				break;  
			}   
		}  
			  
	  
		if (theRect.width>10) {  
			priorX = priorX + theRect.width;  
		} else {  
			priorX = priorX + 10;  
			ofSetColor(0, 191, 255); //deep sky blue  
		}  
    }  
	// start drawing characters 10 in from the side  
	priorX = 10;  
  
	}  
}  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){  
  
	//type characters/to input text, add a character to it. (also you want to take into account the backspace key)  
  
//	if(key==8 && str.size()>0) { // backspace  
//			str = str.substr(0, str.size()-1); // delete one character  
//	} else {  
//		str.append (1, (char)key );  
//	}  
	  
	letters[currLetter] = key;  
	currLetter = currLetter+1;  
	if (currLetter >= 100) {  
		currLetter = 0;  
		arrayFilled = true;  
	}  
	  
	//----------------------------- threshold for background contrast  
	switch (key){  
		case ' ':  
			bLearnBakground = true;  
			break;  
		case '+':  
			threshold ++;  
			if (threshold > 255) threshold = 255;  
			break;  
		case '-':  
			threshold --;  
			if (threshold < 0) threshold = 0;  
			break;  
			  
	  
		//--------------map letters to sound samples  
  
			  
  
		case 'a': beatdistorted1.play();   
			break;  
		case 'b': beatdistorted2.play();   
			break;  
		case 'c': beatdistorted3.play();   
			break;  
		case 'd': beatdistorted4.play();   
			break;  
		case 'e': beatdistorted5.play();   
			break;  
		case 'f': beatdistorted6.play();   
			break;  
		case 'g': digital_dub1.play();   
			break;  
		case 'h': digital_dub2.play();   
			break;  
		case 'i': special_fx1.play();   
			break;  
  
	  
	}  
}  
  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){  
}  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
}  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
}  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
}  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
}  
//--------------------------------------------------------------  
void testApp::windowResized(int w, int h){  
}  

possibly this topic would be better suited for the beginners section of the forum.

Single characters, or even arrays of single characters ( like - char letters[100]; ) are not strings, they are arrays of single characters. it seems like an array of characters should be a string but they are different. it’s one of the deep mysteries (and perhaps needless frustrations) of c++.

when you need to convert one type of data into another type of data this is called (explicit) type casting. so if you have a char[] and need to make it into a string you can use syntax like this : string( char[]). You tried to use it as if it was a method of the theFont object -

  
theFont.string(letters[i],x[i],y[i]);  

instead of

  
theFont( string(letters[i]), x[i], y[i]);  

this might help: http://www.cplusplus.com/doc/tutorial/typecasting/

cheers.

Instead of
Code:

  
>       
>     theFont( string(letters[i]), x[i], y[i]);  
>       
>     

Thats right except it should be

  
theFont.drawString( string(letters[i]), x[i], y[i]);  

Theo

yes, sorry. I should probably pay more attention to the code I’m typing when I’m trying to help someone! :oops:

Thanks for your help. I just tried that and am getting the same error. I will also post in the beginner’s forum if it is more appropriate.

I will look at the c++ reference you sent, but I have to admit I’m nearly clueless with c++ and started oF with just a bit of Processing knowledge and the desire to create (and unfortunately don’t really have a programmer’s background/knowledge).

try -

  
string myStr;  
myStr.push_back(letters[i]);  
theFont.drawString(myStr , x[i], y[i]);  
  

That sorta worked. No errors and the video screen and camera opened, but no letters appear at all.

I’m basically trying to create a version of Text Rain (where a user can interact with text falling on a screen via computer vision), but map each letter to a sound sample. So when the user “touches” a letter, a particular sound goes off.

If anyone has an idea how it can work with this code or what I can possibly change, I’d really appreciate it. I also have an alternate set of code with a completely different problem that I will post.

Thanks again for your help!