Error convert argument const char to char

Hi, I am learning inheritance, and having an issue with a piece of my code. I am having an error when I try and create the object which says no instance of the constructor triangle matches the argument list, and it cannot convert 1 from const char[10] to a char *

I assume the "isosceles" I am trying to enter is the const char? but why is this a const, and not just a char? How would I fix this problem. I cannot make the char style a const, as strcpy doesnt seem to work with const char.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <iostream>
#include <cstring>

class TwoDString
{
	double width;
	double height;

public:
	void showDim()
	{
		std::cout << "Width and height are " << width << " and " << height << "\n";
	}

	double getWidth() { return width; }
	double getHeight() { return height; }
	void setWidth(double w) { width = w; }
	void setHeight(double h) { height = h; }
};

class Triangle : public TwoDString
{
	char style[20];

public:
	Triangle(char* str, double w, double h)
	{
		setWidth(w);
		setHeight(h);
		strcpy_s(style, str);
	}

	double area()
	{
		return getWidth() * getHeight() / 2;
	}

	void showStyle()
	{
		std::cout << "Triangle is " << style << "\n";
	}
};

int main()
{
	Triangle t1("Isosceles", 4.0, 4.0);

	std::cout << "Info for t1: \n";
	t1.showStyle();
	t1.showDim();
	std::cout << "Area is " << t1.area() << "\n";

	return 0;
}
You don't make style const, you make str const. It's a const as you are passing 'Isosceles" which is a const char* as you can't change its contents.

 
Triangle(const char* str, double w, double h)


strcpy_s() is correct.
Last edited on
Use strcpy, not strcpy_s, you're not gaining anything by a so-called safe copy here.

In particular, note that strcpy_s takes 3 parameters, not 2:
https://en.cppreference.com/w/c/string/byte/strcpy
Last edited on
use c++ string and those kinds of issues go away, but try this:

line 46
char derp[] = "Isosceles";
Triangle t1(derp, 4.0, 4.0);

for some unholy reason they took away the ability to dump raw strings in there (breaking decades of code that did so).
you can also do this:

Triangle t1(("Isosceles"s).c_str(), 4.0, 4.0); //force to c++ string and then force back to c-string.. ugly...
Last edited on
.c_str() produces of type const char*. To get char* use .data() in C++17.

Just change Triangle. It should be const anyhow as constructor doesn't change the data.
Thank you, I tried both seeplus and jonnin ways, and they both worked for me, so thank you. I thought I had already tried making the triangle constructor with the const, but i made that change along with the changing style to const, so assumed it didnt work since i done both at the same time. Stupid of me.

Lastchance when i tried to use strcpy, I was getting an error saying it was unsafe and to use strcpy_s. However, i have changed it back and now it seems to be running fine :/ . I was getting that error on a different computer, so not sure if that had something to do with it.
> I am learning inheritance
explain how a triangle is a kind of TwoDString (whatever that is)
consider a composition relationship instead
1
2
TwoDString xx;
// What are the width and height of xx now? 


The TwoDString could/should have a constructor. For example:
1
2
3
4
5
6
7
8
9
10
class TwoDString
{
    // other code
public:
    TwoDString( double w, double h )
     : width{ w }, height{ h }
    { }

    // other code
};

With that in place the constructor of deriving class does not need to call functions:
1
2
3
4
5
Triangle( char* str, double w, double h )
 : TwoDString( w, h )
{
  // copy str to style
}
In particular, note that strcpy_s takes 3 parameters, not 2:


For VS (and others?), there are templated overrides for strcpy_s() that take 2 params for when the destination size can be determined.

https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strcpy-s-wcscpy-s-mbscpy-s?view=vs-2019

Last edited on
and others?

Like which?

It's a highly non-portable function (especially when used with 2 parameters).
Topic archived. No new replies allowed.