Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I do not understand the issue is in my code, I have not seen an error like this so I don't know where to start to debug it. I have only been learning to write code in C++ for about a month now. So, the issue is this "No suitable conversion function from const std::string to char exist." in this line of code "horses[i].name = HorseNames[i];" under HorseNames.

edit:: I add the declaration

C++
struct Horse
{
	char name;
	int distance;
	int eventOffSet;
	int ID;
};

        array <Horse, horseCount> horses;
	const string HorseNames[] = { "Black Beauty", "Sea Biscuit", "Bucephalus", 
                                      "Man o War", "Marengo", "Winning Colors" };

	horses[0].ID = 1;
	horses[1].ID = 2;
	horses[2].ID = 3;
	horses[3].ID = 4;
	horses[4].ID = 5;
	horses[5].ID = 6;


 for (int i = 0; i < horseCount; i++)
		{
			horses[i].name = HorseNames[i];
			cout << "These are the Galent Steeds racing today!: "  
                             <<  horses[i].name << endl;
		}


What I have tried:

I have googled it and couldn't find anything, I tried to change my const string to a static, renamed it to char and nothing seemed to work.
Posted
Updated 28-Jul-17 5:35am
v2
Comments
[no name] 27-Jul-17 21:03pm    
Post a small snippet of code showing where horses and HorseNames are declared.

As already suggested you should show the declaration of the tpyes.

However, from the error message something like this can be assumed:
C++
std::string HorseNames[MAX_HORSES];
struct {
    char name;
    // Probably more members here
} Horses;
Horses horses[MAX_HORSES];
Then HorseNames[i] is a std::string which can not be assigned to a single char like horses[i].name.

You probably want Horses.name to be a std::string too or a char array. In the latter case you could not use an assignment but have to copy the string:
C++
char name[MAX_NAME_LEN];
// Use strncpy or a safe string copy method here to avoid buffer overruns
strncpy(name, HorseNames[i], sizeof(name) / sizeof(name[0]));
// NULL terminate when using strncpy and string is too long
name[sizeof(name) / sizeof(name[0]) - 1] = '\0';
 
Share this answer
 
Comments
Irdia_36 28-Jul-17 14:57pm    
My understanding of pointers isn't real clear, I would need to place the string copy into the for each loop and have it point to the struct name?
Jochen Arndt 29-Jul-17 4:20am    
You actually have neither a pointer nor an array:
char name;
is a single character.

You need an array of charaters (char name[SIZE]) where the data must be copied to that or a pointer (char *namePtr) where another pointer can be assigned. But that will only work if the source exists as long as the destination.
You cannot store a string in a char variable; a char type can hold only a single character. You need either a pointer to a string (char*), or a character array (char[]) as Jochen has suggested above.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900