Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid conversion from "const char*" to "char" error [duplicate]

Tags:

c++

I wanted to post this because I was not really sure what issue I was having with a simple assignment statement. I am doing a homework assignment that asks me to write structs and functions in a simple program to draw out shapes of ASCII characters. Right now I am just trying to test the functions I have written, and I am trying to assign a value to the symbol element of a Circle struct just to test out the DrawShape function I wrote. When I try to assign it a * char, I get an error message saying "error: invalid conversion from 'const char*' to 'char'". I will put the whole code in, though it is very long and unfinished. Any help with this would be appreciated. The problem I am getting is right in the beginning of the main at "circle1.char = '*' "

#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;

const int NUMBER_OF_ROWS = 26;
const int NUMBER_OF_COLUMNS = 81;
char drawSpace[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];

struct Point{
    int x;
    int y;
};

struct Circle{
    Point center;
    int radius;
    char symbol;
    bool buffer[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
};

bool setCircleRadius(Circle &b, int r);
bool setCircleCenter(Circle &b, int x, int y);
bool moveCircle(Circle &b, int x, int y);
void drawCircle (Circle b);
void lineChars(Line a);
void circleChars(Circle b);
void drawShapes();

int main() {
    Circle circle1;
    circle1.radius = 5;
    circle1.symbol = "*";
    circle1.center.x = 40;
    circle1.center.y = 10;

    drawCircle(circle1);

    return 0;
}
like image 739
classISover Avatar asked Nov 16 '11 20:11

classISover


3 Answers

You should be using single quotes for characters. Double quotes means you're using a (potentially single-character) string literal, which is represented as a const char * (pointer to constant character).

Correct syntax: circle1.symbol = '*';

like image 173
Platinum Azure Avatar answered Nov 14 '22 22:11

Platinum Azure


The problem is here:

circle1.symbol = "*";

circle1.symbol is defined as char, but you assign a string to it (an array of chars). What you need to do is

circle1.symbol = '*';
like image 9
Adam Trhon Avatar answered Nov 14 '22 21:11

Adam Trhon


Your definition of Circle says that symbol is a char, yet you try to assign it a string literal of type char[2]:

circle1.symbol = "*";

Instead, you should be assigning it a char:

circle1.symbol = '*';
like image 3
K-ballo Avatar answered Nov 14 '22 23:11

K-ballo