#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "image.h"
int main (){
initwindow(600,400,"Game");
image img("C:/Users/amit/Desktop/game/images/alien.jpg");
...
and when i compile and run on my DE (Dev-C++) i get the following errors:
1 2 3 4
C:\Users\amit\Desktop\game\main.cpp In file included from main.cpp
C:\Users\amit\Desktop\game\image.h In constructor `image::image(char*)':
C:\Users\amit\Desktop\game\image.h invalid conversion from `char*' to `char'
C:\Users\amit\Desktop\game\image.h invalid conversion from `char' to `constchar*'
could someone please explain to me how to pass strings into arrays in c because declaring strings like this - string s = "blahblahblah"; doesn't work for me.
It isn't going to work, as src[] isn't initialized in the class, and you cannot initialize objects in a class in the way src needs to be initialized.
There are three ways of doing things:
1. std::string, the most balanced between easy and security
2. char *, the harder and most balanced between speed and security
1 2 3 4 5 6 7 8 9 10 11
char * src;
public:
image(constchar * s, point p, scale z){
unsignedint len = strlen(s);
src = newchar[s+1];
strcpy(src,s);
}
~image(){
delete[] src; // You need to delete[] src everytime src is not going to be used anymore
src = 0;
}
3. const char *, the easier, but less safer, really not suggesting this way